6

I want to add a version numbering to my Java application (in this case a Vaadin Portlet) which features the Version number in the help view.

The thing is that that version number is the one defined in the my Maven's POM file (for example 1.1.5-SNAPSHOT) and that is the one we are going to change (at release for example).

Any idea on how to get it out of the POM and into (for example, a String) in Java? (If this is at all possible)

Thanks

Fverswijver
  • 459
  • 1
  • 12
  • 30

4 Answers4

8

Use a filtered properties file containing the line

version=${project.version}

and load this properties file from the classpath in your Java program.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Who sets properties file and can I get the value of a project.version from a Java method without using a properties file etc. Can you check my question? http://stackoverflow.com/questions/14760638/how-to-get-maven-project-version-from-java-class-as-like-at-pom – kamaci Feb 07 '13 at 22:48
4

There are many solutions to this problem

But I would defintely prefer the first, as accessing MANIFEST.MF is quite easy in Java

Riduidel
  • 22,052
  • 14
  • 85
  • 185
2

The version in the POM is available as a property "project.version". Expressed as ${project.version}

So you should be able to use it with filtering resources:

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

The nicest approach would probably be to filter (and replace) a properties file or a deployment descriptor.

Dennis Thrysøe
  • 1,791
  • 4
  • 19
  • 31
2

Or you could read the maven pom.properties from classpath. The pom.properties are located at:
META-INF/maven/groupid/artifactId/pom.properties

sdorra
  • 2,382
  • 18
  • 16
  • This would cause a dependency on the Maven build. I'd rather use the MANIFEST.MF file which contains the same information defined a standard manner. – jjmontes Oct 03 '11 at 15:49