One of the Apache Maven committers recently wrote about their plans for Maven 5. I consider the following one of the most significant changes:
In summary, we need to make a distinction between two POM types: the build POM, stored in the project source control, that uses v5 schema for build time, requiring a new Maven version able to use the new features associated with the new schema. The other one is the consumer POM, that is published to Maven Central in the good old v4 schema, so every past or future build tool can continue to consume pre-built artifacts as usual for their dependencies.
It’s an important dichotomy that escaped me for a long time:
- Consumers of the POM require some data, e.g., users who have the project in their dependencies list
- Binary(ies) builders require other data
There are additional concerns. For example, variables make sense for the project’s developers to leverage the DRY. For consumers, it’s an extra layer of indirection that makes understanding the POM harder.
On Reddit, user pmarschall mentioned they were already separating between the concerns in Maven’s current version with the help of the Maven Flatten plugin. It got me interested, and I wanted to try it. For that, I used the Spring Pet Clinic project – commit a7439c7.
Usage is very straightforward. Just add the following snippet in the
org.codehaus.mojo flatten-maven-plugin 1.2.5 flatten process-resources flatten flatten.clean clean clean
Now, if you execute the Maven
process-resources phase, the plugin will create a shortened version of the POM, named
.flattened-pom.xml. Compared to the initial POM, the flattened POM has only three sections in addition to the coordinates:
repositories. Additionally, Maven has resolved all variables. If you run the
install phase and then check your local Maven repository, you’ll notice the POM matches the flattened POM, not the main one. If you want to generate the flattened POM but not replace the main one, use
By default, the plugin keeps only the
repositories sections. You can configure which sections you keep and don’t via the POM. For example, the plugin removes
name, but you can keep it easily if needed. Just add the relevant configuration:
The above method gives you the most flexibility. However, developers of the plugins have already thought about which configuration bundles make sense and offer them out-of-the-box. Here’s an excerpt from the documentation that describes them:
||For Open-Source-Software projects that want to keep all
||The default mode that removes all
||Only resolves variables revision, sha1 and changelist. Keeps everything else. See Maven CI Friendly for further details.|
The Maven Flatten plugin separates between build and consumer POMs. You don’t need to wait until the release of Maven 5. It’s a freebie, so if you’re a library provider, you should probably consider using it.
To go further:
Originally published at A Java Geek on January 30th, 2022
The post The Flatten Maven plugin appeared first on foojay.