Java 无法使用类加载器 AntClassLoader[] 找到 ant taskdef 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18547053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ant taskdef class cannot be found using the classloader AntClassLoader[]
提问by Nyahaha Wakoko
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:
我正在开发一个 eclipse 插件,它将以编程方式运行 Ant XML 文件。
我使用org.apache.tools.ant.helper.ProjectHelperImpl
并org.apache.tools.ant.Project
解析 Ant XML 文件并运行特定目标,在本例中为test
.
我的 Ant XML 文件如下所示:
<!-- 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:
tasks.properties 文件如下所示:
customTask=my.custom.task.CustomTask
My java code that runs the Ant file programatically:
我以编程方式运行 Ant 文件的 java 代码:
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:
test
手动运行目标很好,但以test
编程方式运行目标会显示此错误:
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)
如果我也将是没有编程执行文件project class path
,task definitions
并且test custom ant task
它会成功运行。
我的假设是,当以编程方式运行 Ant 文件时,它没有以某种方式注册类路径?
编辑:
多个目标名称test
将<!-- test echo -->
目标名称更改为testEcho
. (归功于:@smooth 雷鬼)
采纳答案by Nyahaha Wakoko
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:
我已经设法修复了我的错误,我更改了调用 Ant XML 文件的 Java 实现,并且它的工作非常出色。
我的 Java 代码如下所示:
// 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();