maven学习笔记六 构建配置

本文详细介绍了Maven中的profile特性,包括如何在POM中配置、不同类型的profile(全局、个人、项目)、激活方式(环境变量、操作系统等)、以及如何在不同环境中定制构建。强调了合理使用profile以保持项目移植性的重要性。
摘要由CSDN通过智能技术生成

maven一直致力于使构建越来越好。这意味着在POM中对构建进行配置。(profile)

这就避免了所有的所有的文件系统都会对其进行引用。

本地库只存储元数据使这种方式能够得以实现。

无论如何,彻底的移植性是不大可能的。在某种情况下,

插件需要使用本地路径,依赖也有些不同。构建名称也有可能需要改变。

甚至有时候整个插件都不同。为了处理这种情况,maven引入了构建配置这个概念。

配置定义为一个元素的有效的子集和一个额外的单元,和能够被各种形式触发。

配置在构建时修改对象模型,这意味着在不同的环境中设置了一系列等价的不同值。(例如,测试,开发,生产环境)

profile同样也可以容易的为团队的每个成员定制不同的构建结果。

无论怎样,对profile运用得当,那么项目将保留很好的移植性。

那么,仅仅运行一个POM,用maven的-f选项就可以使用不同的参数创建不同的POM。

profile的类型,和定义的位置。

每个项目的profile

 -定义在POM中(pom.xml)

个人的profile

 -定义在maven-setting中,(%USER_HOME%/.m2/.settings.xml)

全局的profile

 -定义在全局的maven-setting中(%M2_HOME%/conf/settings.xml)

profile的描述

 -一个profile的描述定义在项目的基本目录中profiles.xml(maven3并不支持)

怎么触发profile,通过正在使用的配置文件类型怎么造成差异?

profile的触发方式

  • 显示的
  • 通过maven的设置
  • 基于环境变量
  • 系统设置
  • 文件的判断

profile触发的详解

profile能够通过-f命令明确的指定。

这个命令可以用逗号分割profile的ID

mvn groupId:artifactId:goal -P profile-1,profile-2

profile能够在setting中指定被激活,通过activeProfiles元素包含activeProfile子元素来指定要被激活的profile的ID

<settings>
  ...
  <activeProfiles>
    <activeProfile>profile-1</activeProfile>
  </activeProfiles>
  ...
</settings>

那么何时激活这些profile,那么通过<activation>就可以激活,例如基于JDK的环境。

<profiles>
  <profile>
    <activation>
      <jdk>1.4</jdk>
    </activation>
    ...
  </profile>
</profiles>

要配置范围的可以使用

<profiles>
  <profile>
    <activation>
      <jdk>[1.3,1.6)</jdk>
    </activation>
    ...
  </profile>
</profiles>

基于操作系统

<profiles>
  <profile>
    <activation>
      <os>
        <name>Windows XP</name>
        <family>Windows</family>
        <arch>x86</arch>
        <version>5.1.2600</version>
      </os>
    </activation>
    ...
  </profile>
</profiles>

基于系统debug属性

<profiles>
  <profile>
    <activation>
      <os>
        <name>Windows XP</name>
        <family>Windows</family>
        <arch>x86</arch>
        <version>5.1.2600</version>
      </os>
    </activation>
    ...
  </profile>
</profiles>

基于环境变量为test

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

在命令行指定激活

mvn groupId:artifactId:goal -Denvironment=test

文件丢失时激活的profile

<profiles>
  <profile>
    <activation>
      <file>
        <missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
      </file>
    </activation>
    ...
  </profile>
</profiles>

默认激活的列表

<profiles>
  <profile>
    <id>profile-1</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    ...
  </profile>
</profiles>

如果想不激活profile,那么可以在命令行输入

mvn groupId:artifactId:goal -P !profile-1,!profile-2

哪些领域可以定制profile的各种类型,为什么?

在哪儿可以定义profile,和怎样激活他们,这对于我们应该在profile中定义什么是很有用的。这个问题其实并不很简单。

根据你选择的不同的profile,你将得到不同的POM选项。

profile可以在外部文件定义,但这样对于移植性并不有利。

profiles定义在POM中是更加合理的,而且更加利于移植。但是,这样的话,profile仅仅能够运用在项目中和项目的子模块中。

在POM中的应用

  • <repositories>
  • <pluginRepositories>
  • <dependencies>
  • <plugins>
  • <properties> (在主要的POM中无效,但是在后面可以使用)
  • <modules>
  • <reporting>
  • <dependencyManagement>
  • <distributionManagement>
  •  <build> 的子集,由以下元素构成:
    • <defaultGoal>
    • <resources>
    • <testResources>
    • <finalName>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值