Tuesday, March 1, 2011

Maven Release Plugin

I have recently started using a plugin for Maven that has been an incredible time saver, so I thought I would post a quick tutorial on what it is and how to use it. It is called the Maven Release Plugin, and when developing a java library it should be your new best friend.

Basically, this plugin automates a ton of the little manual steps that you have to go through every time you perform a release. It will:
  1. Increment the version number of the project in the pom
  2. Check the updated version number into svn
  3. Add a tag to the project in svn for the current release
  4. Compile, Test, Package
  5. Upload the packaged jar file to your local Maven repository
  6. Append “-SNAPSHOT” to the current version in the project pom for continued development
  7. Check the updated version number into svn

It makes a few assumptions in order to execute successfully:
  1. Before trying to release, all of your local changes must be checked in. It will only release what is current in svn.
  2. Your current version number, the first time you use this, must end with “-SNAPSHOT”
  3. You must have a local Maven repository set up

Once you have this all set up, then releasing a new version of your library becomes as easy as checking your regular code changes into svn and running a simple maven command. No more worrying about remembering to increment your version number, no more concern about not having things checked into svn. It mandates a lot of the best practices that one should be following anyway.

In order to get this set up, there are a handful of steps that must be followed:
  1.  You must have the “svn” command on your command line execution path. For Windows, I downloaded and installed SlikSvn, which automatically adds the svn command to your path (requires a restart after installation)
  2. Add the following block to your project’s pom.xml at the top level under the root <project> node:
    <scm> 
       <connection>scm:svn:https://your.svn.url/ProjRoot</connection> 
       <developerConnection>scm:svn:https://your.svn.url/ProjRoot</developerConnection> 
    </scm>
    
  3. Add the following block to your project’s pom.xml under <build><plugins>:
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-release-plugin</artifactId>
       <version>2.1</version>
    </plugin>
  4. If you are not already set up to deploy to your local Maven repository, you need to add the following block under <build><extensions>
    <extension>
       <groupId>org.apache.maven.wagon</groupId>
       <artifactId>wagon-webdav</artifactId>
       <version>1.0-beta-2</version>
    </extension>
  5. If you are not already set up to deploy to your local Maven repository, you need to add the following block under <distributionManagement>
    <repository>
       <id>internal</id>
       <name>Internal Release Repository</name>
       <url>dav:http://your.internal.repository</url>
    </repository>
In order to run the plugin, you just execute "mvn release:prepare release:perform", or from Eclipse you would do a Run As > Maven build... and put "release:prepare release:perform" in the Goals.

That's all there is to it. With a single command, you can now handle versioning, building, testing, and deploying of your java library.

No comments:

Post a Comment