一个项目从开发到最终上线,一般都需要不同的环境,如果只建一个配置文件,每次切换测试环境都要更换一次配置文件,很麻烦,而且很可能会忘记做这个事情。
为了省点事情,我们可以分别新建不同测试环境下的配置文件,每次测试时,只需要将配置文件名称作为启动的入参。maven 利用profile 就阔以解决。
摘抄maven,以备自己不时之需:
在pom.xml中,添加the following snippet:;
1、将appserver.home作为动态参数,这里是对一个集成测试插件动态配置
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.myco.plugins</groupId>
<artifactId>spiffy-integrationTest-plugin</artifactId>
<version>1.0</version>
<configuration>
<appserverHome>${appserver.home}</appserverHome>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
ok,我们要提供给'appserver.home'几个可能的值,profiles 华丽登场:
2、这里列出2种可能的情况分别对应不同的profile element。
<project>
...
<profiles>
<profile>
<id>appserverConfig-dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<appserver.home>/path/to/dev/appserver</appserver.home>
</properties>
</profile>
<profile>
<id>appserverConfig-dev-2</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
<properties>
<appserver.home>/path/to/another/dev/appserver2</appserver.home>
</properties>
</profile>
</profiles>
..
</project>
比如我想要开发环境下的'appserver.home',执行命令:
mvn -Denv=dev integration-test(解释 -Denv=dev为触发条件,integration-test为<phase>)
如果想要测试环境下的'appserver.home',执行命令:
mvn -Denv=test integration-test(解释同上)
3、如果你想看看 到底是哪个profile 在起作用,2种方法:
I) 执行命令:mvn help:active-profiles
II)执行命令:mvn help:effective-pom
或者干脆2个命令都执行咯:mvn -Denv=dev help:active-profiles help:effective-pom
profile 最基础的应用,就到此为止。
完!