I realize this was answered long ago, but I have come up with a new twist on the answer that some might find useful.
I have the replacer plugin change a string in a Java file on each MAVEN build. So, if the pom.xml
ever updates the pom project version number, it is automatically reflected in a java source file.
TO TELL MAVEN ABOUT THE replace
goal:
<build>
...
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
com.google.code.maven-replacer-plugin
</groupId>
<artifactId>
replacer
</artifactId>
<versionRange>
[1.5.3,)
</versionRange>
<goals>
<goal>replace</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
<runOnIncremental>true</runOnIncremental>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
...
</build>
TO TELL MAVEN TO FILL IN THE CURRENT NAME AND VERSION:
(If you prefer, put the full sub path in the basedir
and remove the **/
from the included filename.)
<build>
...
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>replace-version-number</id>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>${project.basedir}/src/main/java</basedir>
<includes>
<include>**/Constants.java</include>
</includes>
<preserveDir>true</preserveDir>
<replacements>
<replacement>
<token>mavenProjectName = ".*"</token>
<value>mavenProjectName = "${project.name}"</value>
</replacement>
<replacement>
<token>mavenProjectVersion = ".*"</token>
<value>mavenProjectVersion = "${project.version}"</value>
</replacement>
</replacements>
</configuration>
</plugin>
....
</plugins>
...
</build>
IN Constants.java:
...
private static final String mavenProjectName = "";
private static final String mavenProjectVersion = "";
...
The values get filled with the POM's values in the generate_sources
phase, before the compile phase.
The wildcard .*
in the replacer's token means the value is updated even if you check an updated copy into source control.