Maven构建配置文件 仓库 插件

着重介绍如何测试环境 生产环境使用不同Profile来构建应用,Maven针对仓库如何利用的优先级是什么,另外介绍了一下maven中的插件机制,以及常见插件

构建配置文件

使用构建配置文件,可以为不同的环境定制构建过程,例如 Producation 和 Development 环境。

Profile 类型

  • Per Project 定义在工程 POM 文件 pom.xml 中
  • Per User 定义在 Maven 设置 xml 文件中 (%USER_HOME%/.m2/settings.xml)
  • Global 定义在 Maven 全局配置 xml 文件中 (%M2_HOME%/conf/settings.xml)

Profile激活

  • 显式使用命令控制台输入
  • 通过 maven 设置
  • 基于环境变量(用户 / 系统变量)
  • 操作系统配置(例如,Windows family)
  • 现存 / 缺失 文件

显式Profile激活

将使用 pom.xml 来定义不同的 Profile,并在命令控制台中使用 maven 命令激活 Profile。 -P 选项指定 Profile 的名称

mvn test -Ptest

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <profiles>
      <profile>
      <id>test</id>
      <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</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.propertiestofile="${project.build.outputDirectory}/env.properties"/>
                  </tasks>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
      </build>
      </profile>
   </profiles>
</project>

通过 Maven 设置激活 Profile

settings.xml

<settings xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
   <mirrors>
      <mirror>
         <id>maven.dev.snaponglobal.com</id>
         <name>Internal Artifactory Maven repository</name>
         <url>http://repo1.maven.org/maven2/</url>
         <mirrorOf>*</mirrorOf>
      </mirror>
   </mirrors>
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

通过操作系统激活Profile

activation 元素包含下面的操作系统信息。

<profile>
   <id>test</id>
   <activation>
      <os>
         <name>Windows XP</name>
         <family>Windows</family>
         <arch>x86</arch>
         <version>5.1.2600</version>
      </os>
   </activation>
</profile>

通过现存 / 缺失的文件激活 Profile

使用 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>

仓库

仓库是一个位置(place),例如目录,可以存储所有的工程 jar 文件、library jar 文件、插件或任何其他的工程指定的文件。

  • 本地(local)
  • 中央(central)
  • 远程(remote)

顺序 本地 中央 远程

本地仓库

当你运行一次 Maven 构建,Maven 会自动下载所有依赖的 jar 文件到本地仓库中。它避免了每次构建时都引用存放在远程机器上的依赖文件。
本地仓库默认被创建在 %USER_HOME% 目录下,要修改可以在setting.xml中

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>C:/MyLocalRepository</localRepository>
</settings>

中央仓库

http://search.maven.org/#browse
国内速度较慢

远程仓库

开发人员自己定制仓库,包含了所需要的代码库或者其他工程中用到的 jar 文件。

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <dependencies>
      <dependency>
         <groupId>com.companyname.common-lib</groupId>
         <artifactId>common-lib</artifactId>
         <version>1.0.0</version>
      </dependency>
   <dependencies>
   <repositories>
      <repository>
         <id>companyname.lib1</id>
         <url>http://download.companyname.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>companyname.lib2</id>
         <url>http://download.companyname.org/maven2/lib2</url>
      </repository>
   </repositories>
</project>

插件

常见的一些用途:
– 创建 jar 文件
– 创建 war 文件
– 编译代码文件
– 代码单元测试
– 创建工程文档
– 创建工程报告

mvn [plugin-name]:[goal-name]

mvn compiler:compile

插件类型

  • Build plugins 在构建时执行,并在 pom.xml 的 元素中配置。
  • Reporting plugins 在网站生成过程中执行,并在 pom.xml 的 元素中配置。

常用插件列表
– clean 构建之后清理目标文件。删除目标目录。
– compiler 编译 Java 源文件。
– surefile 运行 JUnit 单元测试。创建测试报告。
– jar 从当前工程中构建 JAR 文件。
– war 从当前工程中构建 WAR 文件。
– javadoc 为工程生成 Javadoc。
– antrun 从构建过程的任意一个阶段中运行一个 ant 任务的集合。

  • 插件是在 pom.xml 中使用 plugins 元素定义的。
  • 每个插件可以有多个目标。
  • 你可以定义阶段,插件会使用它的 phase 元素开始处理。
  • 你可以通过绑定到插件的目标的方式来配置要执行的任务。
0条留言