Gradle变量不在复制和重命名任务的范围内

时间:2019-03-20 17:26:48

标签: spring-boot gradle

当Spring Boot的bootWar插件创建战争时,我试图使用变量(从共享文件填充)添加版本号。但是,该变量似乎不在范围内。我在bootWar中添加了doFirst,以显示在创建战争之前确实已填充了变量。 This question建议使用项目,但这也不起作用(请在下面第二次尝试)。在构建过程的这一点上不能使用变量吗?

task populateBuildVersion (dependsOn: ["buildVersionFromFile", "buildVersionPlaceholder"]){
  doLast {
        project.ext.buildVersion = buildVersion
    println "------------------------------------------------------------------"
    println "|   Build version: $buildVersion"
    println "------------------------------------------------------------------"
  }
}

bootWar {
  into('') {
    from "${project(':web').projectDir}/src/main/webapp"
        // PICK ONE, COMMENT THE OTHERS:
        rename "webpacked.js", buildVersion // fails at task runtime with "Caused by: java.lang.NullPointerException: replacement"
        rename "webpacked.js", project.buildVersion // fails at initialization with "Could not get unknown property 'buildVersion' for root project 'myproject' of type org.gradle.api.Project."
        rename "webpacked.js", "${buildVersion}.js" // results in null.js
  }
    doFirst {
        println "in bootWar dofirst: ${buildVersion}" // OK
        println buildVersion // OK
        println project.buildVersion // OK
    }
}
tasks.bootWar.dependsOn(populateBuildVersion)

输出:

> Task :populateBuildVersion
------------------------------------------------------------------
|   Build version: xyz_7.8.2_2417
------------------------------------------------------------------

> Task :bootWar
in bootWar dofirst: xyz_7.8.2_2417
xyz_7.8.2_2417
xyz_7.8.2_2417

> Task :bootWar FAILED (for various reasons)

1 个答案:

答案 0 :(得分:1)

使用采用闭包或rename作为参数的Transformer<String, String>方法。执行任务时将调用闭包(或Transformer)。在您的版本中,配置任务后将访问该属性,因此populateBuildVersion任务尚未执行:

from("${project(':web').projectDir}/src/main/webapp") {
    rename {
        it == 'webpacked.js' ? "${buildVersion}.js" : null
    }
}