信步漫谈之Maven——生命周期和插件


1 maven 的三大生命周期

maven 有三个标准的生命周期,每个生命周期中都包含着一系列的阶段( phase )。这些 phase 就相当于 Maven 提供的统一的接口。
Maven的生命周期是抽象的,其中的具体任务都交由插件来完成。这些 phase 的实现由 Maven 的插件来完成。
每个生命周期的阶段是有顺序的,后面的阶段依赖于前面的阶段。在一个生命周期中,运行某个阶段的时候,它之前的所有阶段都会被运行,也就是说,mvn clean 等同于 mvn pre-clean clean ,如果我们运行 mvn post-clean ,那么 pre-clean,clean 都会被运行。这是Maven很重要的一个规则,可以大大简化命令行的输入。
三大生命周期是相互独立的,一个生命周期的执行不会触发另外两个生命周期中的 phase。

  • clean 生命周期:清理项目,包含三个 phase。
    • pre-clean:执行清理前需要完成的工作
    • clean:清理上一次构建生成的文件
    • post-clean:执行清理后需要完成的工作
  • default(或 build) 生命周期:构建项目,重要的 phase 如下。
    • validate:验证工程是否正确,所有需要的资源是否可用。
    • compile:编译项目的源代码。
    • test:使用合适的单元测试框架来测试已编译的源代码。这些测试不需要已打包和布署。
    • package:把已编译的代码打包成可发布的格式,比如jar。
    • integration-test:如有需要,将包处理和发布到一个能够进行集成测试的环境。
    • verify:运行所有检查,验证包是否有效且达到质量标准。
    • install:把包安装到maven本地仓库,可以被其他工程作为依赖来使用。
    • deploy:在集成或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。
  • site生命周期:建立和发布项目站点,phase如下
    • pre-site:生成项目站点之前需要完成的工作
    • site:生成项目站点文档
    • post-site:生成项目站点之后需要完成的工作
    • site-deploy:将项目站点发布到服务器

2 maven 内置变量

  • ${basedir}表示项目根目录,即包含pom.xml文件的目录;
  • ${version}表示项目版本;
  • ${project.basedir}同${basedir};
  • ${project.baseUri}表示项目文件地址;
  • ${maven.build.timestamp}表示项目构件开始时间;
  • ${maven.build.timestamp.format}表示属性${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm,可自定义其格式,其类型可参考java.text.SimpleDateFormat。
  • ${project.build.directory}表示主源码路径;
  • ${project.build.sourceEncoding}表示主源码的编码格式;
  • ${project.build.sourceDirectory}表示主源码路径;
  • ${project.build.finalName}表示输出文件名称;
  • ${project.version}表示项目版本,与${version}相同;
  • ${project.xxx} 当前pom文件的任意节点的内容
  • ${env.xxx} 获取系统环境变量。
  • ${settings.xxx} 指代了settings.xml中对应元素的值。

3 maven 的插件

官网插件列表
我们在输入 mvn 命令的时候 比如 mvn clean,clean 对应的就是 Clean 生命周期中的 clean 阶段。但是 clean 的具体操作是由 maven-clean-plugin 来实现的。
所以说 Maven 生命周期的每一个阶段的具体实现都是由 Maven 插件实现的。
Maven 实际上是一个依赖插件执行的框架,每个任务实际上是由插件完成。
插件通常提供了一个目标的集合,并且可以使用下面的语法执行:

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

Maven的核心文件很小,主要的任务都是由插件来完成。定位到:%本地仓库%orgapachemavenplugins,可以看到一些下载好的插件。

4 插件仓库

<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <updatePolicy>never</updatePolicy>
        </releases>
    </pluginRepository>
</pluginRepositories>

5 插件目标

一个插件通常可以完成多个任务,每一个任务就叫做插件的一个目标(goal)。如执行mvn install命令时,调用的插件和执行的插件目标如下,例如 maven-compiler-plugin:3.1:compile 之后的 compile 即为插件目标。

[INFO] ------------------------------------------------------------------------
[INFO] Building maven-demo01 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-demo01 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-demo01 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-demo01 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory XXXXXXXX
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-demo01 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-demo01 ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-demo01 ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-demo01 ---

Maven对一些生命周期的阶段(phase)默认绑定了插件目标,通过 maven 的目标前后缀写法,目标前缀是插件的简易别名,目标后缀是插件的工作目标(工作任务),是配置和使用插件的一种简化方式。官网插件前缀介绍

名称 目标前后缀写法
process-test-resources resources:testResources
test-compile compiler:testCompile
compile compile:compile
install install:install
test surefire:test

6 自定义绑定

用户可以根据需要将任何插件目标绑定到任何生命周期的阶段,如:将 maven-source-plugin 的 jar-no-fork 目标绑定到 default 生命周期的 package 阶段,这样,以后在执行 mvn package 命令打包项目时,在 package 阶段之后会执行源代码打包,生成如:ehcache-core-2.5.0-sources.jar 形式的源码包。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>attach-source</id>
                    <!-- 要绑定到的生命周期的阶段 -->
                    <phase>package</phase>
                    <goals>
                    	<!-- 要绑定的插件的目标 -->
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

7 插件配置

Maven插件高度易扩展,可以方便的进行自定义配置(例如配置 maven-compiler-plugin 插件编译源代码的 JDK 版本为 1.7 等),插件的配置方式可参考官网的说明。官网插件配置说明

8 maven 常用插件

  • maven-clean-plugin
    作用:这个插件的主要作用就是清理构建目录下得全部内容,构建目录默认是target。
<plugin>
	<artifactId>maven-clean-plugin</artifactId>
	<version>3.1.0</version>
	<executions>
		<execution>
			<id>auto-clean</id>
			<phase>initialize</phase>
			<goals>
				<goal>clean</goal>
			</goals>
		</execution>
	</executions>
</plugin>
  • maven-compiler-plugin
    作用:设置maven编译的jdk版本,maven3默认用jdk1.5,maven2默认用jdk1.3
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <!-- 源代码使用的JDK版本 -->
        <source>1.8</source>
        <!-- 需要生成的目标class文件的编译版本 -->
        <target>1.8</target>
        <!-- 字符集编码 -->
        <encoding>UTF-8</encoding>
        <!-- 跳过测试 -->
        <skipTests>true</skipTests>
    </configuration>
</plugin>
  • maven-jar-plugin
    作用:打包jar文件时,配置manifest文件,加入lib包的jar依赖
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <!-- 存档 -->
        <archive>
            <!-- 添加maven 描述 -->
            <addMavenDescriptor/>
            <!-- 压缩 -->
            <compress/>
            <forced/>
            <index/>
            <!-- 配置清单(MANIFEST)-->
            <manifest>
                <!-- 添加到classpath 开关 -->
                <addClasspath/>
                <addDefaultImplementationEntries/>
                <addDefaultSpecificationEntries/>
                <addExtensions/>
                <classpathLayoutType/>
                <classpathMavenRepositoryLayout/>
                <!-- classpath 前缀 -->
                <classpathPrefix/>
                <customClasspathLayout/>
                <!-- 程序主函数入口 -->
                <mainClass/>
                <!-- 打包名称 -->
                <packageName/>
                <!-- 使用唯一版本 -->
                <useUniqueVersions/>
            </manifest>
            <!-- 配置清单(MANIFEST)属性 -->
            <manifestEntries>
                <key>value</key>
            </manifestEntries>
            <!-- MANIFEST 文件位置 -->
            <manifestFile/>
            <manifestSections>
                <manifestSection>
                    <name/>
                    <manifestEntries>
                        <key>value</key>
                    </manifestEntries>
                </manifestSection>
            </manifestSections>
            <pomPropertiesFile/>
        </archive>
        <!-- 过滤掉不希望包含在jar中的文件  -->
        <excludes>
            <exclude/>
        </excludes>
        <!-- 添加文件到jar中的文件  -->
        <includes>
            <include/>
        </includes>
    </configuration>
</plugin>
  • maven-dependency-plugin
    作用:自动拷贝依赖的jar包到target目录
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>compile</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <!-- ${project.build.directory}为Maven内置变量,缺省为target -->
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <!-- 表示是否不包含间接依赖的包 -->
                <excludeTransitive>false</excludeTransitive>
                <!-- 表示复制的jar文件去掉版本信息 -->
                <stripVersion>true</stripVersion>
            </configuration>
        </execution>
    </executions>
</plugin>
  • maven-antrun-plugin
    作用:可以在Maven执行时,额外执行Ant脚本。
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <phase>
                <!-- a lifecycle phase -->
            </phase>
            <configuration>
                <target>
                    <!--
					Place any Ant task here. You can add anything
					you can add between <target> and </target> in a
					build.xml.
					-->
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • maven-source-plugin
    作用:打包源码
    注意:在多项目构建中,将source-plugin置于顶层或parent的pom中并不会发挥作用,必须置于具体项目的pom中。
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • maven-resource-plugin
    作用:该插件处理项目的资源文件拷贝到输出目录。可以分别处理 main resources 和 test resources。
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.1</version>
    <configuration>
        <encoding>UTF-8</encoding>
     </configuration>
</plugin>
  • maven-assembly-plugin
    作用:该插件允许用户整合项目的输出,包括依赖、模块、网站文档和其他文档到一个单独的文档,即可用定制化打包。
    创建的文档格式包括:zip,tar,tar.gz(tgz),gar.bz2(tbgz2),jar,dir,war 等等。四种预定义的描述器可用:bin,jar-with-dependencies,src,project。
<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>3.0.0</version>
	<configuration>
		<descriptorRefs>
		<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id> <!-- this is used for inheritance merges -->
			<phase>package</phase> <!-- bind to the packaging phase -->
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

9 参考资料(感谢)

maven-dependency-plugin插件的使用
Maven常用插件整理
maven生命周期理解

原文地址:https://www.cnblogs.com/alfredinchange/p/14017888.html