Michal Kordas
Michal Kordas

Reputation: 10925

Release multimodule project using one version and tag in Gradle

I have multimodule project in Gradle, where parent build.gradle is just an aggregator of subprojects, without any sources.

Structure:

parent with version in gradle.properties
 - child1 inheriting version
 - child2 inheriting version

Both children produce JARs with artifacts, while parent produces empty JAR which I don't want to upload anywhere at all.

Now I want to release such project. There should be one commit with version update and one tag in Git. However, all subprojects should be build and uploaded to a repository. How can I achieve it?

I've tried gradle-release plugin, but I struggle to configure it properly. I get either of the following:

Upvotes: 4

Views: 2209

Answers (1)

lischen_
lischen_

Reputation: 101

I had the same problem and ended up writing the necessary steps in Gradle with the help of the gradle-svn-tools plugin (https://github.com/martoe/gradle-svntools-plugin). Note that I'm no Groovy expert, so I guess a few things can be done more easily :)

My build.gradle in the parent project:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'at.bxm.gradleplugins:gradle-svntools-plugin:1.6.2'
    }
}

allprojects {
    apply plugin: 'at.bxm.svntools'

    //subversion login
    svntools {
        username = svnUsername
        password = credentials.svnpwd
    }
}

//task to write the version parameter given via command line into the  "gradle.properties" files.
// Call with: gradle writeProjectVersion -PnewVersion=1.0.1-SNAPSHOT
task('writeProjectVersion') << {
    if (project.hasProperty('newVersion')) {
        //set version in gradle settings
        project.version = project.newVersion
        project.subprojects?.each {it.version = project.version }
        //set version in all the gradle.properties files
        new File(".").traverse(type : groovy.io.FileType.FILES, nameFilter: 'gradle.properties') {
            project.ant.replaceregexp(file: it, byline: true) {
                key = 'version'
                version = project.version
                regexp(pattern: "^(\\s*)$key((\\s*[=|:]\\s*)|(\\s+)).+\$")
                substitution(expression: "\\1$key\\2$version")
            }
        }
        println 'Successfully changed project version in gradle.properties to \'' + project.version + '\''
    } else {
        println 'No parameter \'newVersion\' provided, so not changing the project version.'
    }
}


//task to commit the gradle.properties changes into SVN - also needs the new version as parameter for the commit message
// Call with: gradle svnCommit -PnewVersion=1.0.1-SNAPSHOT
task('svnCommit', type: at.bxm.gradleplugins.svntools.tasks.SvnCommit) {
    if (project.hasProperty('newVersion')) {
        def files = []
        new File(".").traverse(type : groovy.io.FileType.FILES, nameFilter: 'gradle.properties') { files << it } //appends all matching files to the list
        source = files
        commitMessage = "[Jenkins Release Build] Changing project version to '" + project.newVersion + "'"
    } else {
        println 'No parameter \'newVersion\' provided, so SVN commit was not executed.'
    }
}

//task to tag the current head - also needs the new version as parameter for the commit message
// Call with: gradle svnTag -PnewVersion=1.0.1-SNAPSHOT
task('svnTag', type: at.bxm.gradleplugins.svntools.tasks.SvnTag) {
    if (project.hasProperty('newVersion')) {
        tagName = "$project.newVersion"
        localChanges = true
        commitMessage = "[Jenkins Release Build] Tagging Release version $project.newVersion"
    } else {
        println 'No parameter \'newVersion\' provided, so SVN tagging was not executed.'
    }
}

I use Jenkins for execution and run the following shell script:

# execute gradle tests
gradle test
# set version in gradle.properties and tag the current code (gradle.properties changes are commited in the tag)
gradle writeProjectVersion -PnewVersion=${releaseVersion}
gradle svnTag -PnewVersion=${releaseVersion}
# build the artifacts and upload them to Nexus without running the test cases again
gradle clean build uploadArchives -x test
# set version in gradle.properties and commit the files
gradle writeProjectVersion -PnewVersion=${newSnapshotVersion}
gradle svnCommit -PnewVersion=${newSnapshotVersion}

Hope this helps.

Upvotes: 1

Related Questions