基于多个属性激活maven配置文件

Jan*_*yka 26 profile maven-2 activation

我正在为一个项目创建一个maven 2构建,我想出了配置文件,因为必须为不同的位置(比如柏林,巴黎,北极)和不同的环境(开发,生产)创建构建.这些是通过属性指定的.所以对于"北极""DEV"我这样做:

-Dlocation=NorthPole -Denvironment=DEV
Run Code Online (Sandbox Code Playgroud)

现在我想根据这两个属性来激活我的porfile,而不仅仅是一个.所以我试着跟随:

<profiles>
  <profile>
    <id>NOrth Pole DEV</id>
    <activation>
      <property>
        <name>location</name>
        <value>NorthPole</value>
      </property>
      <property>
        <name>environment</name>
        <value>DEV</value>
      </property>
    </activation>
    ... <!-- Set some North Pole DEV specific stuff -->
  </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

这不起作用,maven期望<property>那里看到最多一个元素.

请注意我还有另一个用于物业的用途,因此使它成为locationEnv有价值的单一属性NorthPole-DEV不是我想要的.

那么有什么办法或解决方法或其他任何方法如何根据属性组合激活配置文件?

Pét*_*rök 13

我担心你的问题没有很好的解决方案(除非我不知道有新的Maven功能).

理论上,您可以引入一个派生属性,其值从您列出的两个属性中连接起来.但是,问题是在pom中定义的属性之前评估配置文件,因此这样的派生属性不能用于激活配置文件:-(

我能想到的类似问题的最佳解决方法是显式激活配置文件,并将命令行参数的不同组合放入单独的批处理/脚本文件中,以使执行更简单并避免错误输入问题.


khm*_*ise 10

为什么不直接使用个人资料:

<profiles>
   <profile>
    <id>north-pole</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    ....
  </profile>
   <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    ....
  </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

现在,您可以通过命令行激活配置文件.

mvn -Pdev,north-pole ...
Run Code Online (Sandbox Code Playgroud)

  • 当然但如果我有配置文件NorthPole-DEV.xml和NorthPole-PROD,我怎么知道你的场景中使用哪一个?我需要根据位置和环境做出决定并采取行动 (2认同)
  • 这并没有解决问题,因为第二个属性会触发配置文件,即使第一个未设置(在我的情况下也很重要).-1对于典型的无用的Maven建议(例如,"以Maven方式做,并使一切变得愚蠢,臃肿和不可维护"). (2认同)

Gáb*_*ták 5

可能的解决方案

尝试这个扩展: https: //github.com/kpiwko/el-profile-activator-extension

这允许有这样的语法:

<profile>
    <id>NOrth Pole DEV</id>

    <activation>
        <property>
            <!-- mvel property name is obligatory -->
            <name>mvel</name>
            <value>isdef location &amp;&amp; location=="NorthPole" &amp;&amp; 
                   isdef environment &amp;&amp; environment=="DEV"</value>
        </property>
    </activation>
</profile>
Run Code Online (Sandbox Code Playgroud)

我自己没有尝试过,但似乎是一个不错的项目。

如何避免手动配置Maven

您需要将项目所需的两个 jar 放入 $MAVEN_HOME/lib/ext 中。不过,您可以自动配置它们。像这样:

测试简介:

<profile>
    <id>prepare-maven-extended-libs</id>
    <activation>
      <file>
        <missing>${maven.home}/lib/ext/el-profile-activator-extension.jar</missing>
      </file>
    </activation>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.redhat.jboss.maven</groupId>
                                    <artifactId>el-profile-activator-extension</artifactId>
                                    <version>1.0.0-SNAPSHOT</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${maven.home}/lib/ext</outputDirectory>
                                    <destFileName>el-profile-activator-extension.jar</destFileName>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>org.mvel</groupId>
                                    <artifactId>mvel2</artifactId>
                                    <version>2.1.3.Final</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${maven.home}/lib/ext</outputDirectory>
                                    <destFileName>mvel2.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/wars</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals><goal>execute</goal></goals>
                    </execution>
                </executions>
                <configuration>
                    <source>
                        fail("For profile activation we use an extension jar. It is now in your ${maven.home}/lib/ext folder. Please restart the build, and then it will be successful.")
                    </source>
                </configuration>
            </plugin>               
        </plugins>
    </build>
</profile>
Run Code Online (Sandbox Code Playgroud)