The Flatten Maven Plugin – DZone Java

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 plugins Section:

<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: licensesAnd dependenciesAnd 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 -DupdatePomFile=false.

By default, the plugin only maintains the . extension licensesAnd dependenciesAnd 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 situation Describe
oss For open source software projects that want to keep everything FlattenDescriptor Optional POM elements except for repositories And pluginRepositories.
ossrh keep everything FlattenDescriptor Optional POM elements required to host the OSS repository.
bom Like ossrh But it additionally maintains dependencyManagement And properties. In particular, it will keep the Dependency Management intact without resolving the original effects and dependencies defined in the import scope. This is useful if your POM is a file Hassan And you don’t want to publish it as is (to remove key variables, solve version issues, etc.).
defaults The default mode that removes all FlattenDescriptor Optional POM elements except for repositories.
clean removes everything FlattenDescriptor Optional POM elements.
fatjar removes everything FlattenDescriptor Optional POM items and all dependencies.
resolveCiFriendliesOnly It only solves revision of variables, sha1 , and change list. He keeps everything else. See Maven CI Friendly for more details.

conclusion

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.

moving forward:

.

Leave a Comment