- 浏览: 212100 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
Wangwei86609:
非常好的规则引擎框架,支持决策树和多线程运行规则https:/ ...
规则引擎 -
hzxlb910:
真详细,收藏哈
maven setting.xml配置说明 -
东方胜:
[b][/b]
脚本语言 Tcl -
345161974:
hyw520110 写道345161974 写道这个Visua ...
Visual Tcl Binary 完整版(完美中文支持) -
hyw520110:
345161974 写道这个Visual Tcl Binary ...
Visual Tcl Binary 完整版(完美中文支持)
Profiles是maven的一个很关键的术语:profile是用来定义一些在build lifecycle中使用的environmental variations,profile可以设置成在不同的环境下激活不同的profile(例如:不同的OS激活不同的profile,不同的JVM激活 不同的profile,不同的dabase激活不同的profile等等)。
定义Profiles
你可以把profiles定义在4个地方:
%M2_HOME%/conf/settings.xml,这是针对该部电脑的所有user的profiles,是global profiles,它会影响所有的maven project build
<your -home-directory>/.m2/settings.xml,这是针对per user的profiles,是user级的profiles,它会影响当前user的所有maven project build
定义在pom.xml文件里面,这是仅针对该project的profiles,是project级的profiles
profiles.xml,它和pom.xml在同一个目录下,也是project级的profiles,使用profiles.xml的目的是希望把profiles的设置从pom.xml里抽离出来设置。
定义在这4个地方的profiles中,涉及范围越窄的profiles会覆盖范围越宽的profiles。即:定义在pom.xml里 profiles会覆盖profiles.xml的,profiles.xml的会覆盖<your -home-directory>/.m2/settings.xml的,<your -home-directory>/.m2/settings.xml的会覆盖%M2_HOME%/conf/settings.xml的。
不过请注意:设置在pom.xml里的profiles是最最推荐的,因为pom.xml会被deploy到repository里,所以 pom.xml里的profiles才会available for subsequent builds originating from the repository or as transitive dependencies。而settings.xml和profiles.xml里定义的profiles不会被deploy到repository, 则有诸多限制,因此,只有下面几个profiles能够在settings.xml和profiles.xml里定义:
repositories
pluginRepositories
properties
其他类型的profiles必须在pom.xml里定义(上面3个profiles也可以在pom.xml里定义)。
Pom.xml能够定义的profiles包括:
<repositories>
<pluginRepositories>
<dependencies>
<plugins>
<properties> (not actually available in the main POM, but used behind the scenes)
<modules>
<reporting>
<dependencyManagement>
<distributionManagement>
a subset of the <build> element, which consists of:
<defaultGoal>
<resources>
<testResources>
<finalName>
激活Profiles
激活profiles有下列几种方式:
Explicitly
Through Maven settings
Based on environment variables
OS settings
Present or missing files
1)通过mvn命令的-P参数来显示激活profiles,该参数值是profile id list(之间用逗号连接)。如:
mvn groupId:artifactId:goal -P profileId-1,profileId-2
2)通过在settings.xml里设置<activeProfiles> element来激活(当然<profiles>也必须在settings.xml里定义)
<settings>
...
<profiles>
<profile>
<id>profile1</id>
...
</profile>
</profiles>
<activeProfiles>
<activeProfile>profile-1</activeProfile>
</activeProfiles>
...
</settings>
列在<activeProfiles>里的profiles list会在每一个project执行时被激活
3)Profiles还可以基于detect到的build environment 的state来自动激活,而不需要象上面2种方式显式激活。这只需要在profile定义时使用<activation> element。如:
<profiles>
<profile>
<activation>
<jdk>1.4</jdk>
</activation>
...
</profile>
</profiles>
上面的代码表示:如果JDK version start with 1.4 (eg. "1.4.0_08", "1.4.2_07", "1.4"),该profile会被激活
<profiles>
<profile>
<activation>
<property>
<name>debug</name>
</property>
</activation>
...
</profile>
</profiles>
上面的代码表示:如果存在system propertie “debug”,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal –Ddebug
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
上面的代码表示:如果存在system propertie “environment”的值为test,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal -Denvironment=test
4)Profiles还可以基于OS setting来自动激活
<profiles>
<profile>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
...
</profile>
</profiles>
上面的代码表示:如果OS为windows xp,该profile会被激活
5)根据某个file不存在而激活profile。例如下面定义的profile是在target/generated-sources/axistools/wsdl2java/org/apache/maven不存在时激活
<profiles>
<profile>
<activation>
<file>
<missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
</file>
</activation>
...
</profile>
</profiles>
使用Profiles时要注意的2个问题
第一、external properties
不是定义在pom.xml里的properties都称为external properties。举例说明最明了:
pom.xml:
<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>
~/.m2/settings.xml
<settings>
...
<profiles>
<profile>
<id>appserverConfig</id>
<properties>
<appserver.home>/path/to/appserver</appserver.home>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>appserverConfig</activeProfile>
</activeProfiles>
...
</settings>
当你执行该pom时,运行正常。但如果another user执行时,则运行失败,因为无法解析${appserver.home}(这是由于该properties是定义在user级别的settings.xml)。
解决方法就是把该profile放到pom.xml里定义,但这样做的缺点是所有使用该profile的pom.xml每个都要定义一次该profile。
最好的解决方法是:Since Maven provides good support for project inheritance, it's possible to stick this sort of configuration in the pluginManagement section of a team-level POM or similar, and simply inherit the paths
第二、pom.xml里定义的profiles不符合激活条件
依然是举个例子:
pom.xml:
<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>dev-2</value>
</property>
</activation>
<properties>
<appserver.home>/path/to/dev/appserver2</appserver.home>
</properties>
</profile>
</profiles>
<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>
上面定义的pom.xml定义了两个profile:不同的”env”参数值会激活不同的profile。当执行命令:
mvn -Denv=dev-2 integration-test
就会激活profile “appserverConfig-dev-2”
当执行命令:
mvn -Denv=dev integration-test
就会激活profile “appserverConfig-dev”
而当执行命令:
mvn -Denv=production integration-test
则运行失败,因为没有激活任何一个profile,因此无法解析${appserver.home}。
查看build time过程中使用了哪些Profiles
执行help plugin的active-profiles goal,使用命令:
mvn help:active-profiles
例子:
对于上面的例子,如果输入命令:
mvn help:active-profiles -Denv=dev
则输出的是:
The following profiles are active:
- appserverConfig-dev (source: pom)
如果有一个profile定义在settings.xml里并使用<activeProfile>激活,那么输入命令:
mvn help:active-profiles
则输出的是:
The following profiles are active:
- appserverConfig (source: settings.xml)
如果输入命令:
mvn help:active-profiles -P appserverConfig-dev
那么输出的是:
The following profiles are active:
- appserverConfig-dev (source: pom)
- appserverConfig (source: settings.xml)
发表评论
-
Maven Artifacts如何部署到仓库
2012-03-28 11:50 944http://www.blogjava.net/lishunl ... -
maven常见问题问答
2011-11-16 13:24 721前言 Maven,发音是[`meivin],"专家 ... -
maven setting.xml配置说明
2011-11-16 12:43 1274setting.xml view plain ... -
Maven实战指南:“打包”的技巧
2011-10-11 10:13 2026http://tech.it168.com/a2011/062 ... -
M2工程 mvn deploy 401 403错误处理
2011-10-10 15:11 1070http://hi.baidu.com/g4_gc/blog ... -
maven部署web工程基础步骤
2011-10-10 12:43 19911.准备工作 下载maven(url:http://a ... -
eclipse maven wtp jar/lib deploy
2011-10-09 09:57 986eclipse工程(with maven & wtp) ... -
配置Maven web项目
2011-09-13 16:00 9121、创建Web应用 mvn archetype:genera ... -
maven报错:mvn deploy
2011-09-09 14:01 1618一.Error deploying artifact: ... -
maven配置篇之pom.xml
2011-09-08 15:24 760说完了settings.xml配置, ... -
maven项目添加jar包.
2011-08-16 09:31 1118很多新手都不知道如何在maven项目里添加jar包. 以前我还 ... -
Hudson+Maven+SVN 快速搭建持续集成环境
2011-05-26 14:09 970原: http://www.blogjava.net ... -
基于maven和hudson打造持续集成环境
2011-05-26 12:58 1050对持续集成的需求 对持续集成的需求主要来自项目过程的痛,在 ... -
maven 配置篇 之 settings.xml
2011-05-24 20:36 837maven2 比起maven1 来说,需要配置的文件少多了,主 ... -
Maven生命周期详解
2011-05-24 17:47 652Maven强大的一个重要的 ... -
激活Maven profile的几种方式
2011-05-24 17:43 821首先简单介绍下 Maven 的 profile 是什么。对于人 ... -
使用Profile和Resources Filter隔离测试环境
2011-05-24 17:42 786Maven能够帮我们很好的 ... -
使用maven-sql-plugin实现持续数据库集成(CDBI)
2011-05-24 17:41 916数据库持续集成(Continuous Database Int ... -
Maven最佳实践:版本管理
2011-05-24 16:32 765原文:http://juvenshun.iteye ... -
Maven仓库
2011-05-24 16:30 811什么是Maven仓库 在不用Maven的时候,比如说以前我们 ...
相关推荐
maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装maven安装...
maven中setting阿里云的详细镜像配置,setting的配置细节 maven中setting阿里云的详细镜像配置,setting的配置细节
在maven中实现多环境的构建可移植性需要使用profile,通过不同的环境激活不同的profile来达到构建的可移植性。
Maven 中文简介, 包含Maven 语法, 节点讲解, 使用方法.
maven 实用手册 中文 Maven权威指南中文版 Maven权威指南中文版
maven中jar包为下载完成last清理,更好的让maven管理jar包
maven-profiles
maven中文学习,maven-definitive-guide_zh.pdf
有些时候,当我们需要在自己的maven工程中添加一个确定的jar而发现Maven中央仓库不存在时,但是我们自己手上有这些需要的jar包,那么我们可以自给自足,把这个jar包添加到我们的自己的maven仓库中,这样方便在maven...
Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令Maven 命令
maven中oracle jar包
maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0maven3.0
Maven中scope详细说明 依赖范围控制哪些依赖在哪些classpath 中可用,哪些依赖包含在一个应用中。
Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven3.9.4版本压缩包,仅供学习参考,更新版本请前往Maven官方下载;Maven...
刚学习Java的时候找jar包有点繁琐,后来才知道可以用maven直接导入,现在把maven中常用的jar包库上传,方便初学者。加油(ง •_•)ง
Maven的profiles的使用例子
删除maven仓库中的失败文件
maven的中文文档和maven3.3.5绿色版,绝对属实,提供给大家下载
apache-maven-3.0.5 apache-maven-3.1.1 apache-maven-3.2.5 apache-maven-3.3.9 apache-maven-3.5.4 apache-maven-3.6.3 apache-maven-3.8.5 每个版本包含4个文件: apache-maven-3.8.5-bin.tar.gz apache-maven-...
主要介绍了一键清除maven仓库中下载失败的jar包的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧