Generate a Version.java file in Maven

I don’t think this is the good way to solve this kind of issue.

A better way is to put the version information in a properties file that will be read by your Java program:

Your properties file will contain the following line:

myapp.version=${project.version}

Then, in your pom.xml, indicate that the file will be filtered by Maven :

<resources>
    <resource>
        <directory>the/directory/that/contains/your/properties/file</directory>
        <filtering>true</filtering>
    </resource>
</resources>

When Maven will build your application, it will replace all ${...} by their value. By default, ${project.version} defines the version of the pom.xml (i.e. the value of the <version> tag).

Then, in your Java code, you will just need to load the properties file and retrieve the myApp.version property value.

Note that you can use the Build Number plugin to set something more “complex” than just your current version (for example if you want to put the build time in your property).

Leave a Comment