简体   繁体   English

Maven - 根据项目属性激活配置文件

[英]Maven - activate profile based on project property

Hi I am trying to achieve something like this: 嗨,我想实现这样的事情:

In a parent pom, I have 在父母家里,我有

<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. 因此,如果我在父pom下运行mvn -DCompile=mod1 install ,它只会将配置文件设置应用于模块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. 然后在父pom下运行mvn -DCompile=war install ,它只会将配置文件设置应用于那些要打包为战争而不是jar或pom的设置。

I tried but it didnt work as expected. 我试过但它没有按预期工作。 Did I miss anything? 我错过了什么吗? Please help. 请帮忙。

PS no need to suggest workarounds as I am only interested on this method. PS无需建议解决方法,因为我只对此方法感兴趣。 Simply answer it is not possible with reason if thats the case. 如果是这样的话,简单地回答它是不可能有理由的。 Thank you 谢谢

It won't work since... 它不会起作用......

To begin with: 首先:

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模块来处理事情。

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 有关激活属性的更多信息

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM