【问题标题】:Why is my Gradle task always running?为什么我的 Gradle 任务总是在运行?
【发布时间】:2015-07-13 16:29:27
【问题描述】:

如果我运行 ./gradlew clean./gradlew tasks --all,它总是在运行我的编译任务(我在下面的 gradle 构建脚本中覆盖了它)

task eclipse(overwrite: true) {
    exec { commandLine = ["./play1.3.x/play", "eclipsify"] }
}

task compileJava(overwrite: true) {
    exec { commandLine = ["./play1.3.x/play", "precompile"] }
}

task deleteDirs(type: Delete) {
    delete 'precompiled', 'tmp'
}

//NOW, assemble needs to zip up directories precompiled, public, lib, and conf
clean.dependsOn('deleteDirs')

我不明白为什么 eclipse 每次都没有运行,并且似乎工作得很好,而覆盖 compile 一个不起作用。

【问题讨论】:

    标签: gradle


    【解决方案1】:

    了解任务配置和任务执行的区别非常重要:

    task eclipsify {
        // Code that goes here is *configuring* the task, and will 
        // get evaluated on *every* build invocation, no matter
        // which tasks Gradle eventually decides to execute.
        // Don't do anything time-consuming here.
        doLast {
            // `doLast` adds a so-called *task action* to the task.
            // The code inside the task action(s) defines the task's behavior.
            // It will only get evaluated if and when Gradle decides to 
            // execute the task.
            exec { commandLine = ["./play1.3.x/play", "eclipsify"] }
        }
    }
    
    // Improving on the previous task declaration, let's now use a *task type* 
    // (see `type: Exec` below). Task types come with a predefined task action, 
    // so it's typically not necessary to add one yourself. Also, many task types 
    // predefine task inputs and outputs, which allows Gradle to check if the task 
    // is up-to-date. Another advantage of task types is that they allow for 
    // better tooling support (e.g. auto-completion of task properties).
    task precompile(type: Exec) {
        // Since this task already has a task action, we only
        // need to configure it.
        commandLine = ["./play1.3.x/play", "precompile"] }
    }
    

    如果您没有正确进行配置与执行,您会看到诸如启动时间过长以及任务似乎在不应该执行的情况下执行等症状。

    要了解哪些任务类型可用以及如何配置它们,请查看Gradle Build Language Reference。此外,还有越来越多的第三方插件和任务类型。

    PS:我更改了任务名称并删除了overwrite: True(只能作为最后的手段使用),以免分散我回答的主要信息。

    【讨论】:

      【解决方案2】:

      Gradle 不知道您的源没有更改。对于任何未知状态,它会将任务标记为不是最新的。由于您的任务是 100% 替换 compile,因此您有责任提供任务的状态。

      Writing Custom Task Classes 章节详细介绍了如何开始使用增量任务。

      使用--info 标志运行您的项目,看看Gradle 为何将compile 任务标记为不是最新的。

      希望对你有帮助。

      【讨论】:

      • 但我运行的是 clean 不依赖于 compileJava 并且它不运行我也覆盖的 eclipse 任务?
      • 实际上,是的,你是。 @Andrey 的回答试图保留您对覆盖任务的感知愿望,这是 Gradle 提供但看似非常不正统的设施。当然当然您可以添加自己的自定义任务以在执行阶段触发。这很明显,但要感谢他尝试以您似乎更喜欢的不那么传统的方式帮助您完成同样的事情。
      • 我想您需要提供一个示例,因为我根本看不到您的答案如何解决它。毕竟,即使我添加了最新的检查,我之前拥有的 gradle compileJava 仍然会运行我不想要的(或者至少据我所知,他们会)。我很想看到一种不同的方法……了解多种解决方案总是有帮助的。
      • @Vidya:我也希望看到一个例子,这似乎是记录最少的领域之一。
      猜你喜欢
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 2014-06-10
      相关资源
      最近更新 更多