ant taskdef class cannot be found using the classloader AntClassLoader[]
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();
Nyahaha Wakoko
Updated on June 04, 2022Comments
-
Nyahaha Wakoko almost 2 years
I'm developing an eclipse plugin that will run an Ant XML file programatically.
I usedorg.apache.tools.ant.helper.ProjectHelperImpl
andorg.apache.tools.ant.Project
then parse the Ant XML file and run a specific target, in this casetest
.
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 thetest
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
andtest 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 nametest
changed<!-- test echo -->
target name totestEcho
. (credits to: @smooth reggae)