It's sometime useful to access the version information from inside the program, for example to show that to the user, to manage upgrades or to identify the running version when a user reports a bug, etc.

When you build using maven, inside your artifacts it is included the pom file and the a property file that contains the version, the groupId and the artifactId.
When you run your application in development, that information is not included in the artifact, because the artifact should not exist: running a build to obtain the final artifact for every test run would be quite stupid.
Running the application from your IDE, during normal test runs, the application should not fail because of the missing property in the nonexistent jar file.

The following lines of code is what I use to display the version number:

 1public void displayTitle() {
 2    String version = "DEVELOPMENT";
 3    InputStream pomPropsStream = getResourceStream(
 4        "META-INF/maven/"
 5            + "your.group.id" + "/"
 6            + "your.artifact.id" 
 7            + "/pom.properties");
 8    if (pomPropsStream != null) {
 9        Properties pomProperties = new Properties();
10        pomProperties.load(pomPropsStream);
11        version = pomProperties.getProperty("version");
12    }
13    System.out.print("MyAppName version " + version
14        + " (c) 2007 BlahBlah Inc.\n\n");
15}
16
17public InputStream getResourceStream(String path) {
18    ClassLoader loader = Thread.currentThread().getContextClassLoader();
19    if (loader != null)
20        return loader.getResourceAsStream(path);
21    return getClass().getClassLoader().getResourceAsStream(path);
22}

So, when you run the application from the built artifact you will see the actual version number of the released artifact. If you run from your IDE, you will see that the version is "DEVELOPMENT" or something else you like.

Also, you may want to be able to run your standalone application outside your IDE but without building the artifacts. What I usually do is adding to my pom following sections:

 1<project>
 2    ...
 3    <properties>
 4        <mainClass>it.newinstance.myApp.Main</mainClass>
 5    </properties>
 6    <build>
 7        <plugins>
 8            ...
 9            <plugin>
10                <!-- use "mvn compile antrun:run" to run the application -->
11                <groupId>org.apache.maven.plugins</groupId>
12                <artifactId>maven-antrun-plugin</artifactId>
13                <configuration>
14                    <tasks>
15                        <java classname="${mainClass}" classpathref="maven.runtime.classpath" fork="true" dir="target" />
16                    </tasks>
17                </configuration>
18            </plugin>
19        </plugins>
20    </build>
21</project>

And you run your application submitting the command

mvn antrun:run

or if you've not compiled yet you do a

mvn compile antrun:run

Notice that also in this case, to run the application, you DON'T need to build the artifacts... it is just compiling and launching.


No Responses to “Displaying the version in a Maven-based project”  

  1. No Comments

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



Calendar

June 2008
M T W T F S S
« May   Jul »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Follow me

twitter flickr LinkedIn feed

Subscribe by email

Enter your email address:

Archives


Categories

Tag Cloud


Listening