java 在 pom.xml 和 settings.xml 中覆盖 Maven 属性的顺序是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10381005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:47:27  来源:igfitidea点击:

What is the order of overwrite Maven properties in pom.xml and settings.xml?

javamaven

提问by Ralph

today I observed that the maven proprties defined in settings.xmloverwrite the one in pom.xml.

今天我观察到定义的 maven 属性settings.xml覆盖了pom.xml.

Because it is only a observation, I am not sure if this is totally correct. So I am looked for the reference or some article but I find no concrete statement about the behaviour of overwriting properties defined with the same name in settings.xmland pom.xml.

因为这只是一个观察,我不确定这是否完全正确。所以我找了参考或一些文章,但我觉得会覆盖在相同的名称定义的属性的行为没有具体的说法settings.xmlpom.xml

Maybe someone can provide a link to the reference part (that I may overlooked) or the a reliable article/blog?

也许有人可以提供参考部分(我可能会忽略)或可靠文章/博客的链接?

采纳答案by khmarbaise

Based on the documentationin my opinion it is clear which one has precedence over the other (excerpt of the docs):

根据我认为的文档,很明显哪个优先于另一个(文档摘录):

If a profile is active from settings, its values will override any equivalently ID'd profiles in a POM or profiles.xml file.

如果配置文件在设置中处于活动状态,其值将覆盖 POM 或 profiles.xml 文件中任何等效的 ID 配置文件。

The profiles.xml does not exist anymore in Maven 3 only in Maven 2.2.1 it is supported but shouldn't be used.

profile.xml 在 Maven 3 中不再存在,仅在 Maven 2.2.1 中它被支持但不应该被使用。

回答by Alexander Volkov

I think properties with equal names are overridden in next sequence (from highest to lowest context):

我认为具有相同名称的属性在下一个序列中被覆盖(从最高到最低的上下文):

  • Global
  • Profile descriptor
  • Per project
  • Per user
  • 全球的
  • 配置文件描述符
  • 每个项目
  • 每个用户

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Hence, pom.xmlproperties (per project) are overridden by settings.xml(per user) properties with equal names.

因此,pom.xml属性(每个项目)被settings.xml(每个用户)具有相同名称的属性覆盖。

回答by yuranos

Just tested it on my maven 3.3.9.

刚刚在我的 Maven 3.3.9 上对其进行了测试。

First of all, properties are always defined in profiles, if we are talking about settings.xml. Properties from global settings XML have the highest priority and will override both user settings.xmland a specific pom.xml, UNLESSthere is a name collision between profiles.

首先,如果我们谈论的是 settings.xml,属性总是在配置文件中定义的。全局设置 XML 中的属性具有最高优先级,并且将覆盖用户settings.xml和特定的pom.xml除非配置文件之间存在名称冲突。

If it's the case, that is there is a profile with the same idin global, user settings, as well as pom.xml, user/local settings.xml is the boss. Even if a property in pom.xmldoesn't sit inside a profile, user/local settings.xmlproperty from an active profile with the same name will beat it. Incidentally, global settings.xmlwill also beat pom.xml property without a profile.

如果是这样,那就是在全局,用户设置以及pom.xml中都有一个具有相同id配置文件,用户/本地设置.xml是老板。即使属性 inpom.xml不在配置文件中,settings.xml来自具有相同名称的活动配置文件的用户/本地属性也会击败它。顺便说一句, globalsettings.xml也将在没有配置文件的情况下击败 pom.xml 属性。

As a side note:For <repositories>and <mirrors>the logic is slightly different:

作为旁注:对于<repositories><mirrors>逻辑略有不同:

  • User/local settings have higher priority than global for <mirrors>.
  • However, for <repositories>: pom - highest, then - local, then - global.
  • 用户/本地设置比全局设置具有更高的优先级<mirrors>
  • 但是,对于<repositories>:pom - 最高,然后 - 本地,然后 - 全局。