Concerning the pom.xml
Maven file:
- Is there any specific rule to apply to the ordering of declared sections?
- Does it have any importance or impact on the build?
- Shall I follow any official convention?
Concerning the pom.xml
Maven file:
Although in most of the cases it is irrevant whether one section is declared before another, readeabilty could be indeed impacted when choosing a weird layout (e.g. Maven coordinates at the end).
But that's not the most important point, because yes, ordering of certain elements can impact your build.
plugin
declarationsThe order of plugin
sections wihtin the build
/plugins
section may be important. Since Maven 3.0.3 (MNG-2258
), different plugin executions attached to the same Maven phase will be invoked in their order of declaration in the pom.xml
file, after any execution attached via default bindings. That is, ordering is important in this case, since it may affect the behavior of the build.
dependency
declarationsAdditionally, also order of dependency
declarations within the dependencies
section may affect your build towards Dependency Mediation, that is, the first declared dependency wins in case of conflict against a transitive dependency. So, once again, ordering is important in certain cases.
Note that if two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.
As a rule of thump: declare first the dependencies you directly reference in your code (that is, as import
statements).
module
declarationAlthough not relevant in most of the case, because (the) other important rules applies before, Maven will also respect the order of declaration of module
element within the modules
section during a multi-module build as last decision point. The reactor mechanism will in fact:
The following relationships are honoured when sorting projects:
- a project dependency on another module in the build
- a plugin declaration where the plugin is another modules in the build
- a plugin dependency on another module in the build
- a build extension declaration on another module in the build
- the order declared in the element (if no other rule applies)
Note: bold added here.
Last but not least, although ordering is not important for other sections of the pom.xml
file, good habit is to follow the official Maven recommendations:
The team has voted during the end of June 2008 to follow a specific POM convention to ordering POM elements.
As a simplified version, follow this order of declaration:
<project>
<modelVersion/>
<parent/>
<groupId/>
<artifactId/>
<version/>
<packaging/>
<properties/>
<dependencyManagement/>
<dependencies/>
<build/>
<reporting/>
<profiles/>
</project>
As a final note, the sortpom-maven-plugin
can also be used to automatically apply this standard ordering, simply invoking the following on the concerned pom.xml
file:
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.5.0:sort \
-Dsort.keepBlankLines -Dsort.predefinedSortOrder=recommended_2008_06
Also note, the exceptions above are not handled, but documented by the plugin as special cases where indeed ordering may affect your build.
For further reading:
© 2022 - 2024 — McMap. All rights reserved.