Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用类加载器AntClassLoader[]找不到ant taskdef类_Java_Xml_Eclipse_Ant_Eclipse Plugin - Fatal编程技术网

Java 使用类加载器AntClassLoader[]找不到ant taskdef类

Java 使用类加载器AntClassLoader[]找不到ant taskdef类,java,xml,eclipse,ant,eclipse-plugin,Java,Xml,Eclipse,Ant,Eclipse Plugin,我正在开发一个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"> <filese

我正在开发一个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>
以编程方式运行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);
手动运行目标
测试
很好,但运行
测试
目标会以编程方式显示此错误

taskdef class my.custom.task.CustomTask cannot be found using the classloader AntClassLoader[]
如果我也将在没有
项目类路径
的情况下以编程方式执行文件,
任务定义
测试自定义ant任务
,它将成功运行。

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

编辑:

多目标名称
test
已将
目标名称更改为
testEcho
。(归功于:@smooth reggae)

我已经设法修复了我的错误,我更改了调用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();
//从首选项获取默认自定义类路径
AntCorePreferences-corePreferences=AntCorePlugin.getPlugin().getPreferences();
URL[]URL=corePreferences.getURL();
//获取插件jar的位置
File bundleFile=FileLocator.getBundleFile(myPlugin.getBundle());
URL=bundleFile.toURI().toURI();
//绑定URL以完成类路径
List classpath=new ArrayList();
addAll(Arrays.asList(url));
添加(url);
AntRunner runner=新的AntRunner();
//设置自定义类路径
setCustomClasspath(classpath.toArray(新URL[classpath.size()]);
//设置生成文件位置
runner.setBuildFileLocation(xmlFile.getAbsolutePath());
//设置生成记录器
runner.addBuildLogger(MyDefaultLogger.class.getName());
//设置要执行的特定目标
setExecutionTargets(新字符串[]{“test”});
//跑
runner.run();

您的样本中是否有打字错误?您有两个名为
test
@smoothreggae的目标:我的错,我现在已经编辑了目标名称。谢谢
// 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();