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

时间:2011-03-24 10:00:46

标签: maven-2 profile activation

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

-Dlocation=NorthPole -Denvironment=DEV

现在我想基于这两个属性来激活我的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>

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

请注意我还对该属性有另一种用法,因此将其设为值locationEnv的单个属性NorthPole-DEV不是我想要的。

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

6 个答案:

答案 0 :(得分:13)

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

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

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

答案 1 :(得分:9)

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

<profiles>
   <profile>
    <id>north-pole</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    ....
  </profile>
   <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    ....
  </profile>
</profiles>

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

mvn -Pdev,north-pole ...

答案 2 :(得分:4)

可能的解决方案

试用此扩展程序: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>

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

如何避免手动配置Maven

您需要将项目所需的两个罐子放入$ MAVEN_HOME / lib / ext。但是,您可以自动配置它们。像这样:

  • 您可以添加$ {MAVEN_HOME / lib / ext / el-profile-activator-extension.jar文件的activated on absense个人资料
  • 此配置文件可以使用dependency plugin将maven从maven下载到初始阶段的$ MAVEN_HOME / lib / ext文件夹中
  • 然后你可以写出一条消息,即构建配置了maven文件夹,下一个构建将会成功。

经测试的个人资料:

<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>

答案 3 :(得分:1)

khmarbaise的答案对我来说似乎更优雅。 对于Jan的评论,您可以通过附加适当的内容来引用该文件 例如使用profile dev,North Pole激活你可以参考NorthPole-dev.xml $ {位置} - $ {ENV} .XML

我不得不发布另一条回复,因为我无法在其他人的回复中添加评论。 :(

答案 4 :(得分:0)

我相信你可以做这样的事情

<properties>
        <env>dev</env>
        <location>North Pole</location>
    </properties>

<profiles>
        <!-- dev North Profile -->
        <profile>
            <id>dev North Pole</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- qa North Profile -->
        <profile>
            <id>qa North Pole</id>
            <properties>
                         <env>qa</env>
                             <location>North Pole</location>
            </properties>
        </profile>

    </profiles>
<build>
do profile specific stuff here
</build>

当然,要激活配置文件,您可以在命令'-P = dev North Pole'中添加

答案 5 :(得分:-2)

经过详尽的调查后,我发布了一个视频,用Spring Boot解释了每个环境对Maven配置文件的使用情况。这是一个spring boot rest项目,它使用Maven配置文件处理每个环境的应用程序属性。

以下是链接:

Youtube:https://youtu.be/UbDpvh3YvDw

Github:https://github.com/carlosCharz/mavenprofilespringboot

代码段:

申请参数

custom.server_url = @ custom.server_url @

custom.server_port = @ custom.server_port @

custom.debuggable = @ custom.debuggable @

custom.image_quality = HIGH

覆盖参数

custom.server_url = api-dev.yourserver.com

custom.server_port = 80

custom.debuggable = true

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wedevol</groupId>
<artifactId>mvnspringboot</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>Spring Boot Project with Maven</name>
<description>This is a spring boot rest project that handle the application properties per environment using Maven profiles.</description>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <!-- https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes -->
</parent>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

<!-- Maven profile per environment -->
<profiles>
    <profile>
        <id>local</id>
        <activation>
           <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <overrides.props.file>local.overrides.properties</overrides.props.file>
            <current.profile>local</current.profile>
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <properties>
            <overrides.props.file>dev.overrides.properties</overrides.props.file>
            <current.profile>dev</current.profile>
        </properties>
    </profile>
    <profile>
        <id>qa</id>
        <properties>
            <overrides.props.file>qa.overrides.properties</overrides.props.file>
            <current.profile>qa</current.profile>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <overrides.props.file>prod.overrides.properties</overrides.props.file>
            <current.profile>prod</current.profile>
        </properties>
    </profile>
</profiles>

<build>
    <finalName>mvnspringboot</finalName>
    <!-- Maven Resources. It handles the copying of project resources to the output directory. -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>profiles/*</exclude>
            </excludes>
        </resource>
    </resources>
    <!-- Maven filtering. The variables are included in the resources ( ${..} or @...@ delimiters) -->
    <filters>
        <filter>src/main/resources/profiles/${overrides.props.file}</filter>
    </filters>
    <plugins>
        <!-- Spring boot maven plugin -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- Ant plugin to print the current maven profile -->
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>Current maven active profile: ${current.profile}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

让我知道它是否有效! Gretings!