Lets take a look at a simple project
1: <project>
3: <modelVersion>4.0.0</modelVersion>
4: <packaging>pom</packaging>
5: <name>Three Stooges</name>
6: <groupId>oracle.blogger</groupId>
7: <artifactId>stooges</artifactId>
8: <version>1.0.0-SNAPSHOT</version>
9: <properties>
10: <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11: </properties>
12: <modules>
13: <module>larry</module>
14: <module>curley</module>
15: <module>moe</module>
16: </modules>
17: </project>
1: <project>
3: <modelVersion>4.0.0</modelVersion>
4: <packaging>jar</packaging>
5: <name>Larry</name>
6: <artifactId>larry</artifactId>
7: <parent>
8: <groupId>oracle.blogger</groupId>
9: <artifactId>stooges</artifactId>
10: <version>1.0.0-SNAPSHOT</version>
11: </parent>
12: </project>
In our larger project, we had over 20 of these to change at once when we branched for release
Instead, we introduced a revision property in the parent pom, which was then used to be the version in all the pom's. Here's the modified parent pom with the revision in place.
1: <project>
3: <modelVersion>4.0.0</modelVersion>
4: <packaging>pom</packaging>
5: <name>Three Stooges</name>
6: <groupId>oracle.blogger</groupId>
7: <artifactId>stooges</artifactId>
8: <version>${revision}</version>
9: <properties>
10: <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11: <flatten.version>1.0.1</flatten.version>
12: <!-- Build Revision -->
13: <revision>1.0.1-SNAPSHOT</revision>
14: </properties>
15: <modules>
16: <module>larry</module>
17: <module>curley</module>
18: <module>moe</module>
19: </modules>
and in each of the children, that change propagates like this.
1: <project>
2: <modelVersion>4.0.0</modelVersion>
3: <packaging>jar</packaging>
4: <name>Larry</name>
5: <artifactId>larry</artifactId>
6: <parent>
7: <groupId>oracle.blogger</groupId>
8: <artifactId>stooges</artifactId>
9: <version>${revision}</version>
10: </parent>
11: </project>
Now, for most things, this all works and we can see that in the build.
But, when we deploy these builds to artifactory, we need to flatten the poms with the versions so that they can be used for versioning. We can use the Maven Flatten Plugin from mojoHaus to help with this. What this does to the pom is:
Add it to your parent pom pluginManagement section.
1: <build>
2: <pluginManagement>
3: <plugins>
4: <plugin>
5: <groupId>org.codehaus.mojo</groupId>
6: <artifactId>flatten-maven-plugin</artifactId>
7: <version>${flatten.version}</version>
8: <configuration>
9: <updatePomFile>true</updatePomFile>
10: </configuration>
11: <executions>
12: <execution>
13: <id>flatten</id>
14: <phase>process-resources</phase>
15: <goals>
16: <goal>flatten</goal>
17: </goals>
18: </execution>
19: <execution>
20: <id>flatten.clean</id>
21: <phase>clean</phase>
22: <goals>
23: <goal>clean</goal>
24: </goals>
25: </execution>
26: </executions>
27: </plugin>
28: <plugin>
29: <groupId>org.eclipse.m2e</groupId>
30: <artifactId>lifecycle-mapping</artifactId>
31: <version>${lifecycle-mapping.version}</version>
32: <configuration>
33: <lifecycleMappingMetadata>
34: <pluginExecutions>
35: <pluginExecution>
36: <pluginExecutionFilter>
37: <groupId>org.codehaus.mojo</groupId>
38: <artifactId>flatten-maven-plugin</artifactId>
39: <versionRange>[0,)</versionRange>
40: <goals>
41: <goal>flatten</goal>
42: </goals>
43: </pluginExecutionFilter>
44: <action>
45: <ignore></ignore>
46: </action>
47: </pluginExecution>
48: </pluginExecutions>
49: </lifecycleMappingMetadata>
50: </configuration>
51: </plugin>
52: </plugins>
53: </pluginManagement>
54: <plugins>
55: <plugin>
56: <groupId>org.codehaus.mojo</groupId>
57: <artifactId>flatten-maven-plugin</artifactId>
58: </plugin>
59: </plugins>
60: </build>
This will run as part of the build like this
and will produce a flattened pom for you
without all the variables and so on as detailed above.
mvn clean will remove all created files afterwards to leave your original pom.xml. The pull request in the repo looked like this before merge.
View comments