【问题标题】:Understanding a subtlety of Gradle/Groovy tasks了解 Gradle/Groovy 任务的微妙之处
【发布时间】:2014-06-06 18:59:52
【问题描述】:

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

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[])
    }
}

注意:区别在于定义任务时的doLast{} 围绕if statement 完成的,它可以工作。并且在使用 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 有影响。

【问题讨论】:

    标签: java groovy gradle


    【解决方案1】:

    相关的task 有一个与之相关的动作(闭包内的逻辑),使用leftShift 运算符表示。这是实际的语义:

    task pullInDeps << { task action }
    

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

    这是doFirst { }doLast { } 的同义词,将任务本身作为参数。

    如果您将任务定义为:

    task pullInDeps { }
    

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

    参考Task Actions中的第二段。

    【讨论】:

    • 我想我应该提到 pullInDeps 也是 gradle.properties 中的一个变量(设置为 true)。这就是我相信你的意思:“因此任务本身作为参数在闭包中不可用。” ?我知道leftShift 运算符用于定义操作顺序,但我不明白为什么它在这个特定任务中会有所不同,因为没有做任何其他事情。
    • Starter导入了吗?
    • 有趣的是,Starter被导入了,但是intellij找不到(虽然类肯定在jar里,但intellij没有显示)。最有趣的是,当使用leftShift 并以某种方式检测到 Starter 类时,它仍然有效。我不确定这是为什么.. 我正在使用 vert.x 和 vert.x-gradle-template。
    • 原因:当 leftShift 运算符 (copyMod 之前运行时 - 它依赖于什么(以及在 Starter 类 buildscript 依赖中引入了什么)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2012-09-29
    • 2019-03-02
    相关资源
    最近更新 更多