Maven 2 基于多属性的maven配置文件激活

Maven 2 基于多属性的maven配置文件激活,maven-2,profile,activation,Maven 2,Profile,Activation,我正在为一个项目创建一个maven 2构建,我提出了一个概要文件,因为构建必须为不同的位置(比如柏林、巴黎、北极)和不同的环境(开发、生产)创建。这些是通过属性指定的。因此,对于“北极”“开发人员”,我会: 现在,我想基于这两个属性而不仅仅是一个属性来激活我的porfile。所以我试着如下: <profiles> <profile> <id>NOrth Pole DEV</id> <activation>

我正在为一个项目创建一个maven 2构建,我提出了一个概要文件,因为构建必须为不同的位置(比如柏林、巴黎、北极)和不同的环境(开发、生产)创建。这些是通过属性指定的。因此,对于“北极”“开发人员”,我会:

现在,我想基于这两个属性而不仅仅是一个属性来激活我的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希望在那里最多看到一个
元素

请注意,我对这些属性也有另一个用途,因此我不想让它成为具有值的单个属性
locationEnv


那么,是否有任何方法、解决方法或其他方法可以基于属性组合激活配置文件?

恐怕没有很好的解决方案来解决您的问题(除非有我不知道的新Maven功能)

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


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

我相信您可以这样做

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

中添加配置文件,为什么不直接使用配置文件,如:

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

对我来说,khmarbaise的回答似乎更优雅。 对于Jan的评论,您可以通过附加属性来引用该文件 e、 g.使用概要文件dev,激活North Pole,您可以使用 ${location}-${env}.xml

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

可能的解决方案) 请尝试此扩展:

这允许使用以下语法:

<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_HOME/lib/ext/el-profile-activator-extension.jar文件的
  • 此概要文件可以使用将JAR从maven下载到init阶段的$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>
    
    
    准备maven扩展libs
    ${maven.home}/lib/ext/el-profile-activator-extension.jar
    org.apache.maven.plugins
    maven依赖插件
    2.8
    复制
    验证
    复制
    com.redhat.jboss.maven
    el剖面激活器扩展
    1.0.0-SNAPSHOT
    罐子
    真的
    ${maven.home}/lib/ext
    el-profile-activator-extension.jar
    org.mvel
    mvel2
    2.1.3.最终版本
    罐子
    真的
    ${maven.home}/lib/ext
    mvel2.jar
    ${project.build.directory}/wars
    真的
    真的
    org.codehaus.gmaven
    gmaven插件
    1.4
    验证
    执行
    fail(“对于概要文件激活,我们使用一个扩展jar。它现在位于${maven.home}/lib/ext文件夹中。请重新启动构建,然后它就会成功。”)
    
    经过详尽的调查后,我发布了一个视频,其中我解释了在每个环境下使用Spring Boot的Maven配置文件。这是一个Spring Boot rest项目,使用Maven配置文件处理每个环境的应用程序属性

    以下是链接:

    Youtube:

    Github:

    代码段:

    应用参数 custom.server\u url=@custom.server\u url@

    custom.server\u port=@custom.server\u port@

    custom.debuggable=@custom.debuggable@

    custom.image\u质量=高

    覆盖参数 custom.server_url=api-dev.yourserver.com

    custom.server_端口=80

    custom.debugable=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>
    
    
    4.0.0
    com.wedevol
    mvnspringboot
    1.0.0
    战争
    使用Maven的springboot项目
    这是一个SpringBootREST项目,它使用Maven概要文件处理每个环境的应用程序属性。
    1.8
    UTF-8
    UTF-8
    org.springframework.boot
    弹簧靴起动器-
    
    <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>
    
    <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>