【问题标题】:ant taskdef class cannot be found using the classloader AntClassLoader[]使用类加载器 AntClassLoader[] 找不到 ant taskdef 类
【发布时间】:2013-09-04 00:26:18
【问题描述】:

我正在开发一个以编程方式运行 Ant XML 文件的 Eclipse 插件。
我使用 org.apache.tools.ant.helper.ProjectHelperImplorg.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 pathtask definitionstest custom ant task 的情况下以编程方式执行文件,它将成功运行。

我的假设是,以编程方式运行 Ant 文件时,它没有以某种方式注册类路径?

编辑:
多个目标名称test&lt;!-- test echo --&gt; 目标名称更改为testEcho。 (感谢:@smooth reggae)

【问题讨论】:

  • 您的样本中有错字吗?您有两个名为 test 的目标
  • @smoothreggae:我的错,我现在已经编辑了目标名称。谢谢。

标签: java xml eclipse ant eclipse-plugin


【解决方案1】:

我已经设法修复了我的错误,我更改了调用 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();

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多