【问题标题】:In groovy how can I get value of a key from build.gradle extra properties在常规中,我如何从 build.gradle 额外属性中获取键的值
【发布时间】:2019-06-02 13:34:30
【问题描述】:

我有一个 Gradle 项目, 我想使用 groovy 从 build.gradle 文件中获取特定值 (这是詹金斯的工作)。

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/'
        }
    }
}

如果可能,我想以“gradle”方式获取 "TestVersion" 版本(即 4.0.0-SNAPSHOT),而不是通过仅解析 json 文件的常规脚本. 有没有办法做到这一点,还是我应该开始将其解析为常规 json?

【问题讨论】:

    标签: gradle groovy


    【解决方案1】:

    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 中读取属性。

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      相关资源
      最近更新 更多