gradle.properties使用点表示法读取属性失败

时间:2016-04-04 15:55:35

标签: gradle properties

我从gradle.properties读取带有点符号的属性时收到错误。为什么呢?

如果我使用version代替build.version,一切正常。 提前谢谢。

失败案例

gradle.properties

build.version=2.0.1

的build.gradle

apply plugin: 'java'

task testProperties << {
    println "***************** TEST **********************"
    println build.version
    println "*********************************************"
}

EXECUTION

... $ gradle devTest
:devTest
***************** TEST **********************
:devTest FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '..../build.gradle' line: 50

* What went wrong:
Execution failed for task ':devTest'.
> Could not find property 'version' on task ':build'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or<br> --debug option to get more log output.

BUILD FAILED

SUCCES CASE

gradle.properties

version=2.0.1

的build.gradle

apply plugin: 'java'

task testProperties << {
    println "***************** TEST **********************"
    println version
    println "*********************************************"
}

EXECUTION

... $ gradle devTest

:devTest

***************** TEST **********************
2.0.1

*********************************************

BUILD SUCCESSFUL

3 个答案:

答案 0 :(得分:14)

You cannot use properties with a dot in this way because Groovy thinks it's a version property of the build instance. If you want a property to contain a dot then you should use it in the following way:

println rootProject.properties['build.version']

答案 1 :(得分:11)

访问属性的另一种方法是使用下面的语法

println "Version: ${project.'build.version'}"

答案 2 :(得分:1)

还有一个:

println property('build.version')
println "Version: ${property('build.version')}"

println ext['build.version']
println "Version: ${ext['build.version']}"
相关问题