Apache Ant Java API未执行所有具有依赖关系的目标

时间:2018-10-04 16:59:33

标签: java ant

我创建了一个小脚本来测试Ant Java api的某些功能。不幸的是,此级别的文档似乎很少。

我希望可以打印以下代码段:

All Even Odd Done

但是它将打印以下内容:

Even Odd Done

package com.amazon.tools.desktop;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.helper.DefaultExecutor;

public class AntTest
{
    public static void main(final String... args)
    {
        final Project project = new Project();

        final Target printAll = new Target();
        printAll.setProject(project);
        printAll.setName("printAll");
        project.addTarget(printAll);
        printAll.addTask(new LambdaTask(project, () -> System.out.println("All")));

        final Target printEven = new Target();
        printEven.setProject(project);
        printEven.setName("printEven");
        project.addTarget(printEven);
        printEven.addTask(new LambdaTask(project, () -> System.out.println("Even")));
        printEven.dependsOn("printAll");

        final Target printOdd = new Target();
        printOdd.setProject(project);
        printOdd.setName("printOdd");
        project.addTarget(printOdd);
        printOdd.addTask(new LambdaTask(project, () -> System.out.println("Odd")));
        printOdd.addDependency("printEven");


        final Target finalTarget = new Target();
        finalTarget.setProject(project);
        finalTarget.setName("finalTarget");
        project.addTarget(finalTarget);
        finalTarget.addTask(new LambdaTask(project, () -> System.out.println("Done")));
        finalTarget.addDependency("printEven");
        finalTarget.addDependency("printOdd");

        project.setExecutor(new DefaultExecutor());
        project.executeTarget("finalTarget");
    }

    public static class LambdaTask extends Task
    {
        Runnable task;

        public LambdaTask(final Project project,
                          final Runnable task)
        {
            this.task = task;
            setProject(project);
        }

        @Override
        public void execute()
        {
            task.run();
        }
    }
}

我可以删除finalTarget.addDependency("printEven");行,并且仍将通过printOdd目标对其具有的依赖关系来临时执行该目标,但是由于某种原因,尽管具有传递依赖项,printAll却从未得到执行。我在这里想念东西吗?

0 个答案:

没有答案