Maven Build Profiles--reference
What is Build Profile?
A Build profile is a set of configuration values which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such asProduction v/s Development environments.
Profiles are specified in pom.xml file using its activeProfiles / profiles elements and are triggered in variety of ways. Profiles modify the POM at build time, and are used to give parameters different target environments (for example, the path of the database server in the development, testing, and production environments).
Types of Build Profile
Build profiles are majorly of three types
| Type | Where it is defined |
|---|---|
| Per Project | Defined in the project POM file, pom.xml |
| Per User | Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml) |
| Global | Defined in Maven global settings xml file (%M2_HOME%/conf/settings.xml) |
Profile Activation
A Maven Build Profile can be activated in various ways.
Explicitly using command console input.
Through maven settings.
Based on environment variables (User/System variables).
OS Settings (for example, Windows family).
Present/missing files.
Profile Activation Examples
Let us assume following directory structure of your project:

Now, under src/main/resources there are three environment specific files:
| File Name | Description |
|---|---|
| env.properties | default configuration used if no profile is mentioned. |
| env.test.properties | test configuration when test profile is used. |
| env.prod.properties | production configuration when prod profile is used. |
Explicit Profile Activation
In the following example, We'll attach maven-antrun-plugin:run goal to test phase. This will allow us to echo text messages for different profiles. We will be using pom.xml to define different profiles and will activate profile at command console using maven command.
Assume, we've created following pom.xml in C:\MVN\project folder.
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.companyname.projectgroup</groupId><artifactId>project</artifactId><version>1.0</version><profiles><profile><id>test</id><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.1</version><executions><execution><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>Using env.test.properties</echo><copyfile="src/main/resources/env.test.properties"tofile="${project.build.outputDirectory}/env.properties"/></tasks></configuration></execution></executions></plugin></plugins></build></profile></profiles></project>
Now open command console, go to the folder containing pom.xml and execute the following mvncommand. Pass the profile name as argument using -P option.
C:\MVN\project>mvn test -Ptest
Maven will start processing and display the result of test build profile.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\project\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\MVN\project\target\surefire-reports -------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run. Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[echo] Using env.test.properties
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sun Jul 08 14:55:41 IST 2012
[INFO] Final Memory: 8M/64M
[INFO] ------------------------------------------------------------------
Now as an exercise, you can do the following steps
Add another profile element to profiles element of pom.xml (copy existing profile element and paste it where profile elements ends).
Update id of this profile element from test to normal.
Update task section to echo env.properties and copy env.properties to target directory
Again repeat above three steps, update id to prod and task section for env.prod.properties
That's all. Now you've three build profiles ready (normal / test / prod).
Now open command console, go to the folder containing pom.xml and execute the following mvncommands. Pass the profile names as argument using -P option.
C:\MVN\project>mvn test -Pnormal
C:\MVN\project>mvn test -Pprod
Check the output of build to see the difference.
Profile Activation via Maven Settings
Open Maven settings.xml file available in %USER_HOME%/.m2 directory where %USER_HOME%represents user home directory. If settings.xml file is not there then create a new one.
Add test profile as an active profile using activeProfiles node as shown below in example
<settingsxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd"><mirrors><mirror><id>maven.dev.snaponglobal.com</id><name>Internal Artifactory Maven repository</name><url>http://repo1.maven.org/maven2/</url><mirrorOf>*</mirrorOf></mirror></mirrors><activeProfiles><activeProfile>test</activeProfile></activeProfiles></settings>
Now open command console, go to the folder containing pom.xml and execute the following mvncommand. Do not pass the profile name using -P option.Maven will display result of test profile being an active profile.
C:\MVN\project>mvn test
Profile Activation via Environment Variables
Now remove active profile from maven settings.xml and update the test profile mentioned in pom.xml. Add activation element to profile element as shown below.
The test profile will trigger when the system property "env" is specified with the value "test". Create a environment variable "env" and set its value as "test".
<profile><id>test</id><activation><property><name>env</name><value>test</value></property></activation></profile>
Let's open command console, go to the folder containing pom.xml and execute the following mvncommand.
C:\MVN\project>mvn test
Profile Activation via Operating System
Activation element to include os detail as shown below. This test profile will trigger when the system is windows XP.
<profile><id>test</id><activation><os><name>Windows XP</name><family>Windows</family><arch>x86</arch><version>5.1.2600</version></os></activation></profile>
Now open command console, go to the folder containing pom.xml and execute the following mvncommands. Do not pass the profile name using -P option.Maven will display result of test profile being an active profile.
C:\MVN\project>mvn test
Profile Activation via Present/Missing File
Now activation element to include os detail as shown below. The test profile will trigger whentarget/generated-sources/axistools/wsdl2java/com/companyname/group is missing.
<profile><id>test</id><activation><file><missing>target/generated-sources/axistools/wsdl2java/com/companyname/group</missing></file></activation></profile>
Now open command console, go to the folder containing pom.xml and execute the following mvncommands. Do not pass the profile name using -P option.Maven will display result of test profile being an active profile.
C:\MVN\project>mvn test reference from :http://www.tutorialspoint.com/maven/maven_build_profiles.htm
Maven Build Profiles--reference的更多相关文章
- Maven Build profiles
They modify the POM at build time, and are meant to be used in complementary sets to give equivalent ...
- Spring Boot-右键maven build成功但是直接运行main方法出错的解决方案
1.代码就一个Controller,从官网复制过来的,如下 package com.springboot.controller; import org.springframework.boot.Spr ...
- Maven build标签
前言: <build >设置,主要用于编译设置 1.分类 在Maven的pom.xml文件中,存在如下两种<build>: (1)全局配置(project build) 针对整 ...
- maven中profiles使用详解
使用的场景 常常遇到一些项目中多环境切换的问题.比如在开发过程中用到开发环境,在测试中使用测试环境,在生产中用生产环境的情况.springboot中提供了 spring.profile.active的 ...
- maven:log4j:WARN No appenders could be found for logger (loggerInfo).或者maven build error:org.apache.maven.lifecycle.LifecycleExecutionExceptio
maven在build构建时,加载资源文件时需要配置资源文件插件: 1,在pom.xml文件中加入 <build> <finalName>${project.build.tar ...
- 在eclipse如何删除无效的maven build
在Eclipse的maven项目中,点击一次“maven build...”明明没有配置,它也就会产生一个maven build,那么如何删除这些无效的配置呢?
- Maven Build Life Cycle--reference
What is Build Lifecycle? A Build Lifecycle is a well defined sequence of phases which define the ord ...
- No compiler is provided in this environment. --Maven build失败
今天,maven build 失败了, 遇到下面的问题 经过查找,通过这个大佬的blog( https://blog.csdn.net/lslk9898/article/details/738367 ...
- 转:eclipse maven build、maven install 等区别
原文地址:eclipse maven build.maven install 等区别
随机推荐
- Android最佳实践指南
Updated on 2016/1/6 修正了一些翻译段落欢迎转载,但请保留译者链接:http://www.jianshu.com/p/613d28a3c8a0 Lessons learned fro ...
- Spring AOP实现方式三之自动扫描注入【附源码】
注解AOP实现 这里唯一不同的就是application 里面 不需要配置每个bean都需要配置了,直接自动扫描 注册,主要知识点是怎么通过配置文件得到bean, 注意类前面的@注解. 源码结构: ...
- S5PV210的IRAM应用
准备分析 IRAM的大小96k,其实前两个程序都在这里运行的,程序都小于16K.要实现的是从把IRAM从的前16k从IRAM的起始地址0xD0020000拷贝到0xD0024000 处,调用mai ...
- hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)
#1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...
- poj 1084 舞蹈链(纠结题)
此题反正我自己是认为poj给的数据范围是有错的,不知道是不是自己太弱了,有大神在的话,欢迎来呸! 其实目的就在于建图,搞的我后来建了一个无比纠结的图,先建立了火柴棍和正方形的一个全图,然后再删除一些火 ...
- 关于app transfer之后的开发
原文 http://blog.csdn.net/donghong2008/article/details/38020855 网络上有很多开发者提问怎么转让App并想知道具体的流程.实际上Appsto ...
- 【转】Mac QQ截图保存在哪里?
原文网址:http://www.pc6.com/edu/67677.html QQ Mac版的截屏图片保存在哪儿呢?可不可以像Windows版本一样设定保存路径呢?当然是可定的.Mac QQ截图保存你 ...
- NEsper事件处理 z
http://esper.codehaus.org/nesper/documentation/documentation.html 环境配置 NEsper库下载:下载网址 Vs2010环境集成 在项目 ...
- java反编译工具
由于JAVA语言安全性高.代码优化.跨平台等特性,从1995年5月由SUN公司发布后,迅速取代了很多传统高级语言,占据了企业级网络应用开发等诸多领域的霸主地位. 不过,JAVA最突出的跨平台优势使得它 ...
- SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演
这样的点分成三类 1 不含0,要求三个数的最大公约数为1 2 含一个0,两个非零数互质 3 含两个0,这样的数只有三个,可以讨论 针对 1情况 定义f[n]为所有满足三个数最大公约数为n的三元组数量 ...