Reputation: 51
I'm developing an eclipse plugin that will run an Ant XML file programatically.
I used org.apache.tools.ant.helper.ProjectHelperImpl
and org.apache.tools.ant.Project
then parse the Ant XML file and run a specific target, in this case test
.
My Ant XML file looks like this:
<!-- project class path -->
<path id="projectClassPath">
<fileset dir="${basedir}/jars">
<include name="**/*.jar" />
</fileset>
</path>
<!-- task definitions -->
<taskdef file="${basedir}/tasks.properties">
<classpath refid="projectClassPath" />
</taskdef>
<!-- test custom ant task -->
<target name="test" description="test target">
<customTask />
</target>
<!-- test echo -->
<target name="testEcho" description="test echo target">
<echo message="this is a test."/>
</target>
The tasks.properties file looks like this:
customTask=my.custom.task.CustomTask
My java code that runs the Ant file programatically:
Project ant = new Project();
ProjectHelperImpl helper = new ProjectHelperImpl();
MyDefaultLogger log = new MyDefaultLogger();
ant.init();
helper.parse(ant, antXml);
log.setMessageOutputLevel(Project.MSG_VERBOSE);
ant.addBuildListener(log);
ant.executeTarget(testTarget);
Running the target test
manually, is fine, but running the test
target programatically displays this error:
taskdef class my.custom.task.CustomTask cannot be found using the classloader AntClassLoader[]
If I will also be executing the file programatically without the project class path
, task definitions
and test custom ant task
it will run successfully.
My assumption is that, when running the Ant file programatically, it did not register the classpath somehow?
EDIT:
multiple target name test
changed <!-- test echo -->
target name to testEcho
. (credits to: @smooth reggae)
Upvotes: 2
Views: 14433
Reputation: 51
I have managed to fix my error, I changed my Java implementation of calling the Ant XML file, and it worked like a charm.
My Java code looks like this:
// get the default custom classpath from the preferences
AntCorePreferences corePreferences = AntCorePlugin.getPlugin().getPreferences();
URL[] urls = corePreferences.getURLs();
// get the location of the plugin jar
File bundleFile = FileLocator.getBundleFile(myPlugin.getBundle());
URL url = bundleFile.toURI().toURL();
// bond urls to complete classpath
List<URL> classpath = new ArrayList<URL>();
classpath.addAll(Arrays.asList(urls));
classpath.add(url);
AntRunner runner = new AntRunner();
// set custom classpath
runner.setCustomClasspath(classpath.toArray(new URL[classpath.size()]));
// set build file location
runner.setBuildFileLocation(xmlFile.getAbsolutePath());
// set build logger
runner.addBuildLogger(MyDefaultLogger.class.getName());
// set the specific target to be executed
runner.setExecutionTargets(new String[] { "test" });
// run
runner.run();
Upvotes: 1