Gradle任务的doFirst()不会首先执行,而是在任务完成后执行

时间:2018-08-13 12:45:03

标签: java gradle

我用Java创建了Gradle任务:

class TestTask extends Exec {

    public TestTask() {

        doLast(new Action<Object>(){
            @Override
            public void execute(Object task) {
                System.out.println("Should be last ");
            }
        });

        doFirst(new Action<Object>(){
            @Override
            public void execute(Object task) {
                System.out.println("Should be first ");
            }
        });

        commandLine("echo", "Should be between");
    }
}

执行此任务,我希望得到

Should be first
Should be between
Should be last

但是我得到了

Should be between
Should be first
Should be last

我正在将插件从Groovy转换为Java,在Groovy中,按如下方式定义时,处理顺序符合预期:

task dofirsttest(type: Exec) {
    doFirst{
        println "Should be first"
    }
    doLast{
        println "Should be last"
    }
    commandLine "echo", "Should be between"
}

我的任务有问题吗?

1 个答案:

答案 0 :(得分:0)

commandLine("echo", "Should be between");处于任务的配置阶段。因此它将首先执行。看看:https://docs.gradle.org/current/userguide/build_lifecycle.html