maven 学习---Maven 构建配置文件
什么是构建配置文件?
构建配置文件是一组配置的集合,用来设置或者覆盖 Maven 构建的默认配置。使用构建配置文件,可以为不同的环境定制构建过程,例如 Producation 和 Development 环境。
Profile 在 pom.xml 中使用 activeProfiles / profiles 元素指定,并且可以用很多方式触发。Profile 在构建时修改 POM,并且为变量设置不同的目标环境(例如,在开发、测试和产品环境中的数据库服务器路径)。
Profile 类型
Profile 主要有三种类型。
| 类型 | 在哪里定义 |
|---|---|
| Per Project | 定义在工程 POM 文件 pom.xml 中 |
| Per User | 定义在 Maven 设置 xml 文件中 (%USER_HOME%/.m2/settings.xml) |
| Global | 定义在 Maven 全局配置 xml 文件中 (%M2_HOME%/conf/settings.xml) |
Profile 激活
Maven 的 Profile 能够通过几种不同的方式激活。
- 显式使用命令控制台输入
- 通过 maven 设置
- 基于环境变量(用户 / 系统变量)
- 操作系统配置(例如,Windows family)
- 现存 / 缺失 文件
Profile 激活示例
我们假定你的工程目录像下面这样:

现在,在 src/main/resources 目录下有三个环境配置文件:
| 文件名称 | 描述 |
|---|---|
| env.properties | 没有配置文件时的默认配置 |
| env.test.properties | 使用测试配置文件时的测试配置 |
| env.prod.properties | 使用产品配置文件时的产品配置 |
显式 Profile 激活
在接下来的例子中,我们将 attach maven-antrun-plugin:run 目标添加到测试阶段中。这样可以我们在不同的 Profile 中输出文本信息。我们将使用 pom.xml 来定义不同的 Profile,并在命令控制台中使用 maven 命令激活 Profile。
假定,我们在 C:\MVN\project 目录下创建了以下的 pom.xml 文件。
<project xmlns="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>
<copy file="src/main/resources/env.test.propertiestofile
="${project.build.outputDirectory}/env.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
现在打开命令控制台,跳转到 pom.xml 所在目录,并执行以下 mvn 命令。使用 -P 选项指定 Profile 的名称。
C:\MVN\project>mvn test -Ptest
Maven 将开始处理并显示 test 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] ------------------------------------------------------------------
现在我们练习一下,你可以按照下面的步骤做:
- 在 pom.xml 的 profiles 元素中添加另一个 profile 元素(拷贝已有的 profile 元素并粘贴到 profiles 元素结尾)。
- 将此 profile 元素的 id 从 test 修改为 normal。
- 将任务部分修改为 echo env.properties,以及 copy env.properties 到目标目录
- 再次重复以上三个步骤,修改 id 为 prod,修改 task 部分为 env.prod.properties
- 全部就这些了。现在你有了三个构建配置文件(normal / test / prod)。
现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。使用 -P 选项指定 Profile 的名称。
C:\MVN\project>mvn test -Pnormal
C:\MVN\project>mvn test -Pprod
检查构建的输出看看有什么不同。
通过 Maven 设置激活 Profile
打开 Maven 的 settings.xml 文件,该文件可以在 %USER_HOME%/.m2 目录下找到,%USER_HOME% 表示用户主目录。如果 settings.xml 文件不存在则需要创建一个。
像在下面例子中展示的一样,使用 activeProfiles 节点添加 test 配置作为激活的 Profile。
<settings xmlns="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>
现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。
C:\MVN\project>mvn test
通过环境变量激活 Profile
现在从 maven 的 settings.xml 中删除激活的 Profile,并更新 pom.xml 中的 test Profile。将下面的内容添加到 profile 元素的 activation 元素中。
当系统属性 “env” 被设置为 “test” 时,test 配置将会被触发。创建一个环境变量 “env” 并设置它的值为 “test”。
<profile>
<id>test</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
</profile>
现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。
C:\MVN\project>mvn test
通过操作系统激活 Profile
activation 元素包含下面的操作系统信息。当系统为 windows XP 时,test Profile 将会被触发。
<profile>
<id>test</id>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。
C:\MVN\project>mvn test
通过现存 / 缺失的文件激活 Profile
现在使用 activation 元素包含下面的操作系统信息。当 target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失时,test Profile 将会被触发。
<profile>
<id>test</id>
<activation>
<file>
<missing>target/generated-sources/axistools/wsdl2java/
com/companyname/group</missing>
</file>
</activation>
</profile>
现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。
C:\MVN\project>mvn test
maven 学习---Maven 构建配置文件的更多相关文章
- maven 学习---Maven构建生命周期
构建生命周期是一组阶段的序列(sequence of phases),每个阶段定义了目标被执行的顺序.这里的阶段是生命周期的一部分. 举例说明,一个典型的 Maven 构建生命周期是由以下几个阶段的序 ...
- maven 学习---Maven构建自动化-Hudson
建立自动化定义场景,依赖项目建设过程中被启动,一旦项目生成成功完成,以确保相关的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序,网页UI和应用程序的桌面UI ...
- maven 学习---Maven 插件
什么是 Maven 插件? Maven 实际上是一个依赖插件执行的框架,每个任务实际上是由插件完成.Maven 插件通常被用来: 创建 jar 文件 创建 war 文件 编译代码文件 代码单元测试 创 ...
- maven 学习---Maven项目文档
本教程将教你如何一步到位创建应用程序的文档.因此,让我们开始,到 C:/MVN 创建java应用程序consumerBanking. OpenconsumerBanking文件夹,然后执行以下命令m ...
- maven 学习---Maven依赖机制
在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级. 案例分析 让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要做什么? 1.在传统 ...
- maven 学习---Maven安装配置
想要安装 Apache Maven 在Windows 系统上, 只需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : JDK 1.8 ...
- maven 学习---Maven教程
Apache Maven是一个软件项目管理和综合工具.基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报告和文件. 本教程将介绍如何使用Maven在Java开发,或任何其 ...
- maven 学习---Maven添加远程仓库
默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资源库 添加Java.ne ...
- maven 学习---Maven中央存储库
当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载. 首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源, 如果没有找到,然后把它会 ...
随机推荐
- element-ui更改滚动条颜色
.find-car ::-webkit-scrollbar-thumb{ background-color: #001f3f; } .find-car ::-webkit-scrollbar-trac ...
- 获取SpringCloud gateway响应的response的值,记录踩坑
最近在做网关改造,想要通过Gateway过滤器获取ResponseBody的值,查看了网上的帖子和官网内容: 帖子:https://cloud.tencent.com/developer/articl ...
- TICK技术栈(一)TICK技术栈介绍
1.什么是TICK技术栈? 1.1 简介 TICK 是由 InfluxData开发的一套开源工具栈,由 Telegraf, InfluxDB, Chronograf, Kapacitor 四个工具的首 ...
- js 判断当前时间是否处于某个一个时间段内
js 判断当前时间(或者所选时间)是否在某一时间段 我们可以使用 jutils - JavaScript常用函数库的 isDuringDate 函数来实现 传入 beginDateStr (开始时间) ...
- python的pip安装时,使用国内Pypi源
有时,国外的网速确实不理想. 想安装python库,还是国内快点. 参考URL: http://www.mamicode.com/info-detail-2248964.html 阿里云 http:/ ...
- try ... except...,好处是执行失败后,仍然可以继续运行
import requeststry: a=requests.get("https:///www.baidu.com") print('连接成功')except: print('连 ...
- Maven中使用tomcat:run出现错误org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
配置是正常的.查阅资料以后说是jdk版本什么的问题.多方修改没有任何改观.换一个思路去查询tomcat:run怎么运行. 是因为他还是沿用了上一次的tomcat插件(默认是6)所以运行的时候使用 to ...
- [C1W2] Neural Networks and Deep Learning - Basics of Neural Network programming
第二周:神经网络的编程基础(Basics of Neural Network programming) 二分类(Binary Classification) 这周我们将学习神经网络的基础知识,其中需要 ...
- 鲜贝7.3--Xshell安装
安装包百度云下载地址:https://blog.csdn.net/yueruitao/article/details/85263968 具体方法请参考: https://blog.csdn.net/q ...
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...