I have two versions in my projects.
These should of course be the same, but sometimes I forget to change one or the other.
Is there a good way to define this version only once and have it used by both maven and code during runtime?
http://www.sonatype.com/books/mvnref-book/reference/resource-filtering-sect-description.html
Have a look at this, you should have a properties file, which has a certain property which refers to the version number. And using filtering you can set the version same both in maven artifact and application.
When you package your application using Maven, Maven will generate a directory META-INF/maven/<groupId>/<artifactId>/pom.properties. That file contains the version of your project according to the POM, and you could load that file from the JAR file on runtime in order to read your version number.
Only thing is, this file does only exist in a packaged project (be it a JAR, a WAR or an EAR). You might want to write some fallback mechanism for when you are running the code from your workbench.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With