1. Apache Maven 概述
本文档将以一些例子来验证 Maven 的主要概念。在文档的开头由一个简单的 Maven 项目展开,然后慢慢展现一个与数据库交互、拥有远程 API 交互的多模块的企业级 Web 项目。
1.1 Maven 是什么?
Maven 是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。 当你使用Maven的时候,你用一个明确定义的项目对象模型来描述你的项目,然后 Maven 可以应用横切的逻辑,这些逻辑来自一组共享的(或者自定义的)插件。
1.2 比较 Maven 和 Ant
当然,Maven 是 Ant 的另一种选择,但是 Apache Ant8 继续是一个伟大的,被广泛使用的工具。它已经是多年以来 Java 构建的统治者,而你很容易的在你项目的 Maven 构建中集成 Ant 构建脚本。这是 Maven 项目一种很常见的使用模式。而另一方面,随着越来越多的开源项目转移到 Maven 用它作为项目管理平台,开发人员开始意识到 Maven 不仅仅简化了构建管理任务,它也帮助鼓励开发人员的软件项目使用通用的接口。Maven 不仅仅是一个工具,它更是一个平台,当你只是将 Maven 考虑成 Ant 的另一种选择的时候,你是在比较苹果和橘子。“Maven”包含了很多构建工具以外的东西。
使用 Maven 还是 Ant 的决定不是非此即彼的,Ant 在复杂的构建中还有它的位置。如果你目前的构建包含一些高度自定义的过程,或者你已经写了一些 Ant 脚本通过一种明确的方法完成一个明确的过程,而这种过程不适合 Maven 标准,你仍然可以在 Maven 中用这些脚本。作为一个 Maven 的核心插件, Ant 还是可用的。自定义的插件可以用 Ant 来实现,Maven 项目可以配置成在生命周期中运行 Ant 的脚本。
2. 安装和运行 Maven
2.1 下载
地址: http://maven.apache.org
2.2 安装
设置环境变量 M2_HOME,即安装包解压路径。然后,添加${M2_HOME}/bin 到 PATH 中。
2.3 验证

2.4 安装细节

bin/ : 包含了运行 Maven 的 mvn 脚本
boot/ : 包含了一个创建 Maven 运行所需要的类装载器的 JAR 文件 plexus-classworlds-2.5.2.jar
conf/ : 包含了一个全局的 settings.xml 文件,该文件用来自定义你机器上 Maven 的一些行为。如果你需要自定义 Maven,更通常的做法是覆写 ~/.m2 目录下的 settings.xml 文件,每个用户都有对应的这个目录。
lib/ : Maven 库文件
LICENSE : Apache Maven 软件许可证
NOTICE : 包含了一些 Maven 依赖的类库所需要的通告及权限
README.txt : 包含了一些安装指令
2.5 用户相关配置和仓库
~/.m2/settings.xml
该文件包含了用户相关的认证,仓库和其他信息。用来自定义 Maven 的行为。
~/.m2/repository/
本地仓库,当你从远程 Maven 仓库下载依赖的时候,Maven 在你本地仓库存储了这个依赖的一个副本。
2.6 获得 Maven 帮助
http://maven.apache.org
Maven 的 Web 站点,包含了丰富的文档。
http://www.sonatype.com
Sonatype 官网,包含了 Maven 的一些指南说明。
2.7 使用 Maven Help 插件
有些时候,你需要一个工具来帮助你理解一些 Maven 使用的模型,以及某个插件有什么可用的目标。Maven Help 插件能让你列出活动的 Maven Profile,显示一个实际 POM(effective POM),打印实际 settings(effective settings),或者列出 Maven 插件的属性。
Maven Help 插件有四个目标。前三个目标是:active-profiles, effective-pom 和effective-settings,描述一个特定的项目,它们必须在项目的目录下运行。 最后一个目标:describe,相对比较复杂,展示某个插件或者插件目标的相关信息。
help:active-profiles
列出当前构建中活动的 Profile(项目的,用户的,全局的)。
help:effective-pom
显示当前构建的实际 POM,包含活动的 Profile。
help:effective-settings
打印出项目的实际 settings, 包括从全局的 settings 和用户级别 settings 继承的配置。
help:describe
描述插件的属性。它不需要在项目目录下运行,但是你必须提供你想要描述插件的 groupId 和 artifactId。
2.7.1 描述一个 Maven 插件
一旦你开始使用 Maven,你会花很多时间去试图获得 Maven 插件的信息:插件如何工作?配置参数是什么?目标是什么? 你会经常使用 help:describe 目标来获取这些信息。通过 plugin 参数你可以指定你想要研究哪个插件,你可以传入插件的前缀(如 help 插件就是 maven-help-plugin),或者可以是 groupId:artifact[:version] ,这里 version 是可选的。比如, 下面的命令使用 help 插件的 describe 目标来输出 Maven Help 插件的信息。
命令窗口执行: mvn help:describe -Dplugin=help
输出信息如下:
| [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom --- [INFO] org.apache.maven.plugins:maven-help-plugin:2.2 Name: Maven Help Plugin Description: The Maven Help plugin provides goals aimed at helping to make sense out of the build environment. It includes the ability to view the effective POM and settings files, after inheritance and active profiles have been applied, as well as a describe a particular plugin goal to give usage information. Group Id: org.apache.maven.plugins Artifact Id: maven-help-plugin Version: 2.2 Goal Prefix: help This plugin has 9 goals: help:active-profiles Description: Displays a list of the profiles which are currently active for this build. help:all-profiles Description: Displays a list of available profiles under the current project. Note: it will list all profiles for a project. If a profile comes up with a status inactive then there might be a need to set profile activation switches/property. help:describe Description: Displays a list of the attributes for a Maven Plugin and/or goals (aka Mojo - Maven plain Old Java Object). help:effective-pom Description: Displays the effective POM as an XML for this build, with the active profiles factored in. help:effective-settings Description: Displays the calculated settings as XML for this project, given any profile enhancement and the inheritance of the global settings into the user-level settings. help:evaluate Description: Evaluates Maven expressions given by the user in an interactive mode. help:expressions Description: Displays the supported Plugin expressions used by Maven. help:help Description: Display help information on maven-help-plugin. Call mvn help:help -Ddetail=true -Dgoal=<goal-name> to display parameter details. help:system Description: Displays a list of the platform details like system properties and environment variables. For more information, run 'mvn help:describe [...] -Ddetail' [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.862 s [INFO] Finished at: 2017-07-19T10:06:36+08:00 [INFO] Final Memory: 10M/109M [INFO] ------------------------------------------------------------------------ |
通过设置 plugin 参数来运行 describe 目标,输出为该插件的 Maven 坐标,目标前缀,和该插件的一个简要介绍。尽管这些信息非常有帮助,你通常还是需要了解更多的详情。如果你想要 Help 插件输出完整的带有参数的目标列表,只要运行带有参数 full 的 help:describe 目标就可以了,像这样:
命令窗口执行:
mvn help:describe -Dplugin=help –Dfull
输出信息如下:
| [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom --- [INFO] org.apache.maven.plugins:maven-help-plugin:2.2 Name: Maven Help Plugin Description: The Maven Help plugin provides goals aimed at helping to make sense out of the build environment. It includes the ability to view the effective POM and settings files, after inheritance and active profiles have been applied, as well as a describe a particular plugin goal to give usage information. Group Id: org.apache.maven.plugins Artifact Id: maven-help-plugin Version: 2.2 Goal Prefix: help This plugin has 9 goals: help:active-profiles Description: Displays a list of the profiles which are currently active for this build. Implementation: org.apache.maven.plugins.help.ActiveProfilesMojo Language: java Available parameters: output User property: output Optional parameter to write the output of this help in a given file, instead of writing to the console. Note: Could be a relative path. help:all-profiles Description: Displays a list of available profiles under the current project. Note: it will list all profiles for a project. If a profile comes up with a status inactive then there might be a need to set profile activation switches/property. Implementation: org.apache.maven.plugins.help.AllProfilesMojo Language: java Available parameters: output User property: output Optional parameter to write the output of this help in a given file, instead of writing to the console. Note: Could be a relative path. help:describe Description: Displays a list of the attributes for a Maven Plugin and/or goals (aka Mojo - Maven plain Old Java Object). Implementation: org.apache.maven.plugins.help.DescribeMojo Language: java Available parameters: artifactId User property: artifactId The Maven Plugin artifactId to describe. Note: Should be used with groupId parameter. cmd User property: cmd A Maven command like a single goal or a single phase following the Maven command line: mvn [options] [<goal(s)>] [<phase(s)>] detail (Default: false) User property: detail This flag specifies that a detailed (verbose) list of goal (Mojo) information should be given. goal User property: goal The goal name of a Mojo to describe within the specified Maven Plugin. If this parameter is specified, only the corresponding goal (Mojo) will be described, rather than the whole Plugin. groupId User property: groupId The Maven Plugin groupId to describe. Note: Should be used with artifactId parameter. medium (Default: true) User property: medium This flag specifies that a medium list of goal (Mojo) information should be given. minimal (Default: false) User property: minimal This flag specifies that a minimal list of goal (Mojo) information should be given. output User property: output Optional parameter to write the output of this help in a given file, instead of writing to the console. Note: Could be a relative path. plugin User property: plugin The Maven Plugin to describe. This must be specified in one of three ways: 1. plugin-prefix, i.e. 'help' 2. groupId:artifactId, i.e. 'org.apache.maven.plugins:maven-help-plugin' 3. groupId:artifactId:version, i.e. 'org.apache.maven.plugins:maven-help-plugin:2.0' version User property: version The Maven Plugin version to describe. Note: Should be used with groupId/artifactId parameters. help:effective-pom Description: Displays the effective POM as an XML for this build, with the active profiles factored in. Implementation: org.apache.maven.plugins.help.EffectivePomMojo Language: java Available parameters: output User property: output Optional parameter to write the output of this help in a given file, instead of writing to the console. Note: Could be a relative path. help:effective-settings Description: Displays the calculated settings as XML for this project, given any profile enhancement and the inheritance of the global settings into the user-level settings. Implementation: org.apache.maven.plugins.help.EffectiveSettingsMojo Language: java Available parameters: output User property: output Optional parameter to write the output of this help in a given file, instead of writing to the console. Note: Could be a relative path. showPasswords (Default: false) User property: showPasswords For security reasons, all passwords are hidden by default. Set this to true to show all passwords. help:evaluate Description: Evaluates Maven expressions given by the user in an interactive mode. Implementation: org.apache.maven.plugins.help.EvaluateMojo Language: java Available parameters: artifact User property: artifact An artifact for evaluating Maven expressions. Note: Should respect the Maven format, i.e. groupId:artifactId[:version][:classifier]. expression User property: expression An expression to evaluate instead of prompting. Note that this must not include the surrounding ${...}. help:expressions Description: Displays the supported Plugin expressions used by Maven. Implementation: org.apache.maven.plugins.help.ExpressionsMojo Language: java Available parameters: output User property: output Optional parameter to write the output of this help in a given file, instead of writing to the console. Note: Could be a relative path. help:help Description: Display help information on maven-help-plugin. Call mvn help:help -Ddetail=true -Dgoal=<goal-name> to display parameter details. Implementation: org.apache.maven.plugins.help.HelpMojo Language: java Available parameters: detail (Default: false) User property: detail If true, display all settable properties for each goal. goal User property: goal The name of the goal for which to show help. If unspecified, all goals will be displayed. indentSize (Default: 2) User property: indentSize The number of spaces per indentation level, should be positive. lineLength (Default: 80) User property: lineLength The maximum length of a display line, should be positive. help:system Description: Displays a list of the platform details like system properties and environment variables. Implementation: org.apache.maven.plugins.help.SystemMojo Language: java Available parameters: output User property: output Optional parameter to write the output of this help in a given file, instead of writing to the console. Note: Could be a relative path. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.898 s [INFO] Finished at: 2017-07-19T10:18:51+08:00 [INFO] Final Memory: 10M/109M [INFO] ------------------------------------------------------------------------
|
该选项能让你查看插件所有的目标及相关参数。但是有时候这些信息显得太多了。这时候你可以获取单个目标的信息,设置 mojo 参数和 plugin 参数。下面的命令列出了 Compiler 插件的 compile 目标的所有信息:
mvn help:describe -Dplugin=compiler -Dmojo=compile –Dfull
说明:在 Maven 里面, 一个插件目标也被认为是一个 “Mojo”。
3. 一个简单的 Maven 项目
3.1 概述
本节使用 Maven archetype 插件创建一个简单的 Maven 项目
3.2 创建
命令窗口执行:mvn archetype:generate –DgroupId=org.sonatype.mavenbook –Dartifactid=simple –Dpackage=org.sonatype.mavenbook –Dversion=1.0-SNAPSHOT
输出信息如下:
…
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1007: Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-1 2: 1.0-alpha-2 3: 1.0-alpha-3 4: 1.0-alpha-4 5: 1.0 6: 1.1 Choose a number: 6: [INFO] Using property: groupId = org.sonatype.mavenbook [INFO] Using property: artifactId = simple [INFO] Using property: version = 1.0-SNAPSHOT [INFO] Using property: package = org.sonatype.mavenbook Confirm properties configuration: groupId: org.sonatype.mavenbook artifactId: simple version: 1.0-SNAPSHOT package: org.sonatype.mavenbook Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: C:\Users\Administrator [INFO] Parameter: package, Value: org.sonatype.mavenbook [INFO] Parameter: groupId, Value: org.sonatype.mavenbook [INFO] Parameter: artifactId, Value: simple [INFO] Parameter: packageName, Value: org.sonatype.mavenbook [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:\Users\Administrator\simple [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 09:36 min [INFO] Finished at: 2017-07-19T11:13:35+08:00 [INFO] Final Memory: 16M/156M [INFO] ------------------------------------------------------------------------
|
生成的 simple 项目目录如下:

Maven Archtype 插件创建了一个与 artifactId 匹配的目录:simple,这是项目的基础目录。
每个项目在文件 pom.xml 里有它的项目对象模型 (POM),这个文件描述了这个项目,配置的插件,声明的依赖。
我们项目的源码、资源文件被放在了 src/main 目录下面。在我们简单 Java 项目这样的情况下,这个目录包含了一下 java 类和一些配置文件。在其它的项目中,它可能是 web 应用的文档根目录,或者还放一些应用服务器的配置文件。在一个 Java 项目中,Java 类放在 src/main/java 下面,而 classpath 资源文件放在 src/main/resources 下面。
我们项目的测试用例放在 src/test 下。在这个目录下面,src/test/java 存放像使用 JUnit 或者 TestNG 这样的 Java 测试类。目录 src/test/resources 下存放测试 classpath 资源文件。
Maven Archtype 插件生成了一个简单的类 org.sonatype.mavenbook.App,它是一个仅有 13 行代码的 Java,所做的只是在 main 方法中输出一行消息:
package org.sonatype.mavenbook;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
3.3 发布
在 simple 项目的包含 pom.xml 目录下,运行命令 mvn install

同时,本地资源库会有:

验证程序:

4. 几个主要概念
4.1 项目对象模型(POM, Project Object Model)
当 Maven 运行的时候它向项目对象模型(POM)查看关于这个项目的信息。POM 回答类似这样的问题:这个项目是什么类型的?这个项目的名称是什么?这个项目的构建有自定义么?这里是一个由 Maven Archetype 插件的 create 目标创建的默认的 pom.xml 文件。
simple 项目的 pom.xml 文件:
这个 pom.xml 文件是你将会面对的Maven项目中最基础的 POM,一般来说一个 POM 文件会复杂得多:定义多个依赖,自定义插件行为。最开始的几个元素:groupId,artifactId, packaging, version,是 Maven 的坐标(coordinates),它们唯一标识了一个项目。name 和 url 是 POM 提供的描述性元素,它们给人提供了可阅读的名字,将一个项目关联到了项目 web 站点。最后,dependencies 元素定义了一个单独的,测试范围(test-scoped)依赖,依赖于称为 JUnit 的单元测试框架。
当 Maven 运行的时候,它是根据项目的 pom.xml 里设置的组合来运行的,一个最上级的 POM 定义了 Maven 的安装目录,在这个目录中全局的默认值被定义了,(可能)还有一些用户定义的设置。想要看这个“有效的 (effective)” POM,或者说 Maven 真正运行根据的 POM,在 simple 项目的基础目录下跑下面的命令:
mvn help:effective-pom
可以看到,结果信息很详细,暴露了 Maven 的默认位置。
|
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building simple 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:2.2:effective-pom (default-cli) @ simple --- [INFO] Effective POMs, after inheritance, interpolation, and profiles are applied:
<!-- ====================================================================== --> <!-- --> <!-- Generated by Maven Help Plugin on 2017-07-20T04:49:54 --> <!-- See: http://maven.apache.org/plugins/maven-help-plugin/ --> <!-- --> <!-- ====================================================================== -->
<!-- ====================================================================== --> <!-- --> <!-- Effective POM for project --> <!-- 'org.sonatype.mavenbook:simple:jar:1.0-SNAPSHOT' --> <!-- --> <!-- ====================================================================== -->
<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>org.sonatype.mavenbook</groupId> <artifactId>simple</artifactId> <version>1.0-SNAPSHOT</version> <name>simple</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </pluginRepository> </pluginRepositories> <build> <sourceDirectory>C:\Users\Administrator\simple\src\main\java</sourceDirectory> <scriptSourceDirectory>C:\Users\Administrator\simple\src\main\scripts</scriptSourceDirectory> <testSourceDirectory>C:\Users\Administrator\simple\src\test\java</testSourceDirectory> <outputDirectory>C:\Users\Administrator\simple\target\classes</outputDirectory> <testOutputDirectory>C:\Users\Administrator\simple\target\test-classes</testOutputDirectory> <resources> <resource> <directory>C:\Users\Administrator\simple\src\main\resources</directory> </resource> </resources> <testResources> <testResource> <directory>C:\Users\Administrator\simple\src\test\resources</directory> </testResource> </testResources> <directory>C:\Users\Administrator\simple\target</directory> <finalName>simple-1.0-SNAPSHOT</finalName> <pluginManagement> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.3.2</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>default-clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>default-testResources</id> <phase>process-test-resources</phase> <goals> <goal>testResources</goal> </goals> </execution> <execution> <id>default-resources</id> <phase>process-resources</phase> <goals> <goal>resources</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>default-testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <executions> <execution> <id>default-test</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-install</id> <phase>install</phase> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>default-deploy</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.3</version> <executions> <execution> <id>default-site</id> <phase>site</phase> <goals> <goal>site</goal> </goals> <configuration> <outputDirectory>C:\Users\Administrator\simple\target\site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> <execution> <id>default-deploy</id> <phase>site-deploy</phase> <goals> <goal>deploy</goal> </goals> <configuration> <outputDirectory>C:\Users\Administrator\simple\target\site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> </executions> <configuration> <outputDirectory>C:\Users\Administrator\simple\target\site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </plugin> </plugins> </build> <reporting> <outputDirectory>C:\Users\Administrator\simple\target\site</outputDirectory> </reporting> </project>
[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.804 s [INFO] Finished at: 2017-07-20T16:49:54+08:00 [INFO] Final Memory: 10M/114M [INFO] ------------------------------------------------------------------------
|
4.2 插件和目标(Plugins and Goals)

一个 Maven 插件是一个单个或者多个目标的集合。Maven 插件的例子有一些简单但核心的插件,像 Jar 插件,它包含了一组创建 JAR 文件的目标,Compiler 插件,它包含了一组编译源代码和测试代码的目标,或者 Surefire 插件,它包含一组运行单元测试和生成测试报告的目标。而其它的,更有专门的插件包括:Hibernate3 插件,用来集成流行的持久化框架 Hibernate,JRuby 插件,它让你能够让运行 ruby 称为 Maven 构建的一部分或者用 Ruby 来编写 Maven 插件。Maven 也提供了自定义插件的能力。一个定制的插件可以用 Java 编写,或者用一些其它的语言如 Ant,Groovy,beanshell 和之前提到的 Ruby。
当提到一个插件目标的时候,我们常常用速记符号:pluginId:goalId。例如,当提到 Archetype 插件的 create 目标的时候,我们写成 archetype:create。
目标定义了一些参数,这些参数可以定义一些明智的默认值。在 archetype:create 这个例子中,我们并没有在命令行中指定这个目标创建什么类型的 archetype,我们简单的传入一个 groupId 和一个 artifactId。这是我们对于约定优于配置(convention overconfiguration)的第一笔。这里 create 目标的约定,或者默认值,是创建一个简单的项目,叫做 Quickstart。create 目标定义了一个配置属性 archetypeArtifactId,它有一个默认值为 maven-archetype-quickstart。Quickstart archetype 生成了一个最小项目的躯壳,包括一个POM 和一个类。Archetype插件比第一个例子中的样子强大得多,但是这是一个快速开始新项目的不错的方法。
4.3 生命周期(LifeCycle)
生命周期是包含了一个项目构建中的一系列有序的阶段。
插件目标可以附着在生命周期阶段上。随着 Maven 沿着生命周期的阶段移动,它会执行附着在特定阶段上的目标。每个阶段可能绑定了零个或者多个目标。
Maven 默认的生命周期:

说明:这只是一部分展示
我们知道,在包类型为 jar 的项目中,打包阶段将会创建一个 JAR 文件。但是,在它之前的目标做什么呢,像 compiler:compile 和 surefire:test? 在 Maven 经过它生命周期中 package 之前的阶段的时候,这些目标被运行了;Maven 执行一个阶段的时候,它首先会有序的执行前面的所有阶段,到命令行指定的那个阶段为止。每个阶段对应了零个或者多个目标。我们没有进行任何插件配置或者定制,所以这个例子绑定了一组标准插件的目标到默认的生命周期。当 Maven 经过以 package 为结尾的默认生命周期的时候,下面的目标按顺序被执行:
resources:resources
Resources 插件的 resources 目标绑定到了 resources 阶段。这个目标复制 src/main/resources 下的所有资源和其它任何配置的资源目录,到输出目录。
compiler:compile
Compiler 插件的 compile 目标绑定到了 compile 阶段。这个目标编译 src/main/java 下的所有源代码和其他任何配置的资源目录,到输出目录。
resources:testResources
Resources 插件的 testResources 目标绑定到了 test-resources 阶段。这个目标复制 src/test/resources 下的所有资源和其它任何的配置的测试资源目录,到测试输出目录。
compiler:testCompile
Compiler 插件的 testCompile 目标绑定到了 test-compile 阶段。这个目标编译 src/test/java 下的测试用例和其它任何的配置的测试资源目录,到测试输出目录。
surefire:test
Surefire 插件的 test 目标绑定到了 test 阶段。这个目标运行所有的测试并且创建那些捕捉详细测试结果的输出文件。默认情况下,如果有测试失败,这个目标会终止。
jar:jar
Jar 插件的 jar 目标绑定到了 package 阶段。这个目标把输出目录打包成 JAR 文件。
