简体   繁体   中英

In groovy how can I get value of a key from build.gradle extra properties

I have a Gradle project, And I want to get a specific value from the build.gradle file using groovy (it's a jenkins job ).

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

I want to get the "TestVersion" version (which is 4.0.0-SNAPSHOT) in a "gradle" way if it is possible and not by a groovy script that just parses a json file. Is there a way to do it or should I just start parsing it as a regular json?

Gradle files are written in Groovy syntax, not json. You cannot (and should not) easily parse any gradle file.

You can move the TestVersion value to a normal property file in your project, and read that file from both your Jenkins script and your build.gradle , using a standard properties file parser.

Another alternative is to create a gradle task to help with whatever you want to do in Jenkins, and call gradle from within Jenkins.

As a similar example, you can use the "gradle properties" command to read properties from gradle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM