`

maven profiles

阅读更多
定义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>
分享到:
评论

相关推荐

    maven profile多环境配置

    在maven中实现多环境的构建可移植性需要使用profile,通过不同的环境激活不同的profile来达到构建的可移植性。

    Maven权威指南 很精典的学习教程,比ANT更好用

    通过Maven Profiles实现可移植性 11.2.1. 覆盖一个项目对象模型 11.3. 激活Profile 11.3.1. 激活配置 11.3.2. 通过属性缺失激活 11.4. 外部Profile 11.5. Settings Profile 11.5.1. 全局Settings ...

    Maven Nexus配置

    目 录 Maven Nexus配置 1.配置nexus 1 2.管理仓库 2 3. 管理组 2 4. 配置maven 3 5.部署构件至Nexus 4 6.Nexus监听端口 5 7.Maven Profiles 5

    Task4Maven_TwoProfiles_Invoker

    Task4Maven_TwoProfiles_Invoker 在这个项目中,我尝试调用 assembly:single 目标两次 - 使用“Profile1”和“Profile2”配置文件。 如果我只是调用 assembly:single goal with Profile1 或 Profile2 它工作正常。 ...

    maven profile自动切换环境参数的2种方法详解

    主要给大家介绍了关于maven profile自动切换环境参数的2种方法,文中通过示例代码将这两种方法介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

    Maven管理SpringBoot Profile详解

    主要介绍了Maven管理SpringBoot Profile详解,具有一定参考价值,需要的朋友可以了解下。

    xmljava系统源码-spring-boot-assembly:SpringBoot项目使用maven-assembly-plugin根据不

    profiles和maven assembly插件根据不同环境打包成tar.gz或者zip 将spring boot项目中的配置文件提取到外部config目录中 将spring boot项目中的启动jar包移动到boot目录中 将spring boot项目中的第三方依赖jar包移动...

    使用maven Profile实现多环境构建

    使用maven Profile实现多环境构建,详细请参考 :http://blog.csdn.net/u011781521/article/details/77899467

    springboot实现maven打包加载不同环境的方式二

    当前案例中包含一整套的代码和word文档,非常适合新手... 主要是通过maven打包加载不同环境的properties文件 然后将对于的属性绑定到指定的实体对象中;然后通过调用接口可以看到加载不同环境控制台打印的内容是不一样的

    profiles例子

    Maven的profiles的使用例子

    pom.xml maven project object model

    a NB project object model file, which is very userful to set up one maven project

    maven-profiles

    maven-profiles

    springboot实现maven打包加载不同环境的方式一

    当前案例中包含一整套的代码和word文档,非常适合新手代码简单易懂; 主要是通过maven打包配合springboot application.properties文件配置实现通过打包来完成加载不同环境的配置内容;

    multiple-profiles-yaml.7z

    Springboot Maven yaml 自动多环境打包工具 ...通过maven resources plugin把配置文件从src/main/resources/${profiles.active}拷贝到src/main/resources/ 然后打包 例如: mvn clean mvn package -P dev

    maven的安装和配置

    Apache Maven是一个很流行的软件项目管理和理解工具,它能够被用来构建和管理任何基于java的项目,主要有以下几个目标: • 简化构建过程 • 提供统一的构建系统 • 提供高质量的项目信息 ...2.4、profiles配置

    Apache Maven Dependency Management

    Learn how to use profiles, POM, parent POM, and modules Increase build-speed and decrease archive size Set, rationalize, and exclude transitive dependencies Optimize your POM and its dependencies ...

    idea下test自动测试profiles配置

    idea下test自动测试profiles配置

    apache-maven-3.0.2-bin

    * [MNG-4918] - MavenProject#clone() doubles active profiles * [MNG-4919] - Plugin execution contributed by lifecycle mapping gets lost when same goal is bound multiple times * [MNG-4923] - ...

    rh-maven33-icc-profiles-openicc-1.3.1-5.11.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

Global site tag (gtag.js) - Google Analytics