gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:41:25 26 4
gpt4 key购买 nike

关于pom.xml Maven文件:

  • 是否有任何特定规则适用于声明部分的排序?
  • 它对构建有任何重要性或影响吗?
  • 我要遵守任何官方惯例吗?
  • 最佳答案

    尽管在大多数情况下,一个部分是否在另一个之前声明是无关紧要的,可读性选择奇怪的布局时确实会受到影响(例如,最后的 Maven 坐标)。

    但这不是最重要的一点,因为 是的 , 某些元素的排序 会影响您的构建 .

    订购 plugin声明
    plugin的顺序build 中的部分/plugins部分可能很重要。由于 Maven 3.0.3 ( MNG-2258 ),附加到同一 Maven 阶段的不同插件执行将在其 中调用申报顺序pom.xml文件,在通过 default bindings 附加的任何执行之后.也就是说,订购很重要在这种情况下,因为它可能会影响构建的行为。

    订购 dependency声明

    此外,还可订购 dependency dependencies 中的声明部分可能会影响您对 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 也会尊重 module 的声明顺序。 modules 中的元素多模块构建期间的部分作为最后一个决策点。 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由插件作为特殊情况,其中确实排序可能会影响您的构建。

    进一步阅读 :
  • Official Maven doc: Plugin Bindings
  • Official Maven doc: Dependency Mediation
  • Official Maven doc: Maven Code Style And Code Conventions
  • 关于Maven POM 文件 : any rule on ordering of elements and sections?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854044/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com