java - 了解 Gradle/Groovy 任务的微妙之处

标签 java groovy gradle

运行构建脚本时出现错误:

task pullInDeps(dependsOn: copyMod, description: 'Pull in all the module dependencies for the module into the nested mods directory') {
if (pullInDeps == 'true') {
    setSysProps()
    def args = ['pulldeps', moduleName]
    Starter.main(args as String[])
}
}

但是,我在运行时没有收到错误:

task pullInDeps(dependsOn: copyMod, description: 'Pull in all the module dependencies for the module into the nested mods directory') << {
    if (pullInDeps == 'true') {
        setSysProps()
        def args = ['pulldeps', moduleName]
        Starter.main(args as String[])
    }
}

注意:区别在于定义任务时的<<。另请注意,如果前者是通过 if 语句 周围的 doLast{} 完成的,则它可以工作。并且在使用 doFirst{} 时有效

--这是来自 vert.x gradle-template-example(但将其添加到我自己的项目中)。

我真的只是想更好地理解 gradle/groovy,因为我已经解决了这个问题。

编辑:

错误:

* What went wrong:
A problem occurred evaluating script.
> Could not find property 'Starter' on task ':pullInDeps'.

我不确定为什么 leftShiftdoFrist/Last() 对 Starter 产生影响。

最佳答案

相关任务有一个与其关联的操作(闭包内的逻辑),该操作使用 leftShift 表示。运算符(operator)。这是实际的语义:

task pullInDeps << { task action }

任务本身作为参数传递到闭包中,用于定义操作。

这与doFirst { }同义。和 doLast { }它将任务本身作为参数。

如果您将任务定义为:

task pullInDeps { }

任务本身将被配置,而不是定义任何操作,因此任务本身作为参数在闭包中不可用。

引用Task Actions中的第二段.

关于java - 了解 Gradle/Groovy 任务的微妙之处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24088880/

相关文章:

gradle - 使用Tar Task为Gradle中的每个文件夹创建tar.gz

gradle - 只能在NetBeans中加载一些gradle项目

java - Batik 传递库依赖项

java - 如何测试字符串数组以查找数组中是否包含特定字符串?

java - Cucumber-JVM 3 - 使用 Maven 的 Allure 2 测试报告

jenkins - 如何从groovy脚本运行jenkins作业

grails - 找出哪个Quartz触发器触发了作业

android - Android Studio 中的项目不再起作用

java - Spring Boot 数据源设置

java - 在 java 中使用 HashMap<Object,Object> 时,键的类型是什么?