A member of the Apache Maven committee recently wrote about their plans for Maven 5. I consider one of the following most important changes:
In short, we need to distinguish between two types of POM:
- the building boomstored in the project source control, which uses the v5 schema for build time, and requires a new Maven version to be able to use the new features associated with the new schema.
- the consumer boomwhich has been deployed to Maven Central in the good old v4 layout, so every previous or future builder can continue to consume pre-built artifacts as normal for their dependencies.
It’s an important binary that has long escaped me:
- POM consumers need some data, for exampleUsers who have the project in their dependencies list
- Binary options creator(s) require other data
There are additional concerns. For example, variables make sense for project developers to take advantage of dry. For consumers, it’s an extra layer of quirk that makes POM more difficult to understand.
On Reddit, user pmarschall mentioned that he was already separating concerns in the current Maven version with the help of the Maven Flatten plugin. It piqued my interest, and I wanted to try it. For that, I used the Spring Pet Clinic project – commit a7439c7.
The use is very simple. Just add the following snippet in a file
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>1.2.5</version> <configuration> </configuration> <executions> <execution> <id>flatten</id> <phase>process-resources</phase> <goals> <goal>flatten</goal> </goals> </execution> <execution> <id>flatten.clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin>
Now, if you implement a veteran
process-resources stage, the plugin will create a shortened version of the POM, with the name
.flattened-pom.xml. Compared to the raw POM, the flat POM has only three sections in addition to the coordinates:
repositories. In addition, Maven has solved all variants. If you play a file
install stage and then check your local Maven repository, you will notice that the POM matches the flat POM, not the main repository. If you want to create the flat POM but not replace the parent element, use
By default, the plugin only maintains the . extension
repositories Sections. You can configure which partitions you keep and which you don’t via the POM. For example, remove the plugin
name, but you can easily keep it if needed. Just add the appropriate configuration:
<configuration> <pomElements> <name>keep</name> </pomElements> </configuration>
The above method gives you the most flexibility. However, plug-in developers have already thought about which configuration packages make sense and offer them out of the box. Here’s an excerpt from the documentation describing them:
The Maven Flatten plugin separates build and consumer POMs. You don’t need to wait for Maven 5 to be released. It’s a freebie, so if you’re a library provider, you should probably consider using it.