Re: Automating JarDesc jar generation from ant [message #258972] |
Thu, 05 March 2009 12:27 |
J.-P. Pellet Messages: 71 Registered: July 2009 |
Member |
|
|
> I've got a complicated .jardesc that combines multiple projects, with
> exclusions. After jar creation I need to sign it - which doesn't seem
> to be supported by the .jardesc wizard.
>
> I'd like to invoke the "Create Jar" task on the .jardesc from Ant so
> that I can use another ant task to do the jar signing without manual
> intervention.
>
> I don't want to maintain the filesets in both .jardesc and build.xml.
>
> Is this ability currently exposed, or are there any documents that
> describe how to invoke an Eclipse action from within Ant so I can
> develop my own Ant task?
>
> Thanks for any pointers.
>
> Regards, Jon
Jon, I'm having the same problem 5 years later. As I still couldn't find
an easy solution, I created my own task. It fires the Create Jar Eclipse
action just as if you had right-clicked the jardesc file and selected
Create Jar, and so avoids duplicating the jar information in the jardesc
and in build.xml.
First, create a new plugin project, and add dependencies on
org.apache.ant, org.eclipse.core.runtime, org.eclipse.jst.ui,
org.eclipse.core.resources, adn org.eclipse.ui. Next, create a new class
similar to this one:
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.internal.ui.jarpackager.CreateJarActionDeleg ate;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.PlatformUI;
@SuppressWarnings( "restriction" ) // for CreateJarActionDelegate
public class EclipseJar extends Task {
private String jardesc;
public void setJardesc( String jardesc ) {
this.jardesc = jardesc;
}
@Override
public void execute() throws BuildException {
Runnable doIt = new Runnable() {
public void run() {
PlatformUI.getWorkbench().getDisplay().syncExec(
new Runnable() {
public void run() {
try {
CreateJarActionDelegate action =
new CreateJarActionDelegate();
action.selectionChanged( null,
new DummyJardescSelection( jardesc ) );
action.run( null );
} catch( Exception exc ) {
exc.printStackTrace( System.out );
}
}
} );
}
};
doIt.run();
}
private static class DummyJardescSelection
implements IStructuredSelection {
private final IFile jardesc;
public DummyJardescSelection( String jardesc ) {
this.jardesc = (IFile) ResourcesPlugin.getWorkspace().getRoot().
findMember( new Path( jardesc ) );
}
public Object getFirstElement() {
return jardesc;
}
@SuppressWarnings( "unchecked" )
public Iterator iterator() {
return toList().iterator();
}
public int size() {
return 1;
}
public Object[] toArray() {
return toList().toArray();
}
@SuppressWarnings( "unchecked" )
public List toList() {
return Collections.singletonList( jardesc );
}
public boolean isEmpty() {
return false;
}
}
}
Next, package this compiled class as a jar, say MyAntTasks.jar, and in
your build.xml file, put something like this:
<project name="projectname" basedir="." default="jar">
<taskdef name="eclipseJar" classname="EclipseJar"
classpath="../anttaskproject/MyAntTasks.jar"/>
<property name="jarfile" value="jardestproject/FinalJar.jar" />
<property name="jardescfile" value="jardestproject/FinalJar.jardesc"/>
<target name="clean">
<!-- Delete the jar file -->
<delete file="${jarfile}" />
</target>
<target name="jar">
<!-- Build the jar file the Eclipse way, using the jardesc -->
<eclipseJar jardesc="${jardescfile}" />
</target>
</project>
Be sure to run this ant task in the same JRE as the IDE. This works for
me. Any suggestion for improvement?
Cheers,
J.-P.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03052 seconds