For one of my current projects I am changing the build process for our application from Ant to Maven. I think this is becoming a quite common change nowadays (at least it's the third project in a rather short period I am involved with this change). And although Maven has its characteristics, I am still convinced it is a good thing to do.
Now at one point in our build process we had an Ant task to fill in a version number and build number into a property file of our application. This property file is used by the application at runtime to check if the version of the application matches the version of our database objects and the version is used in the generated SOAP response messages, which makes it easier when communicating with the customer in case of problems.
But lets get back to the issue: we had this functionality in an Ant task and I wanted to do this same thing in Maven. And it appeared that this was a remarkably easy thing to do! I ran into this Maven plugin which exactly did what I wanted.
I'll show you an example how to use it here:

  1. Install Maven2
  2. This step is obvious, I would say. For this example there are no special settings necessary in the settings.xml. Just make sure you have access to the internet if you are behind a proxy/firewalll.

  3. Create a Maven2 project
  4. I created an example project with the command :
    mvn archetype:create -DgroupId=net.pascalalma -DartifactId=VersionTest

  5. Add the project to a SVN repository
  6. To obtain the buildnumber from Subversion we have to put the project under version control. This can be done by any SVN client (I used my favourite one: TortoiseSVN)

  7. Add 'scm' tag to the pom.xml
  8. To give Maven access to the SVN repository you need to configure the SVN connection in the pom, like this:

    XML:
    1. <scm>
    2.     <connection>scm:svn:http://myserver/myproject/svn/trunk/</connection>
    3.     <developerConnection>scm:svn:http://myserver/myproject/svn/trunk/</developerConnection>
    4.  </scm>

  9. Add properties file to project
  10. Next step is to add the file that will contain the version and buildnumber. I created a file called 'version.properties' in the directory 'src/main/resources'.
    In the property file I put:

    XML:
    1. my.project.version = ${project.version}.${buildNumber}

Let's see now how to add the version and buildnr to the properties file. Add to the project's pom.xml the following:

XML:
  1. <resources>
  2.   <resource>
  3.     <filtering>true</filtering>
  4.     <directory>src/main/resources/</directory>
  5.     <includes>
  6.       <include>**/*</include>
  7.     </includes>
  8.   </resource>
  9. </resources>
  10. <plugins>
  11.   <plugin>
  12.     <groupId>org.codehaus.mojo</groupId>
  13.     <artifactId>buildnumber-maven-plugin</artifactId>
  14.     <version>1.0-beta-1</version>
  15.     <executions>
  16.       <execution>
  17.         <phase>validate</phase>
  18.         <goals>
  19.           <goal>create</goal>
  20.         </goals>
  21.       </execution>
  22.     </executions>
  23.     <configuration>
  24.         <doCheck>false</doCheck>
  25.         <doUpdate>true</doUpdate>
  26.     </configuration>
  27.   </plugin>
  28. </plugins>

When you now build your project with 'mvn clean package' for instance, the placeholders will be replaced by the corresponding values!
And here's another example which adds the buildNumber to the manifest file of your project (in case of having a jar file as deliverable). Put the following in your project's pom.xml:

XML:
  1. <plugin>
  2.   <artifactId>maven-jar-plugin</artifactId>
  3.   <version>2.0</version>
  4.   <configuration>
  5.     <archive>
  6.       <manifestEntries>
  7.         <implementation-Build>${buildNumber}</implementation-Build>
  8.       </manifestEntries>
  9.     </archive>
  10.   </configuration>
  11. </plugin>

Now when you run 'mvn package' it will add the buildnumber to the manifest file. More info can be found here (including an example to extract the buildNumber in your Java code from the manifest file).
You can also find lots more info (about formatting the buildnumber) at the homepage of the plugin here.
These kind of things only make me more enthousiastic about using Maven.