开发者

Calling Ant target from within custom task causes 'failed to create task or type import' error

开发者 https://www.devze.com 2023-01-16 06:14 出处:网络
I have created a custom Ant task as per one of my previous posts which calls an existing target in another xml file.

I have created a custom Ant task as per one of my previous posts which calls an existing target in another xml file.

It seems to be doing what I want in terms of calling the build xml that I want, however, it now throws a very curious error:

build.xml:4: Problem: failed to create task or type import
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
    at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:508)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:369)
    at org.hardhat.task.HardHatTask.executeHardHat(HardHatTask.java:47)
    at org.hardhat.task.HardHatTask.execute(HardHatTask.java:23)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)

The build xml that my custom Ant task is calling looks something like this:

<project name="myproject" default="all" basedir=".">
    <import file="includes.xml"/>

    <target name开发者_JAVA技巧="all" depends="doStuff" />
</project>

The error says the problem has to do with line 4 and the task 'import'. Any ideas as to why Ant doesn't seem to understand one of its own tasks?


Interestingly, it looks like the reason why it wasn't working was because my custom Ant task looked like this:

public void execute() {
    Ant ant = new Ant();
    Project project = new Project();
    project.setProperty("...", ...);
    ant.setProject(project);
    File directory = new File("...");
    ant.setDir(directory);
    ant.setAntfile("build.xml");
    ant.setTarget("all");
    ant.execute();
}

All I had to do was call getProject() instead of new Project() and the problem went away:

public void execute() {
    Ant ant = new Ant();
    Project project = getProject();
    project.setProperty("...", ...);
    ant.setProject(project);
    File directory = new File("...");
    ant.setDir(directory);
    ant.setAntfile("build.xml");
    ant.setTarget("all");
    ant.execute();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消