0

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?

Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91

1 Answers1

3

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.

tkruse
  • 10,222
  • 7
  • 53
  • 80