本文介绍了在maven构建中定义机器特定资源的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在maven构建中是否提供环境特定资源的标准方法?

Is there a standard way to provision environment specific resources in a maven build?

例如 - 我们期望我们的构建将在应用程序中使用的本地服务的特定IP地址不同的环境中运行。

For example - we expect that our build will run in environments where the specific IP address of a the local service which is used in an application is different.

一个选项是将其设置为shell环境变量,但它不清楚,这将导致运行单元测试的surefire jvm。

One option is to set this as a shell environmental variable, but its not clear wether this will propogate down to the surefire jvm's which run unit tests.

另一个选择是在pom.xml子类文件中提供此信息,但随附其他行李(每个开发人员都需要维护自己的pom文件),这当然会打破

Another option is to provision this information in the pom.xml subclassed file , but that comes with other baggage (each developer would need to maintain their own pom file), and this would of course break any sort of automated build environment.

推荐答案

以下示例说明如何使用构建配置文件来获取不同的属性集值

The following example shows how a build profile can be used to pick up different sets of property values.

您可以使用 -P 参数激活一个的构建配置文件

You can use the -P parameter to activate one of the build profile

$ mvn -Ptest1 compile
..
[INFO] --- maven-antrun-plugin:1.7:run (default) @ demo ---
[INFO] Executing tasks

main:
     [echo] arbitrary.property=1.0
..

切换个人资料选项增加与第二个配置文件相关联的属性值:

Switching profile picks up the property value associated with the second profile:

$ mvn -Ptest2 compile
..
[INFO] --- maven-antrun-plugin:1.7:run (default) @ demo ---
[INFO] Executing tasks

main:
     [echo] arbitrary.property=2.0
..



pom.xml



pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <configuration>
                            <target>
                                <echo message="arbitrary.property=${arbitrary.property}"/>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>test1</id>
            <properties>
                <arbitrary.property>1.0</arbitrary.property>
            </properties>
        </profile>
        <profile>
            <id>test2</id>
            <properties>
                <arbitrary.property>2.0</arbitrary.property>
            </properties>
        </profile>
    </profiles>
</project>

这篇关于在maven构建中定义机器特定资源的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 19:40