Maven POM 文件:关于元素和部分排序的任何规则?
Maven POM file: any rule on ordering of elements and sections?
关于 pom.xml
Maven 文件:
- 是否有任何特定规则适用于声明部分的排序?
- 它对构建有什么重要性或影响吗?
- 我应该遵守任何官方惯例吗?
虽然在大多数情况下,一个部分是否在另一部分之前声明是无关紧要的,但是 可读性 在选择奇怪的布局时确实会受到影响(例如最后的 Maven 坐标) .
但这不是最重要的一点,因为是的,某些元素的排序会影响您的构建。
plugin
声明的顺序
plugin
部分在 build
/plugins
部分中的顺序可能很重要。由于 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。也就是说,在这种情况下,顺序很重要,因为它可能会影响建造。
dependency
声明的顺序
此外,dependencies
部分中 dependency
声明的顺序也可能会影响您向 Dependency Mediation 的构建,也就是说,如果与传递冲突,第一个声明的依赖项将获胜依赖。因此,排序在某些情况下很重要。
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.
作为一条规则:首先声明您在代码中直接引用的依赖项(即,作为 import
语句)。
module
声明顺序
虽然在大多数情况下不相关,因为(之前)其他重要规则适用,Maven 也将尊重 modules
部分中的 module
元素在 multi 期间的声明顺序-模块构建作为最后的决策点。 reactor mechanism 实际上会:
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)
注: 此处加粗。
标准布局
最后但同样重要的是,虽然pom.xml
文件的其他部分的顺序并不重要,但好习惯是遵循官方Maven recommendations:
The team has voted during the end of June 2008 to follow a specific POM convention to ordering POM elements.
作为简化版本,请遵循以下声明顺序:
<project>
<modelVersion/>
<parent/>
<groupId/>
<artifactId/>
<version/>
<packaging/>
<properties/>
<dependencyManagement/>
<dependencies/>
<build/>
<reporting/>
<profiles/>
</project>
最后一点,sortpom-maven-plugin
也可用于自动应用此标准排序,只需在相关 pom.xml
文件上调用以下内容:
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.5.0:sort \
-Dsort.keepBlankLines -Dsort.predefinedSortOrder=recommended_2008_06
另请注意,上述异常不会被处理,但 documented 由插件作为特殊情况处理,在这种情况下确实订购可能会影响您的构建。
进一步阅读:
关于 pom.xml
Maven 文件:
- 是否有任何特定规则适用于声明部分的排序?
- 它对构建有什么重要性或影响吗?
- 我应该遵守任何官方惯例吗?
虽然在大多数情况下,一个部分是否在另一部分之前声明是无关紧要的,但是 可读性 在选择奇怪的布局时确实会受到影响(例如最后的 Maven 坐标) .
但这不是最重要的一点,因为是的,某些元素的排序会影响您的构建。
plugin
声明的顺序
plugin
部分在 build
/plugins
部分中的顺序可能很重要。由于 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。也就是说,在这种情况下,顺序很重要,因为它可能会影响建造。
dependency
声明的顺序
此外,dependencies
部分中 dependency
声明的顺序也可能会影响您向 Dependency Mediation 的构建,也就是说,如果与传递冲突,第一个声明的依赖项将获胜依赖。因此,排序在某些情况下很重要。
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.
作为一条规则:首先声明您在代码中直接引用的依赖项(即,作为 import
语句)。
module
声明顺序
虽然在大多数情况下不相关,因为(之前)其他重要规则适用,Maven 也将尊重 modules
部分中的 module
元素在 multi 期间的声明顺序-模块构建作为最后的决策点。 reactor mechanism 实际上会:
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)
注: 此处加粗。
标准布局
最后但同样重要的是,虽然pom.xml
文件的其他部分的顺序并不重要,但好习惯是遵循官方Maven recommendations:
The team has voted during the end of June 2008 to follow a specific POM convention to ordering POM elements.
作为简化版本,请遵循以下声明顺序:
<project>
<modelVersion/>
<parent/>
<groupId/>
<artifactId/>
<version/>
<packaging/>
<properties/>
<dependencyManagement/>
<dependencies/>
<build/>
<reporting/>
<profiles/>
</project>
最后一点,sortpom-maven-plugin
也可用于自动应用此标准排序,只需在相关 pom.xml
文件上调用以下内容:
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.5.0:sort \
-Dsort.keepBlankLines -Dsort.predefinedSortOrder=recommended_2008_06
另请注意,上述异常不会被处理,但 documented 由插件作为特殊情况处理,在这种情况下确实订购可能会影响您的构建。
进一步阅读: