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
- Maven 2 or above
- 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
0 Responses to “Application Version.java Maven”