如何根据环境值设置属性?

时间:2016-06-03 14:06:56

标签: maven

我尝试创建一个maven脚本,负责处理不同环境devtestproduction的包装。

我想制作一些将某些文件复制到特定目录的模板。像这样:

<profile>       
    <id>default-profile</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>   
    <build>
        <pluginManagement> 
            <plugins>                   
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>                          
                    <executions>            
                        <execution>
                            <id>prepare-package</id>
                            <phase>prepare-package</phase>
                            <configuration>
                                <target>
                                    <echo message="Copying configuraiton files .."/>
                                    <copy file="${package-mode.config-directory}/x.xml" todir="src/main/webapp/WEB-INF/config" /> 
                                </target>
                            </configuration>                                    
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>               
                </plugin>
            </plugins>
        </pluginManagement>
    </build>    
</profile>

我目前的问题是我需要根据我想要构建的环境来设置${package-mode.config-directory}

我目前正在使用maven-enforcer-plugin以便设置环境变量:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
        <execution>
            <id>enforce-property</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <requireProperty>
                        <property>package-mode</property>
                        <message>You must set a "package-mode" property!</message>
                        <regex>(test|dev|deploy)</regex>
                        <regexMessage>
                            [ ########### ] "package-mode"" must be either "test", "dev" or "deploy".
                        </regexMessage>
                    </requireProperty>
                </rules>
                <fail>true</fail>
            </configuration>
        </execution>
    </executions>
</plugin>

但我现在面临的问题是我需要为${package-mode.config-directory}进行某种条件构建。我需要类似这个:

<plugin>
    <!-- ... -->
    <property>package-mode</property>
    <condition>
        <equals>test</equals>
        <package-mode.config-directory>src/main/assembly/test</package-mode.config-directory>
    </condition>
    <condition>
        <equals>dev</equals>
        <package-mode.config-directory>src/main/assembly/dev</package-mode.config-directory>
    </condition>
    <condition>
        <equals>deploy</equals>
        <package-mode.config-directory>src/main/assembly/deploy</package-mode.config-directory>
    </condition>
</plugin>

有可能吗?

1 个答案:

答案 0 :(得分:0)

结果我可以简单地使用已经强制执行的环境变量package-mode,并将目录命名为其有效值。人们可以争论这是否“好”但是它有效并且它构建了我的战争文件:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>prepare-package</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>src/main/webapp/WEB-INF/config</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/assembly/${package-mode}/config</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>