Java 覆盖来自 Maven 父 POM 的模块属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18641504/
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-08-12 09:46:49  来源:igfitidea点击:

Overriding module properties from parent POM in Maven

javamavenmaven-3pom.xmlparent-pom

提问by zacheusz

Is it possible to override module properties from parent pom without changing the module pom?

是否可以在不更改模块 pom 的情况下覆盖父 pom 的模块属性?

For example:

例如:

module pom (I can't change it at all):

模块 pom(我根本无法改变它):

<properties>
    <someProperty>some value to be replaced</properties>
</properties>

parent pom:

父pom:

<properties>
    <someProperty>strongValue</someProperty> <!-- some magic here -->
</properties>

effective module pom:

有效模块 pom:

<properties>
    <someProperty>strongValue</someProperty>
</properties>

If yes then how to achieve it?

如果是,那么如何实现它?

采纳答案by Robert Scholte

No, you can't. The idea is that if it shouldn't be possible to override a value, don't use a property. If you have no other option, you might want to force it with http://maven.apache.org/enforcer/enforcer-rules/requireProperty.htmlwhich will break the build if a property has a different value than expected.

不,你不能。这个想法是,如果不可能覆盖一个值,就不要使用属性。如果您没有其他选择,您可能希望使用http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html强制它,如果属性具有与预期不同的值,这将破坏构建。

回答by user944849

Only way I know to do this for sure is to define the property on the command line, e.g. mvn -DsomeProperty=strongValue <mvn goals/phases here>.

我知道要这样做的唯一方法是在命令行上定义属性,例如mvn -DsomeProperty=strongValue <mvn goals/phases here>.

You might experiment with profiles defined in the parent; I suspect that won't help.

您可以尝试在父级中定义的配置文件;我怀疑这不会有帮助。

回答by kanaparthikiran

A child POM can overwrite the value of a property defined in a parent pom. So it works by just putting a section in the child POM and set the values to desired values.

子 POM 可以覆盖父 POM 中定义的属性值。因此,它只需在子 POM 中放置一个部分并将值设置为所需的值即可。