Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Lives in the UK. Likes blogging, cycling and eating lemon drizzle cake. Roger is a DZone MVB and is not an employee of DZone and has posted 72 posts at DZone. You can read more from them at their website. View Full User Profile

Adding Version Information to your JAR’s Manifest

02.06.2012
Email
Views: 2888
  • submit to reddit

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:

    my_module-1.2.3-RELEASE.jar

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:

    my_module.jar

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.

<build>
   <finalName>${project.artifactId}</finalName>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>2.3.2</version>
         <configuration>
            <archive>
               <index>true</index>
               <manifestSections>
                  <manifestSection>
                     <name>${project.name}</name>
                     <manifestEntries>
                        <mode>development</mode>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                     </manifestEntries>
                  </manifestSection>
               </manifestSections>
            </archive>
         </configuration>
      </plugin>
</build>

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

    <finalName>${project.artifactId}</finalName>

...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...

 <artifactId>my_module</artifactId>

...then your jar file will be called:

    my_module.jar

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:

    <groupId>${project.groupId}</groupId>
    <artifactId>${project.artifactId}</artifactId>
    <version>${project.version}</version>

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.

 

From http://www.captaindebug.com/2012/01/adding-version-information-to-your-jars.html

Tags:
Published at DZone with permission of Roger Hughes, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Christian Schli... replied on Mon, 2012/02/06 - 4:43am

The maven-jar-plugin already places pom.xml and pom.properties into META-INF/maven/groupId/artifactId. This is much better because you can now merge multiple JAR into one without loosing meta data, e.g. by using the maven-assembly-plugin.

Roger Hughes replied on Tue, 2012/02/07 - 9:52am in response to: IllegalException


Christian
Thanks for the comment. Yes, you're right, the POM.xml and pom.properties files are added in to the JAR file and, to us developers, this is really quite useful. However, when dealing with support staff who don't know about maven I find it easier to say something like "Could you check the Manifest file" because it's usually something that thay already know about and understand - hence the blog.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.