Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Version number in java file

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?

like image 512
Franz Kafka Avatar asked Jan 12 '12 01:01

Franz Kafka


1 Answers

When building a JAR, Maven writes the pom.xml and a pom.properties file into META-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:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

and then put a version.properties template into the src/main/resources directory that looks like:

version = ${project.version}

Maven will substitute in the version read out of the POM, and your application then just needs to read the version.properties resource and query the version key.

like image 57
prunge Avatar answered Sep 28 '22 12:09

prunge