The two main approaches for enforcing maven 3 seem to be:
<prerequisites>
element.The best approach to uses seems to depend on a few different factors. This question is to help people decide which approach makes the most sense for them.
Question: What types of project structures are best suited to maven-enforcer-plugin and what types of project structure are best suited to pom prerequisites.
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>enforce-maven-3</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.0.5</version>
</requireMavenVersion>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<project>
...
<prerequisites>
<maven>3.0.5</maven>
</prerequisites>
...
</project>
The Enforcer plugin provides goals to control certain environmental constraints such as Maven version, JDK version and OS family along with many more built-in rules and user created rules.
<artifactId>maven-enforcer-plugin</artifactId> <version>3.1. 0</version>
Build plugins They execute during the build process and should be configured in the <build/> element of pom.
This rule requires that dependency version numbers converge. If a project has two dependencies, A and B, both depending on the same artifact, C, this rule will fail the build if A depends on a different version of C than the version of C depended on by B.
Plugin Configuration and Goals. Maven Enforcer has two goals: enforcer:enforce and enforcer:display-info. The enforce goal runs during a project build to execute rules specified in the configuration, while the display-info goal shows current information about the built-in rules that are present in the project's pom.xml. Let's define...
The Maven Enforcer plugin has many other rules to promote project quality and consistency irrespective of the development environment. Also, the plugin has a command to display info about some currently configured rules: 5. Custom Rules So far, we've been exploring the built-in rules of the plugin.
The keyword enforce gives a subtle suggestion of the existence of rules to abide by. This is how the Maven Enforcer plugin works. We configure it with some rules that are to be enforced during the build phase of the project.
The requireMavenVersion and requireJavaVersion rules enable a project-wide lock-in of required Maven and Java versions, respectively. This will help eliminate the disparity that might arise from using different versions of Maven and JDK in development environments. Let's update the rules section of the plugin configuration:
Prerequisites are deprecated, and you should use the enforcer plugin. MNG-5297 requests a documentation update to clarify this.
The maven-enforcer-plugin
FAQ explains the difference:
Why can't I just use the prerequisites tag in the pom?
The prerequisites tag was designed to be used by tools like plugins. It will work for regular projects, but it isn't inherited to their children. If it is set in a parent reactor, then Maven will do the check. However if one of the children are built, the check is not performed. The enforcer plugin is designed to allow centralized control over the build environment from a single "super-pom", and to allow greater flexibility in version specification by supporting ranges.
In addition, note that prerequisites do not work with Maven 3.
The Maven pom prerequisites
approach may be more appropriate for smaller, simpler projects. This approach will be lighter weight as it will not require the maven-enforcer-plugin
to be downloaded.
The maven-enforcer-plugin
approach may be more suitable if you have children projects that you wish to inherit the maven 3 requirement from the parent. pom prerequisites
is not inherited by children projects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With