05
Feb
12

Application Version.java Maven


This page describes how to have Maven configure your application so that it can report the current application build version to the user.

This comes in handy when displaying help information for your application as well as allowing production support staff to know what version is actually running on the server.

Requirements

  1. Maven 2 or above
  2. Maven Java 5 or above

Create the project using Maven Archetype

First step is to create the job using a Maven archetype. Open up the command prompt and navigate to an empty directory.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
groupId: com.test
artifactId: appVersion

Answer the rest of the questions with defaults “Just hit the enter key”

Change to the project’s base directory.

cd appVersion

Create the resources folder

Edit the pom.xml file and add the following section towards the bottom of the page right before the closing project tag.

vi pom.xml

        <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
        </build>

mkdir -p src/main/resources/com/test

vi src/main/resources/com/test/default.properties

app.version=${pom.version}

vi src/main/java/com/test/Version.java

package com.test;

import java.util.ResourceBundle;

/**
 * Prints the application version. 
 */
public class Version {

	public static void main(String[] args) {
		String version = ResourceBundle.getBundle ("com.test.default").getString("app.version");
		System.out.println("Application Version: " + version);
	}

}

Compile and Test

mvn -q clean compile exec:java -Dexec.mainClass=com.test.Version 
Advertisements

0 Responses to “Application Version.java Maven”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 74 other followers

February 2012
S M T W T F S
« Jan   Apr »
 1234
567891011
12131415161718
19202122232425
26272829  

Blog Stats

  • 801,397 hits

%d bloggers like this: