Based on answer from @superole . This is a simplified version without the need to set additional properties. Only the project version is copied to Version.java.
Put Version.java in src/main/templates :
package thepackage; public final class Version { public static String VERSION="${project.version}"; }
Ask maven to replace tokens in Version.java
<resources> <resource> <directory>src/main/templates</directory> <includes> <include>*.java</include> </includes> <filtering>true</filtering> <targetPath>${project.build.directory}/generated-sources/java/thepackage</targetPath> </resource> </resources>
Ask maven to recognize generated-sources/java as a build path:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.8</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources/java/</source> </sources> </configuration> </execution> </executions> </plugin>
Finally, let Eclipse m2e
- know a new build path
- and not get into an infinite loop assembly.
The second point is achieved by disabling using the maven-resources-plugin during the incremental build of eclipse.
<pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <versionRange>[1.0,)</versionRange> <goals> <goal>parse-version</goal> <goal>add-source</goal> <goal>maven-version</goal> <goal>add-resource</goal> <goal>add-test-resource</goal> <goal>add-test-source</goal> </goals> </pluginExecutionFilter> <action> <execute> <runOnConfiguration>true</runOnConfiguration> <runOnIncremental>true</runOnIncremental> </execute> </action> </pluginExecution> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <versionRange>[1.0.0,)</versionRange> <goals> <goal>resources</goal> </goals> </pluginExecutionFilter> <action> <execute> <runOnConfiguration>true</runOnConfiguration> <runOnIncremental>false</runOnIncremental> </execute> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement>
thepackage should be replaced with your package: also adjust targetPath . It was easier for me to set the path in targetPath instead of having many subfolders in src/main/templates .
koppor Nov 27 '13 at 18:09 2013-11-27 18:09
source share