Jenkins and Maven Release plugin

OK, So here’s a quick tutorial on how to make a Jenkins and Maven Release plugin build that automatically creates a release build using Jenkins and we are assuming your code is in Git.

Step 1 is easy, get your code from Git. You just configure the Jenkins build as always.

Now, We need one additional plugin for Jenkins which is “Workspace Cleanup Plugin”. This allows us to enable
“Delete workspace before build starts” in Build Environment section.

Now the tricky part: Under Pre Steps you must add a new shell command:

git checkout master

This is because otherwise your git is checked out without a specific branch and maven release plugin will fail.

Build command

-B -Dusername=jenkins -Dpassword=JenkinsPasswordForGit release:prepare release:perform

Important notice – you can configure jenkins user to have automatic access to git in other ways also, but I decided to keep this info in the build.

Once you start releasing your project with Jenkins, you quickly discover that manually managing the version numbers inside your own multi-module project will become a hassle. Simple solution would be to configure Jenkins to upgrade your own dependencies to the latest releases (requires you to limit versions plugin to your own packages):

-B versions:use-latest-releases scm:checkin -Dmessage=$BUILD_NUMBER -Dusername=jenkins -Dpassword=JenkinsPasswordForGit release:prepare release:perform

Now we might want to limit versions plugin to our own com.codeyouneed package since having it upgrade ALL dependencies all the time is really bad.

<build>
    <pluginManagement>
        <plugins>
	        <plugin>
	            <groupId>org.codehaus.mojo</groupId>
	            <artifactId>versions-maven-plugin</artifactId>
	            <version>2.1</version>
	            <configuration>
	                <includesList>com.codeyouneed</includesList>
	                <generateBackupPoms>false</generateBackupPoms>
	                <allowSnapshots>true</allowSnapshots>
	            </configuration>
	        </plugin>
		</plugins>
	</pluginManagement>
</build>

Now that’s pretty much it. That git checkout master command took me a while to figure out.

Of course, for maven release plugin to work you actually need to add some stuff to your pom.xml also:


	<properties>
		<scm.url>scm:git:https://git.codeyouneed.com/git/test/repo</scm.url>
	</properties>
	
	<distributionManagement>
		<repository>
			<id>codeyouneed</id>
			<url>https://codeyouneed/artifactory/libs-releases-local/</url>
		</repository>
	</distributionManagement>

	<scm>
		<url>${scm.url}</url>
		<connection>${scm.url}</connection>
		<developerConnection>${scm.url}</developerConnection>
		<tag>HEAD</tag>
	</scm>

Important notice if you are dealing with multi-module maven projects – Submodules have to leave out their name from scm.url since release plugin will add the name by itself into the end of the URL.

    <scm.url>scm:git:https://git.codeyouneed.com/git/test[/submodulename]</scm.url>

So you should leave out the “/submodulename”.

Special thanks to a blog post that lead me to the right path: http://labs.bsb.com/2014/02/better-maven-releases-with-jenkins/

Jenkins Logo

About Reigo Reinmets

Enterprise Software consultant with 9 years of experience in enterprise software world who’s working on various projects that are mostly based on Liferay. Most blog posts here are real-life findings and issues we’ve encountered while working on projects and Java tutorials on Liferay.

Leave a Reply

Your email address will not be published. Required fields are marked *