Generate a Version.java file in Maven Generate a Version.java file in Maven java java

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).


You can also use maven-replacer-plugin if you feel ant is a little bit ugly:The pom entry might be:

<project>  ...  <properties>    <version.template.file>src/main/java/com/stackoverflowVersion.java.template</version.template.file><version.file>src/main/java/com/stackoverflow/Version.java</version.file>  </properties>  ...  <build>    <plugins>      <plugin>        <groupId>com.google.code.maven-replacer-plugin</groupId>            <artifactId>maven-replacer-plugin</artifactId>            <version>1.4.0</version>            <executions>                                <execution>                    <phase>process-sources</phase>                    <goals>                        <goal>replace</goal>                    </goals>                </execution>            </executions>            <configuration>                <file>${version.template.file}</file>                <outputFile>${version.file}</outputFile>                <replacements>                    <replacement>                        <token>@buildnumber@</token>                        <value>${svn.revision}</value>                    </replacement>                    <replacement>                        <token>@buildtime@</token>                        <value>${maven.build.timestamp}</value>                    </replacement>                    <replacement>                        <token>@pomversion@</token>                        <value>${project.version}</value>                    </replacement>                </replacements>                                    </configuration>      </plugin>    </plugins>  </build>  ...</project>

The Version.java.template might be:

package com.stackoverflow;public final class Version {    public static final String build_number="@buildnumber@";    public static final String build_time="@buildtime@";    public static final String pomversion="@pomversion@";}


This is an old question, but there is another solution that does a great job this perfectly (in the Maven sense): Templating Maven Plugin.

Using this plugin results in the processed Java file being put into the target/generated-sources folder, as you would expect. And it adds the folder under generated-sources to the build path. You will no longer check-in the processed file by mistake.

How to use

First put the following under src/main/java-templates/com/foo/bar/Version.java:

package com.foo.bar;public final class Version {    public static final String VERSION = "${project.version}";}

Then add the following to your POM:

<build>    <plugins>    ...        <plugin>            <groupId>org.codehaus.mojo</groupId>            <artifactId>templating-maven-plugin</artifactId>            <version>1.0.0</version>            <executions>                <execution>                    <id>filtering-java-templates</id>                    <goals>                        <goal>filter-sources</goal>                    </goals>                </execution>            </executions>        </plugin>    ...    </plugins></build>

The folder target/generated-sources/java-templates is added to the build path by Maven.