Thursday, 26 January 2012

Adding Version Information to your JAR’s Manifest

One of the handy things about using Maven is that, by default, the names of the artifacts it creates include the current version number from the POM's version tag. It doesn’t matter what type of artifact it is, whether it’s a JAR, WAR or EAR you generally end up with something like this:


And this is all usually fine when you’re dealing with a project that’s built solely using Maven. It does, however, become a little more inconvenient if you’re developing JAR files with Maven, but your WAR file is developed using Ant. In this case, when you have to check files in to the /WEB-INF/libs directory yourself (perish the thought) and your JAR file changes often, you end up constantly adding and deleting files from your version control system. From experience, it turns out to be more convenient, in terms of file management, if you remove the version number from the JAR file name and use something like this:


However, there will always be the need to know the version of your JAR that has been included in your webapp’s WAR file and that’s where the maven-jar-plugin comes in handy. This allows you to write custom information into your JAR file’s manifest as shown by the POM file snippet below.


The POM file extract above contains two handy features. The first:


...removes the version number from the JAR file name by specifying the finalname tag. In this case the finalname has been set to the project.artifactId. This means that if your artifact tag looks something like this...


...then your jar file will be called:


For more on Maven’s default properties take a look at this blog from last January.

The second task accomplished by the POM extract above is to use the maven-jar-plugin to add group, artifact and version information to the jar’s manifest file. The lines that are responsible for this are:


You can obviously add any information you want to a jar’s manifest file, but in this scenario, adding the version number was extremely useful when it came to support and tracing problems.

1 comment:

sandeep@Java said...

I just came from DZone.
Good article man.

Java Tutorial