我有一个Gradle项目,
我想使用groovy从build.gradle文件中获取特定值
(这是 Jenkins 的工作)。
build.gradle:
allprojects {
subprojects {
version = "1.0-SNAPSHOT"
if (project.hasProperty("TestVersion"))
version = project."TestVersion"
}
ext {
sourceCompatibility = 1.8
targetCompatibility = 1.8
springBootVersion = '2.1.0.RELEASE'
TestVersion = '4.0.0-SNAPSHOT'
}
repositories {
mavenLocal()
maven {
maven {
url 'http://repo1.maven.org/maven2/'
}
}
}
我想以一种“渐变”的方式来获取“TestVersion” 版本(即4.0.0-SNAPSHOT),而不是通过仅解析json文件的常规脚本来获取。
有没有办法做到这一点,还是我应该开始将其解析为常规json?
最佳答案
Gradle文件使用Groovy语法而不是json编写。您不能(也不应该)轻松地解析任何gradle文件。
您可以使用标准属性文件解析器将TestVersion值移至项目中的常规属性文件,并移至read that file from both your Jenkins script and your build.gradle。
另一种选择是创建gradle任务以帮助您在Jenkins中进行任何操作,然后在Jenkins中调用gradle。
作为类似的示例,您可以use the "gradle properties" command从gradle中读取属性。