在Maven中简要分享配置文件配置文件的方法?

时间:2013-05-28 11:03:57

标签: java maven maven-2 maven-profiles

我有一个像这样定义的maven个人资料;

<profile>
            <id>instrumentation</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>instrumentation.enabled</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>cobertura-maven-plugin</artifactId>
                        <version>2.5.1</version>
                        <configuration>
                            <formats>
                                <format>xml</format>
                                <format>html</format>
                            </formats>
                            <instrumentation>
                                <ignores>
                                </ignores>
                                <excludes>
                                    <exclude>**/Test*.class</exclude>
                                    <exclude>**/Abstract*.java</exclude>
                                    <!-- Log Classes -->
                                    <exclude>**/Log.class</exclude>
                                    <!-- Exception Classes -->
                                    <exclude>**/*Exception.class</exclude>
                                </excludes>
                            </instrumentation>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>cobertura</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>2.6</version>
                        <executions>
                            <execution>
                                <id>copy-resources</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                <!-- this is important -->
                                    <overwrite>true</overwrite>
                                    <!-- target -->
                                    <outputDirectory>${env.DERBASE}/java/${project.artifactId}/target/classes</outputDirectory>
                                    <resources>
                                        <resource>
                                        <!-- source -->
                                            <directory>${env.DERBASE}/java/${project.artifactId}/target/generated-classes/cobertura</directory>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

所以我想做的就是改变这一行;

<goal>cobertura</goal>

到此;

<goal>instrumentation</goal>

如果设置了一个env变量,例如INSTRUMENTATION_EXCLUDE_TESTS。

我只是想知道实现这个目标的最佳方法是什么? (即,只需更改并粘贴单行更改的配置文件)

非常感谢!

1 个答案:

答案 0 :(得分:2)

所以,我通过设置一个环境变量来解决这个问题,该变量可以是;

export COBERTURA_GOAL=cobertura

export COBERTURA_GOAL=instrument

然后,在我的pom中,定义了以下属性;

<cobertura.goal>${env.COBERTURA_GOAL}</cobertura.goal>

插入了哪个;

 <goal>${cobertura.goal}</goal>