Maven 构建配置文件

构建配置文件是一系列的配置项的值,可以用来设置或者覆盖 Maven 构建默认值。

使用构建配置文件,你可以为不同的环境,比如说生产环境(Production)和开发(Development)环境,定制构建方式。

配置文件在 pom.xml 文件中使用 activeProfiles 或者 profiles 元素指定,并且可以通过各种方式触发。配置文件在构建时修改 POM,并且用来给参数设定不同的目标环境(比如说,开发(Development)、测试(Testing)和生产环境(Production)中数据库服务器的地址)。


构建配置文件的类型

构建配置文件大体上有三种类型:

类型 在哪定义
项目级(Per Project) 定义在项目的POM文件pom.xml中
用户级 (Per User) 定义在Maven的设置xml文件中 (%USER_HOME%/.m2/settings.xml)
全局(Global) 定义在 Maven 全局的设置 xml 文件中 (%M2_HOME%/conf/settings.xml)

配置文件激活

Maven的构建配置文件可以通过多种方式激活。

  • 使用命令控制台输入显式激活。
  • 通过 maven 设置。
  • 基于环境变量(用户或者系统变量)。
  • 操作系统设置(比如说,Windows系列)。
  • 文件的存在或者缺失。

配置文件激活实例

假定项目结构如下:

其中在src/main/resources文件夹下有三个用于测试文件:

文件名 描述
env.properties 如果未指定配置文件时默认使用的配置。
env.test.properties 当测试配置文件使用时的测试配置。
env.prod.properties 当生产配置文件使用时的生产配置。

注意:这三个配置文件并不是代表构建配置文件的功能,而是用于本次测试的目的;比如,我指定了构建配置文件为 prod 时,项目就使用 env.prod.properties文件。

注意:下面的例子仍然是使用 AntRun 插件,因为此插件能绑定 Maven 生命周期阶段,并通过 Ant 的标签不用编写一点代码即可输出信息、复制文件等,经此而已。其余的与本次构建配置文件无关。

1、配置文件激活

profile 可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个 profile,然后每个 profile 对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。

以下实例,我们将 maven-antrun-plugin:run 目标添加到测试阶段中。这样我们可以在不同的 profile 中输出文本信息。我们将使用 pom.xml 来定义不同的 profile,并在命令控制台中使用 maven 命令激活 profile。

pom.xml 文件如下:

<project xmlns="" xmlns:xsi=""  xsi:schemaLocation=" ">  <modelVersion>4.0.0</modelVersion>  <groupId>com.jsoft.test</groupId>  <artifactId>testproject</artifactId>  <packaging>jar</packaging>  <version>0.1-SNAPSHOT</version>  <name>testproject</name>  <url></url>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>  </dependencies>  <profiles>      <profile>          <id>test</id>          <build>              <plugins>                 <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <version>1.8</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.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>                          </tasks>                          </configuration>                       </execution>                    </executions>                 </plugin>              </plugins>          </build>      </profile>      <profile>          <id>normal</id>          <build>              <plugins>                 <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <version>1.8</version>                    <executions>                       <execution>                          <phase>test</phase>                          <goals>                             <goal>run</goal>                          </goals>                          <configuration>                          <tasks>                             <echo>Using env.properties</echo>                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>                          </tasks>                          </configuration>                       </execution>                    </executions>                 </plugin>              </plugins>          </build>      </profile>      <profile>          <id>prod</id>          <build>              <plugins>                 <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <version>1.8</version>                    <executions>                       <execution>                          <phase>test</phase>                          <goals>                             <goal>run</goal>                          </goals>                          <configuration>                          <tasks>                             <echo>Using env.prod.properties</echo>                             <copyfile="src/main/resources/env.prod.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>                          </tasks>                          </configuration>                       </execution>                    </executions>                 </plugin>              </plugins>          </build>      </profile>   </profiles></project>

注意:构建配置文件采用的是 <profiles> 节点。

说明:上面新建了三个 <profiles>,其中 <id> 区分了不同的 <profiles> 执行不同的 AntRun 任务;而 AntRun 的任务可以这么理解,AntRun 监听 test 的 Maven 生命周期阶段,当 Maven 执行 test 时,就触发了 AntRun 的任务,任务里面为输出文本并复制文件到指定的位置;而至于要执行哪个 AntRun 任务,此时构建配置文件起到了传输指定的作用,比如,通过命令行参数输入指定的 <id>

执行命令:

mvn test -Ptest

提示:第一个 test 为 Maven 生命周期阶段,第 2 个 test 为构建配置文件指定的 <id> 参数,这个参数通过 -P 来传输,当然,它可以是 prod 或者 normal 这些由你定义的<id>

运行的结果如下:

可以看出成功的触发了AntRun的任务。并且是对应构建配置文件下的 <id> 为 test 的任务。

再测试其余两个命令,结果如下:

2、通过Maven设置激活配置文件

打开 %USER_HOME%/.m2 目录下的 settings.xml 文件,其中 %USER_HOME% 代表用户主目录。如果 setting.xml 文件不存在就直接拷贝 %M2_HOME%/conf/settings.xml 到 .m2 目录,其中 %M2_HOME% 代表 Maven 的安装目录。

配置 setting.xml 文件,增加 <activeProfiles>属性:

<settings xmlns=""   xmlns:xsi=""   xsi:schemaLocation="   /xsd/settings-1.0.0.xsd">   ...   <activeProfiles>      <activeProfile>test</activeProfile>   </activeProfiles></settings>

执行命令:

mvn test

提示 1:此时不需要使用 -Ptest 来输入参数了,上面的 setting.xml 文件的 <activeprofile> 已经指定了 test 参数代替了。

提示 2:同样可以使用在 %M2_HOME%/conf/settings.xml 的文件进行配置,效果一致。

执行结果:

3、通过环境变量激活配置文件

先把上一步测试的 setting.xml 值全部去掉。

然后在 pom.xml 里面的 <id> 为 test 的 <profile> 节点,加入 <activation> 节点:

<project xmlns="" xmlns:xsi=""  xsi:schemaLocation=" ">  <modelVersion>4.0.0</modelVersion>  <groupId>com.jsoft.test</groupId>  <artifactId>testproject</artifactId>  <packaging>jar</packaging>  <version>0.1-SNAPSHOT</version>  <name>testproject</name>  <url></url>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>  </dependencies>  <profiles>      <profile>          <id>test</id>          <activation>            <property>               <name>env</name>               <value>test</value>            </property>          </activation>          <build>              <plugins>                 <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <version>1.8</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.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>                          </tasks>                          </configuration>                       </execution>                    </executions>                 </plugin>              </plugins>          </build>      </profile>      <profile>          <id>normal</id>          <build>              <plugins>                 <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <version>1.8</version>                    <executions>                       <execution>                          <phase>test</phase>                          <goals>                             <goal>run</goal>                          </goals>                          <configuration>                          <tasks>                             <echo>Using env.properties</echo>                             <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>                          </tasks>                          </configuration>                       </execution>                    </executions>                 </plugin>              </plugins>          </build>      </profile>      <profile>          <id>prod</id>          <build>              <plugins>                 <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <version>1.8</version>                    <executions>                       <execution>                          <phase>test</phase>                          <goals>                             <goal>run</goal>                          </goals>                          <configuration>                          <tasks>                             <echo>Using env.prod.properties</echo>                             <copyfile="src/main/resources/env.prod.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>                          </tasks>                          </configuration>                       </execution>                    </executions>                 </plugin>              </plugins>          </build>      </profile>   </profiles></project>

执行命令:

mvn test -Denv=test

提示 1:上面使用 -D 传递环境变量,其中 env 对应刚才设置的 <name> 值,test 对应<value>。

提示 2:在 Windows 10 上测试了系统的环境变量,但是不生效,所以,只能通过 -D 传递。

执行结果:

4、通过操作系统激活配置文件

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 的结果。

mvn test

5、通过文件的存在或者缺失激活配置文件

现在使用 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 的结果。

mvn test
 

【第五篇】-Maven 构建配置文件之Spring Cloud直播商城 b2b2c电子商务技术总结的更多相关文章

  1. [第五篇]——Docker 镜像加速之Spring Cloud直播商城 b2b2c电子商务技术总结

    Docker 镜像加速 国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器.Docker 官方和国内很多云服务商都提供了国内加速器服务,例如: 科大镜像: 网易: 阿里云: 你 ...

  2. 【第十五篇】- Maven 依赖管理之Spring Cloud直播商城 b2b2c电子商务技术总结

    Maven 依赖管理 Maven 一个核心的特性就是依赖管理.当我们处理多模块的项目(包含成百上千个模块或者子项目),模块间的依赖关系就变得非常复杂,管理也变得很困难.针对此种情形,Maven 提供了 ...

  3. 【第十七篇】- Maven Web 应用之Spring Cloud直播商城 b2b2c电子商务技术总结

    Maven Web 应用 本章节我们将学习如何使用版本控制系统 Maven 来管理一个基于 web 的项目,如何创建.构建.部署已经运行一个 web 应用. 创建 Web 应用 我们可以使用 mave ...

  4. 【第一篇】- Maven 系列教程之Spring Cloud直播商城 b2b2c电子商务技术总结

    Maven 教程 Maven 翻译为"专家"."内行",是 Apache 下的一个纯 Java 开发的开源项目.基于项目对象模型(缩写:POM)概念,Maven ...

  5. 【第二篇】- Maven 环境配置之Spring Cloud直播商城 b2b2c电子商务技术总结

    Maven 环境配置 Maven 是一个基于 Java 的工具,所以要做的第一件事情就是安装 JDK. 如果你还未安装 JDK,可以参考我们的 Java 开发环境配置. 系统要求 项目 要求 JDK ...

  6. [第九篇]——Docker 镜像使用之Spring Cloud直播商城 b2b2c电子商务技术总结

    Docker 镜像使用 当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载. 下面我们来学习: 1.管理 ...

  7. [第八篇]——Docker 容器使用之Spring Cloud直播商城 b2b2c电子商务技术总结

    Docker 客户端 docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项. xxx@xxx:~# docker 可以通过命令  docke ...

  8. [第三篇]——CentOS Docker 安装之Spring Cloud直播商城 b2b2c电子商务技术总结

    CentOS Docker 安装 Docker 支持以下的 64 位 CentOS 版本: CentOS 7 CentOS 8 更高版本... 使用官方安装脚本自动安装 安装命令如下: curl -f ...

  9. [第十六篇]——Docker 安装 CentOS之Spring Cloud直播商城 b2b2c电子商务技术总结

    Docker 安装 CentOS CentOS(Community Enterprise Operating System)是 Linux 发行版之一,它是来自于 Red Hat Enterprise ...

随机推荐

  1. 从Python到Go:初学笔记

    本文记录了我在学习Go的过程时的一些笔记,主要是比较Python和Go之间的差异并作简单描述,以此使Python程序员对Go语言的特性有简略的了解.初学难免有纰漏,欢迎各位批评指正补充交流,谢谢. 数 ...

  2. Bugku-你必须让他停下来

    这道题使用burpsuite抓包就能做,一开始抓包发到repeater之后flag不一定可以直接得到,原因是flag藏在特定的那张图片后面,我们一直go到那张图片便可以得到flag. 进入题目给的网址 ...

  3. Python语法之用户交互与基本运算符

    一.与用户交互 1.接收用户输入 python3中的input会把用户输入的所有内容都存成str类型 age = input("请输入您的年龄: ") # "内容&quo ...

  4. Android程序员提加薪被拒,刷2000题跳槽涨薪50%!

    为什么想跳槽? 简单说一下当时的状况,我在这家公司做了两年多,这两年多完成了一个大项目,作为开发的核心主力,开发压力很大,特别是项目上线前的几个月是非常辛苦,几乎每晚都要加班到12点以后,周末最多只有 ...

  5. 程序员被老板要求两个月做个APP,要不比京东差,网友:做一个快捷方式,直接链到京东

    隔行如隔山,这句话说得一点都没错.做一个程序员,很多人都会羡慕,也有很多人会望而却步. 作为一个外行人,你别看程序员每天坐在电脑前敲敲键盘打打代码,以为很简单,其实啊也只有程序员自己明白,任何一个看似 ...

  6. 中高级Android大厂面试秘籍,为你保驾护航金三银四,直通大厂(上)

    前言 当下,正面临着近几年来的最严重的互联网寒冬,听得最多的一句话便是:相见于江湖~.缩减HC.裁员不绝于耳,大家都是人心惶惶,年前如此,年后想必肯定又是一场更为惨烈的江湖厮杀.但博主始终相信,寒冬之 ...

  7. 为什么npm install 经常失败

    Hello 您好,我是大粽子.深耕线上商城的攻城狮(程序员)一枚. 前言 这段时间真的是忙,最近能抽时间搞搞大家在自己环境中遇到的各种问题了,我呢就是见不得我的代码在你的电脑运行不起来的.就像姜子牙睡 ...

  8. remote: Support for password authentication was removed

    周末提交代码,把代码push到github上,控制台报了下面的错误: remote: Support for password authentication was removed on August ...

  9. Spring 学习笔记(3)Spring MVC

    一.什么是 MVC MVC 实际上就是一种设计模式 Model-View-Controller Model 模型其实就是数据,Dao,Bean 等等 View 视图就是所看到的东西,网页,JSP,展示 ...

  10. C#设计模式---迭代器模式(Iterator Pattern)

    一.目的 提供一种方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节. 二.定义 迭代器模式提供了一种方法访问一个聚合对象(理解为集合对象)中各个元素,而又无需暴露该对象的内部表示,这样既可 ...