Emil Genov

Learn, Practice & Share

Friday, October 29, 2010

Tagging maven build with SVN version, and using this version inside application

To do this, you need to take following steps:
  1. Add build-number plugin to your project main POM
    ...
          <build>
                  ...
                  <plugins>
                      ...
                      <plugin>
                          <groupId>org.codehaus.mojo</groupId>
                          <artifactId>buildnumber-maven-plugin</artifactId>
                          <version>1.0-beta-3</version>
                          <executions>
                              <execution>
                                  <phase>validate</phase>
                                  <goals>
                                      <goal>create</goal>
                                  </goals>
                              </execution>
                          </executions>
                          <configuration>
                              <doCheck>false</doCheck>
                              <doUpdate>false</doUpdate>
                          </configuration>
                      </plugin>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-release-plugin</artifactId>
                          <version>2.0</version>
                      </plugin>
                      ...
                  </plugins>
              ...
          </build>
          ...
    
  2. Change your current jar plugin (or add one if such is missing):
    ...
                       <plugin>
                          <artifactId>maven-assembly-plugin</artifactId>
                          <version>2.2-beta-2</version>
                          <configuration>
                              <archive>
                                  ...
                                  <manifest>
                                      ...    
                                      <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                      ...
                                  </manifest>
                                  <manifestEntries>
                                      <Implementation-Version>${pom.version} (SVN version:${buildNumber})</Implementation-Version>
                                      <SVN-Version>${buildNumber}</SVN-Version>
                                  </manifestEntries>
                                  ...
                              </archive>
                              ...
                          </configuration>
                          <executions>
                              <execution>
                                  <goals>
                                      <goal>single</goal>
                                  </goals>
                                  <phase>package</phase>
                              </execution>
                          </executions>
                      </plugin>
                      ...
    
  3. To get version inside your code use:
    public String getVersion() {
        return <some class inside jar, that is created by maven>.class.getPackage().getImplementationVersion();
    }
    

  4. To get version from running program (server) using only command line:
    1. Create an JMX interface, with method getVersion() exposed, expose this interface in JMX on host XXX.YYY.ZZZ on port PPP
    2. Download cmdline-jmxclient utility jar from it's site
    3. Issue this:
      java -jar cmdline-jmxclient-0.10.3.jar a:a  XXX.YYY.ZZZ:PPP <name of your JMX bean> Version
      

0 comments:

Blog Archive