Java Maven - 根据项目属性激活配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18889554/
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
Maven - activate profile based on project property
提问by user1589188
Hi I am trying to achieve something like this:
嗨,我正在努力实现这样的目标:
In a parent pom, I have
在父 pom 中,我有
<profile>
<activation>
<property>
<name>Compile</name>
<value>${project.artifactId}</value>
...
so that if I run mvn -DCompile=mod1 install
under the parent pom, it will only apply the profile settings to module 1, but not the others.
这样如果我mvn -DCompile=mod1 install
在父 pom 下运行,它只会将配置文件设置应用于模块 1,而不是其他模块。
Similarly, if I have
同样,如果我有
<profile>
<activation>
<property>
<name>Compile</name>
<value>${project.packaging}</value>
...
then running mvn -DCompile=war install
under the parent pom, it will only apply the profile settings to those to be packed as war but not jar nor pom.
然后mvn -DCompile=war install
在父 pom 下运行,它只会将配置文件设置应用于要打包为 war 而不是 jar 或 pom 的配置文件。
I tried but it didnt work as expected. Did I miss anything? Please help.
我试过了,但没有按预期工作。我错过了什么吗?请帮忙。
P.S. no need to suggest workarounds as I am only interested on this method. Simply answer it is not possible with reason if thats the case. Thank you
PS 不需要建议解决方法,因为我只对这种方法感兴趣。如果是这样的话,简单地回答这是不可能的。谢谢
采纳答案by Sander Verhagen
It won't work since...
它不会工作,因为...
To begin with:
首先:
- Profile activation works with system propertiesand not with Maven properties.
- 配置文件激活适用于系统属性,而不适用于 Maven 属性。
And why it would not work from the parent down to the children:
以及为什么它从父母到孩子都不起作用:
- The properties in the parent POM are expanded before your child POM even comes in the picture.
- 父 POM 中的属性在您的子 POM 出现之前就展开了。
Many have gone here before you and failed.
许多人在你之前来到这里并失败了。
One might wonder what you are really trying to do. Are you building different artifacts from the same source project? Perhaps you need a few more Maven modules to take care of things.
人们可能想知道你真正想要做什么。您是否从同一个源项目构建不同的工件?也许您需要更多的 Maven 模块来处理事情。
回答by Mite Mitreski
Yes you can activate the profile depending one or variety of parameters like env
variables.
是的,您可以根据一个或多个参数(如env
变量)激活配置文件。
<project>
...
<profiles>
<profile>
<id>development</id>
<activation>
<property>
<name>!environment.type</name>
</property>
</activation>
</profile>
</profiles>
</project>
If you are trying to have different packaging depending on X than you can use the assembly plugin and do your magic there http://maven.apache.org/plugins/maven-assembly-plugin/
如果您尝试根据 X 使用不同的包装,那么您可以使用程序集插件并在那里施展魔法 http://maven.apache.org/plugins/maven-assembly-plugin/
More on the activation property
有关激活属性的更多信息
回答by avianey
As of Maven 3.0, profiles in the POM can also be activated based on properties from active profiles from the settings.xml.
从 Maven 3.0 开始,POM 中的配置文件也可以根据 settings.xml 中活动配置文件的属性来激活。
You can check the sample given here :
您可以查看此处给出的示例:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
http://maven.apache.org/guides/introduction/introduction-to-profiles.html