Maven的pom.xml文件结构之环境配置profiles

在Maven项目中,profile是根据不同的构件环境,对构建(build)过程进行动态配置的手段。

可以通过pom.xml定义多个profile,也可以通过settings.xml文件定义多个profile。对于一个profile,如果同时在pom.xml和settings.xml中配置,则settings.xml中的配置优先。

settings.xml中的profile只能配置如下元素:

  • id
  • activation
  • repositories
  • pluginRepositories
  • properties

pom.xml中的profile能配置如下元素:

  • id
  • activation
  • repositories
  • pluginRepositories
  • properties
  • build
  • modules
  • dependencies
  • dependencyManagement
  • repositories
  • pluginRepositories
  • distributionManagement
  • reporting

在每个profile中,都有一个<activation>给出当前profile被激活的环境条件,其中给出的任何一个条件满足即激活该profile。示例如下:

  <profiles>
    <profile>
      <id>mytest</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>sparrow-type</name>
          <value>African</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>
</project>
说明:

  • jdk,检查构建使用的JDK
  • os,检查构建服务器的操作系统
  • property,检查构建过程中是否存在名为${name}的属性及其值
  • file,检查这里配置的文件是否存在(<exists>)或者不存在<missing>

一个profile是否被激活,还可以根据在settings.xml文件中的<activeProfile>中的id属性,或者根据JVM启动参数-P给出的激活条件,如mvn clean package -Pmytest。


  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值