Displaying the version in a Maven-based project
0 Comments Published June 21st, 2008 in Java Tags: .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.
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « May | Jul » | |||||
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | ||||||
Archives
Categories
- Android (3)
- Apple (27)
- Books (7)
- Eclipse (14)
- Errors (4)
- Firefox (7)
- Git (2)
- Hardware (17)
- Horror Code (8)
- Internet (21)
- Java (102)
- JavaScript (9)
- Life, universe and everything (46)
- Lifehacks (25)
- Linux (51)
- Opinions (26)
- OSX (6)
- Python (1)
- Software (31)
- Speeches and Conferences (8)
- Unix (4)
- Web (23)
- Windows (19)
Tag Cloud
Android apple architecture Bash configuration CSS Development Düsseldorf Eclipse Git Google Hardware hdr How-To Java JAXB job junit Karmic Linux lion MacBook music Open Source Opinion oracle OSX patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu unix video Web Workaround XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links




















No Responses to “Displaying the version in a Maven-based project”
Please Wait
Leave a Reply