Grails的“战争”调用未将额外的属性传递给Gradle

时间:2019-11-01 19:15:56

标签: gradle grails groovy

当调用“ grails war”命令时,grails.env属性将传递给Gradle,但是我用-D定义的任何其他属性都不会传递。

我已验证Gradle将获得属性,并且可以使用诸如“ gradle -Dgrails.env = development -Dfoo.bar = blech”之类的命令来打印属性

使用以下命令调用grails:

grails -Dgrails.env =开发-Dfoo.bar =死战 build.gradle:

ext {
    currentBuildEnvironment = System.properties['grails.env']
    println "Current build environment is ${currentBuildEnvironment}"
    fooBar = System.properties['foo.bar']
    println "fooBar: ${fooBar}"
}

这将为currentBuildEnvironment正确打印“开发”,但为fooBar打印null。

1 个答案:

答案 0 :(得分:0)

默认情况下,您不允许将自定义属性传递给grails命令。

来自https://github.com/nick-bratton/AdobeXD-Scale-Everything

  

grails命令是gradle调用的前端,因此   可能会有意想不到的副作用。例如,执行时   grails -Dapp.foo = bar run-app app.foo系统属性不会   适用于您的应用程序。这是因为bootRun在您的   build.gradle配置系统属性。为了使这项工作,你   可以简单地将所有System.properties附加到build.gradle中的bootRun   喜欢:

bootRun{
   systemProperties System.properties // Please note not to use '=', because this will > override all configured systemProperties. This will append them.
}

并在脚本中用于获取任何自定义属性:

ext {
    fooBar = bootRun.systemProperties['foo.bar']
    println "fooBar: ${fooBar}"
}

此外,您可以基于前缀传递一组有限的属性:

bootRun{
    systemProperties System.properties.inject([:]){acc,item-> item.key.startsWith('foo.')?acc << [(item.key.substring('foo.'.length())):item.value]:acc }
}

然后获取不带前缀的属性:

ext {
    fooBar = bootRun.systemProperties['bar']
    println "fooBar: ${fooBar}"
}

您可以在bootRun部分中使用传递的属性:

systemProperties System.properties.inject([:]){acc,item-> item.key.startsWith('foo.')?acc << [(item.key):item.value]:acc }

将具有所有以'foo'开头并带有后缀的属性:

bootRun.systemProperties['foo.bar']