What is the simplest way to retrieve version number from maven’s pom.xml in code, i.e., programatically?
What is the simplest way to retrieve version number from maven’s pom.xml in code,
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming you’re using Java, you can:
Create a
.propertiesfile in (most commonly) yoursrc/main/resourcesdirectory (but in step 4 you could tell it to look elsewhere).Set the value of some property in your
.propertiesfile using the standard Maven property for project version:In your Java code, load the value from the properties file as a resource from the classpath (google for copious examples of how to do this, but here’s an example for starters).
In Maven, enable resource filtering. This will cause Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property. You can find some info here but you mostly just do this in your pom:
You can also get to other standard properties like
project.name,project.description, or even arbitrary properties you put in your pom<properties>, etc. Resource filtering, combined with Maven profiles, can give you variable build behavior at build time. When you specify a profile at runtime with-PmyProfile, that can enable properties that then can show up in your build.