Maven POM file: any rule on ordering of elements and sections?

你。 提交于 2019-12-04 06:17:44

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.


Order of plugin declarations

The 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.


Order of dependency declarations

Additionally, 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).


Order of module declaration

Although 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.


Standard layout

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!