سؤال

I saw this question asked multiple times, however without satisfying answer: let's presume you have maven project producing some jar (java desktop app). How can one define version number in pom.xml, which would be automatically incremented (or even manually, does not matter) when appropriate (e.g. with every build) but with the possibility to load this version into the application? The goal is to display for the user the version of the app he is currently using.

هل كانت مفيدة؟

المحلول

There are in particular four options you can go for:

  1. Using the pom.properties file which is created by default by Maven.
  2. Using the information which is provided by the MANIFST.MF file. There are several ways to get those information.
  3. Create a property which is filtered during the build process and will be read by your application.
  4. Using a generated class which contains the appropriate informations.

The first option can be handled by a Java class like this:

public class TheVersionClass
{
    ..

    public TheVersionClass()
    {
        InputStream resourceAsStream =
          this.getClass().getResourceAsStream(
            "/META-INF/maven/com.soebes.examples/version-examples-i/pom.properties"
          );
        this.prop = new Properties();
        try
        {
            this.prop.load( resourceAsStream );
        }
        ...
    }
}

The second option is to use the MANIFEST.MF file

public class TheVersionClass {
    public TheVersionClass() {
        System.out.println( "  Implementation Title:" + this.getClass().getPackage().getImplementationTitle() );
        System.out.println( " Implementation Vendor:" + this.getClass().getPackage().getImplementationVendor() );
        System.out.println( "Implementation Version:" + this.getClass().getPackage().getImplementationVersion() );
        System.out.println( "    Specification Tile:" + this.getClass().getPackage().getSpecificationTitle() );
        System.out.println( "  Specification Vendor:" + this.getClass().getPackage().getSpecificationVendor() );
        System.out.println( " Specification Version:" + this.getClass().getPackage().getSpecificationVersion() );
    }
}

Unfortunately these informations are usually not be put into the MANIFEST.MF file so you have to change your configuration.

The third option is to create a file which is filtered as a resourced during the build process and the forth option is to use templating-maven-plugin to create appropriate classes. All the above can be looked into the github project.

And of course you can enhance any of the examples to your need to use buildnumber-maven-plugin to add information from your version control system into your MANIFEST.MF file by using the following snippet which should be located into your root of your modules or much better into a company pom file which add this to be executed for every build:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
      <revisionOnScmFailure>UNKNOWN</revisionOnScmFailure>
      <getRevisionOnlyOnce>true</getRevisionOnlyOnce>
      <providerImplementations>
        <svn>javasvn</svn>
      </providerImplementations>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>create</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

I wouldn't change the version if you have just a new build. It might be usefull to add the buildnumber from Jenkins or whatever CI solution you are using into the MANIFEST.MF file, but i would use the version of Maven which in case of a release will be changed from 1.0-SNAPSHOT into 1.0.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top