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 
About these ads

0 Responses to “Application Version.java Maven”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 49 other followers

%d bloggers like this: