我有一个Gradle 6.0.1
项目。build.gradle
(摘录(如下所示:
plugins {
id "application"
id "com.github.edeandrea.xjc-generation"
id "eclipse"
id "idea"
id "java"
id "org.springframework.boot"
}
...
dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:${property("spring-boot.version")}")
// ...more stuff here
}
// ...more stuff here
我正在settings.gradle
管理所有插件版本:
pluginManagement {
plugins {
id "application"
id "com.github.edeandrea.xjc-generation" version "1.0"
id "eclipse"
id "idea"
id "java"
id "org.springframework.boot" version "${spring-boot.version}"
}
}
rootProject.name = "spring-core"
。我通常将工件版本放在gradle.properties
:
#
# Dependency Versions
oracle.version = 18.3.0.0
spring-boot.version = 2.2.1.RELEASE
#
# Gradle Settings
org.gradle.configureondemand = false
org.gradle.daemon = false
#
# System Settings
systemProp.file.encoding = UTF-8
systemProp.sun.jnu.encoding = UTF-8
现在的问题是我无法像在build.gradle
中那样读取settings.gradle
(从gradle.properties
(中的点属性 - 我已经尝试使用${property("spring-boot.version")}
。
有什么办法可以做到这一点吗?我可以轻松地将密钥更改为类似 springBootVersion
的东西并且它可以工作,但我想知道是否有一种方法可以以我目前拥有的方式:spring-boot.version
.
使用getProperty("spring-boot.version")
带有其他变体的简单 Gradle 项目
task test{
doLast {
//success
println project.property('aaa.bbb.ccc')
println project.'aaa.bbb.ccc'
println getProperty('aaa.bbb.ccc')
//failure: Could not get unknown property 'aaa.bbb.ccc' for task ':test' of type org.gradle.api.DefaultTask
println property('aaa.bbb.ccc')
}
}
gradle.properties
aaa.bbb.ccc=12345
property('aaa.bbb.ccc')
失败,因为它尝试获取当前对象(任务(的属性,但aaa.bbb.ccc
为项目定义的
然而project.property('aaa.bbb.ccc')
成功了,因为它应该
project.'aaa.bbb.ccc'
与时髦的project.getProperty('aaa.bbb.ccc')
相同
project.getProperty('aaa.bbb.ccc')
工作是因为时髦的基本对象GroovyObject
(IHMO(
和没有前缀的getProperty(name)
实际上位于org.gradle.groovy.scripts.BasicScript中,并没有真正记录...
对我来说,@daggett的解决方案不起作用。但是我能够以这种方式从~/.gradle/gradle.properties
访问settings.gradle
内部定义的属性:
settings.ext.find('MY_PROPERTY')
为了提供一些上下文,我使用它来读取 Gradle 构建缓存节点远程服务器的凭据:
buildCache {
boolean isCiServer = System.getenv().containsKey("CI")
remote(HttpBuildCache) {
enabled = settings.ext.find('GRADLE_BUILD_CACHE_NODE_PWD') != null
url = 'https://myserver:443/cache/'
allowUntrustedServer = true
push = isCiServer
credentials {
username = settings.ext.find('GRADLE_BUILD_CACHE_NODE_USR')
password = settings.ext.find('GRADLE_BUILD_CACHE_NODE_PWD')
}
}
}
EDIT
另一种不需要使用ext
的解决方案:
username = settings.hasProperty('GRADLE_BUILD_CACHE_NODE_USR') ? settings.GRADLE_BUILD_CACHE_NODE_USR : null