1.profiles
我们的项目会存在不同的运行环境,比如开发环境、测试环境、生产环境,而我们的项目在不同的环境中,有的配置可能会不一样。
需要通过profiles
配置
配置如下
<profiles>
<profile>
<id>dev</id> <!--不同环境Profile的唯一id-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>fat</id>
<properties>
<env>fat</env>
</properties>
</profile>
</profiles>
各种环境
- dev 开发环境
- fat 工厂验收测试
- uat (User Acceptance Test)用户接受度测试 即验收测试,所以UAT环境主要是用来作为客户体验的环境。
- prod 生产环境
- pt 性能测试
- newuat
- master 线上环境(生产环境)
2.激活方式
-
1.使用maven打包时通过-P参数,-P后跟上profile的唯一id,如
mvn clean package -Pdev
-
2.通过pom文件里的activation属性
<profile> <id>prod</id> <properties> <profiles.active>prod</profiles.active> </properties> <!--activation用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失--> <activation> <!--配置默认激活--> <activeByDefault>true</activeByDefault> <!--当jdk环境版本1.5或以上时,此profile被激活--> <jdk>[1.5,)</jdk> <!--根据当前操作系统--> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <!--通过系统环境变量,name-value自定义--> <property> <name>env</name> <value>test</value> </property> <!--通过文件的存在或缺失--> <file> <missing>target/generated-sources/axistools/wsdl2java/ com/companyname/group</missing> <exists/> </file> </activation> </profile>
-
3.settings.xml中使用activeProfiles指定
settings.xml 中的profile 元素是 pom.xml中 profile 元素的裁剪版本。它包含了 activation, repositories , pluginRepositories 和 properties元素。这里的profile元素只包含这四个子元素是因为这里只关心构建系统这个整体(这正是 settings.xml文件的角色定位),而非单独的项目对象模型设置。
如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。
<activeProfiles> <activeProfile>carkey-repo-profile</activeProfile> <activeProfile>s3s3l-profile</activeProfile> </activeProfiles> <profile> <id>carkey-repo-profile</id> <repositories> <repository> <id>snapshot-repo</id> <url></url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile>