Pages

Wednesday, January 9, 2013

Versioning a webapp using SVN revision as build number with maven

Versioning a webapp is useful for keeping track of bugs fixed, upgrades, libraries versions modifications, etc.

In this article I will show how to use SVN revision number as part of the version number of a webapp using the buildnumber-maven-plugin. Using that number makes things a lot easier in case you have to downgrade your application, reproduce a bug in a particular previous version, etc.

Here I will use the version format Major.Minor.SVNRevision. Major is a number manually assigned, Minor is a sequential number that is incremented automatically after every build, and SVNRevision is the revision number obtained from the SVN repository, you can easily modify this format to suit your needs. The whole number is generated by maven in the build process, ie when you execute mvn install on your project.

For this to work first of all you have to configure the scm section in your pom.xml:


<scm>
    <connection>scm:svn:https://svnserver/projectname/trunk</connection>
    <developerConnection>scm:svn:https://svnserver/projectname/trunk</developerConnection>
    <tag>HEAD</tag>
    <url>https://svnserver/projectname/trunk</url>
</scm>


Then, you must add the buildnumber plugin to the plugins section inside <build> tag

<plugin>

<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>generate-buildnumber</id>
<phase>prepare-package</phase>
<goals>
<goal>create</goal>
</goals>
<configuration>
<doUpdate>true</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations> 
<useLastCommittedRevision>true</useLastCommittedRevision>
<buildNumberPropertyName>buildRevision</buildNumberPropertyName>
</configuration>
</execution>
<execution>
<id>generate-sequential</id>
<phase>prepare-package</phase>
<goals>
<goal>create</goal>
</goals>
<configuration>
<buildNumberPropertiesFileLocation>src/main/resources/buildNumber.properties</buildNumberPropertiesFileLocation>
<format>{0,number,1'.'00}</format>
<items>
   <item>buildNumber0</item>
</items>
<buildNumberPropertyName>buildSequential</buildNumberPropertyName>
</configuration>
</execution>
</executions>
</plugin>

The plugin has two executions configured.

The first one is responsible for getting the svn revision number from the repository, the 
create goal is executed upon the prepare-package phase, that phase is reached automatically when you issue a build command on maven (install, package, release); the <buildNumberPropertyName> tag value creates a variable name that hold the revision number, later we'll see how it is used.

The second execution creates the sequential part of the version number, it references the
buildNumber.properties file that hold the last number used, which is automatically incremented by 1 after every build. The item tag indicates the name of the variable inside the properties file. The format of the generated number is 1.XX, where XX is the number obtained from the properties file, the plugin uses java.text.MessageFormat to format the number using {0,number,1'.'00} format in this case. Note that we use <buildNumberPropertyName> again to store the sequential number in a new variable.


Using the generated version number

Now that we already have the pom.xml configured to generate the version number, it's time to use it. I'll show you here how to put it in your application's home page but you can reference it elsewhere.

First, I'll declare two variables in the pom.xml that references the two variables created by the buildnumber plugin:

<properties>
    ...
    <webapp.version>${buildSequential}</webapp.version>
    <webapp.revision>${buildRevision}</webapp.revision>
</properties>

Then I'll add another plugin to my pom, maven-war-plugin that is responsible for the packaging and generation of the web application archive.

<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
<includes>
<include>index.html</include>
</includes>
</resource>
</webResources>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildRevision}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>

In the configuration of the plugin I indicate to filter index.html in order to replace variables included there by values computed from this pom. You can find more information about filtering in the plugin's site. I also added a default MANIFEST.MF file indicating the generated revision number as Implementation-Build value. That file is included inside the META-INF folder of the webapp.

Then, inside the index.html you can reference the variables declared earlier in the pom:

<html>
  <head>
    <title>Webapp's Name ${webapp.version} (Revision: ${webapp.revision})</title>
    ...
  </head>
  ...
</html>

That's all, when your application is packaged and deployed, you'll see in your home page the title indicating the version number, for example: Webapp's Name 1.23 (Revision: 4567)

Hope it helps. Comment if you have any problem using this approach.

34 comments:

  1. Nice Explanation

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks for the best blog. it was very useful for me.keep sharing such ideas in the future as well.
    embroidery digitizing

    ReplyDelete
  4. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read.
    embroidery digitizing

    ReplyDelete
  5. . I am realy very happy to visit your blog. Now I am found which I actually want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post.
    This post is Awesome about Embroidery Digitizing
    Thanks for sharing.

    ReplyDelete
  6. I am really amazed at the ideal approach to viewing your subject. Where do you collect this information? I want to write great writing like you do. 야설


    ReplyDelete
  7. Your article has proven useful to me. It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content. 야설


    ReplyDelete

  8. Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out. 국산야동


    ReplyDelete
  9. Thank you for sharing this information. I read your blog and I can't stop my self to read your full blog. Again Thanks and 온라인카지노 Best of luck to your next Blog in future.

    ReplyDelete
  10. I will recommend your website to everyone. You have스포츠토토 a very good gloss. Write more high-quality articles. I support you.

    ReplyDelete
  11. Great blog article. Really looking forward to온라인카지노 read more.

    ReplyDelete
  12. The creating on your site is excellent as well as I would love to create it like you. What did you consider composing? Taking a look at your article, I sympathize with you a great deal. Look, individuals sympathize with you a great deal. I wish to leave a message on the internet site like you. That was really considerate. Thanks. 카지노사이트

    ReplyDelete
  13. What you presented was well researched and well worded in order to get your stand on this across to all your readers 에볼루션카지노

    ReplyDelete
  14. hiya great weblog! Does strolling a weblog which include this take a incredible deal of work? I've genuinely no understanding in programming but i used to be hoping to start my very own weblog in the close to future. Anyhow, should you've got any ideas or techniques for brand spanking new weblog proprietors please proportion. I recognise this is off subject matter however i just had to ask. Thank you i’m long gone to convey my little brother, that he ought to also visit see this website on everyday foundation to take up to date from newest news update. You certainly make it appear so easy with your presentation however i discover this count to be genuinely some thing which i think i would in no way recognize. It seems too complicated and very large for me. I’m looking forward in your next publish, i’ll attempt to get the grasp of it! 먹튀검증

    ReplyDelete
  15. most of whatever you say is astonishingly valid and it makes me contemplate the cause why i hadn’t checked out this with this light before. This specific piece simply did turn the light on for me personally as some distance as this trouble is going. Though there's virtually one issue i'm no longer too cozy with so even as i make the effort to reconcile that with the center topic of your function, permit me have a look at simply what all the rest of the readers have to say. Properly finished. I’m definitely digging the template/subject matter of this weblog. It’s easy, but effective. A number of times it’s very tough to get that “best stability” between usability and appearance. I need to say which you’ve executed a super process with this. Additionally, the blog hundreds very speedy for me on chrome. Exquisite weblog ! Notable work ! This is the kind of records which are alleged to be shared across the net. 메이저사이트

    ReplyDelete
  16. Your web site is excellent. I was really touched by this short article. I'm so delighted to locate the write-up I have actually been trying to find for a very long time. I covet the number of individuals sympathize with your writing. You find out a great deal of understanding from my writing. 바카라사이트

    ReplyDelete