I come from ANT where you can invoke a .java file to obtain the version number through executing some method. I would like to do something similar in my new pom.xml to automatically obtain the correct version number. I need the app to know about its own version and at the same time Maven to know about the version got make the right builds. Holding the version infomormation doubled is no option.
Or can Maven create a new .java file during/before building?
I can image quite a few ways to achive this, but is there a best practise way to do this in Maven project?
When building a JAR, Maven writes the
pom.xmland apom.propertiesfile intoMETA-INF/maven/[group ID]/[artifact ID]directory. You can then read the version out of one of these in your application.Alternatively, you can explicitly write a new version.properties file by using resource filtering.
If you put the following in your POM:
and then put a
version.propertiestemplate into the src/main/resources directory that looks like:Maven will substitute in the version read out of the POM, and your application then just needs to read the
version.propertiesresource and query theversionkey.