【发布时间】:2013-09-04 00:26:18
【问题描述】:
我正在开发一个以编程方式运行 Ant XML 文件的 Eclipse 插件。
我使用 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>
tasks.properties 文件如下所示:
customTask=my.custom.task.CustomTask
我以编程方式运行 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);
手动运行目标test 很好,但以编程方式运行test 目标会显示此错误:
taskdef class my.custom.task.CustomTask cannot be found using the classloader AntClassLoader[]
如果我也将在没有project class path、task definitions 和test custom ant task 的情况下以编程方式执行文件,它将成功运行。
我的假设是,以编程方式运行 Ant 文件时,它没有以某种方式注册类路径?
编辑:
多个目标名称test 将<!-- test echo --> 目标名称更改为testEcho。 (感谢:@smooth reggae)
【问题讨论】:
-
您的样本中有错字吗?您有两个名为
test的目标 -
@smoothreggae:我的错,我现在已经编辑了目标名称。谢谢。
标签: java xml eclipse ant eclipse-plugin