Incrementing Maven Project Version with Jenkins/Git

EngineerBetter_DJ picture EngineerBetter_DJ · Mar 21, 2012 · Viewed 14.3k times · Source

I'm using Jenkins to build Maven Java projects and deploy them to a Nexus repository. I also use Git, although I'm more used to Subversion so my Git knowledge is limited.

I'd like Jenkins/Maven to:

  1. Merge feature branch into integration branch
  2. Build merged code, run unit tests
  3. If they pass, increment Maven version number
  4. Push merged code to origin's integration branch
  5. Deploy artifact to Nexus repository

I gather the Git merging can be achieved thus: http://twasink.net/2011/09/20/git-feature-branches-and-jenkins-or-how-i-learned-to-stop-worrying-about-broken-builds/

I've also read a lot about the maven-release-plugin.

I'm not sure how exactly to achieve the above results. If I hardcode SCM details into each project's POM, then won't the maven-release-plugin be acting only on that repository rather than Jenkins' local one?

If I use a solution of having Jenkins pass a environment variable to Maven to specify the version number, then I'd expect to have local version resolution issues in my IDE.

Answer

Łukasz Rżanek picture Łukasz Rżanek · May 5, 2012

OK, this a quite a difficult to do. But it's doable. Let me explain the idea step by step.

  1. First we need to merge the two branches together. This is quite easy to do with Jenkins Git plugin, as it have this feature built in: Merge before build option is available in Advanced section in your project configuration.

  2. So we got the code merged between those two branches, we can move to building the project - business as usual, Maven style Jenkins project.

  3. Now we need to increment the version of the project and it's modules, commit the merged branch and push the artifacts to the repository. And here we have two options to go with:

    1. Use maven-release-plugin to the work. Yes, it's possible, as no one said that you will need to put the URLs in your pom, as it can be provided in two different ways. The plugin will use one provided with command line switch -DconnectionUrl=. And if none will be provided, it will check release.properties file to get it from there. Last reserve is to go to the pom.xml ang get ${project.scm.connection} variable. So you can easily use the plugin to do all of the dirty work without any hassles and introduce that property using different techniques provided by Jenkins.
    2. You can use a combination of maven-versions-plugin(specificaly versions:set and versions:commit goals) with maven-scm-plugin to do the tagging and committing the changes.

    In both cases you can use the Post Steps with Run only if build succeeds option and invoke the top-level Maven target as usual. For all of the settings mentioned above or described by the particular plugin there is an option to provide them easily in a text field. For uploading your artifacts to the Nexus repo I would go with Jenkins build in feature to Deploy artifacts to Maven repository, that is availble within your project configuration. This will require you to provide a repository configuration in either the POM definition, or as property.

Hope that covers all of the questions you have. If you need me to be more specific in any range of the answer, don't hesitate to ask.