maven-生命周期与插件

Maven的生命周期是抽象的,具体的操作由插件实现,类似于java的模板设计模式。

1、生命周期

  • 认识生命周期

  maven有clean、default、site三种生命周期,每种生命周期都包含一些阶段,clean包含了pre-clean、clean、post-clean阶段;default生命周期包含了validate、compile、test、package、verify、install、deploy阶段;site生命周期包含了pre-site、site、post-site、site-deploy阶段。三套生命周期是互相独立的,每种生命周期的阶段是前后依赖的。执行某个阶段,则会先依次执行该生命周期的前面阶段。

  • 命令行执行生命周期

mvn clean  仅执行clean生命周期的pre-clean和clean阶段

mvn test    执行default生命周期的validate、compile、test阶段

mvn clean package  执行clean生命周期的pre-clean和clean阶段以及default生命周期的validate、compile、test、package阶段

mvn clean install site-deploy  执行三种生命周期

2、插件详解

  • 插件目标

   maven中插件是以构件的形式存在。一个插件能完成好几个功能,每个功能对应插件的一个目标。比如插件maven-compiler-plugin可以完成以下compile、testCompile目标

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:UserszhpliDesktopmyapp	argetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:UserszhpliDesktopmyappsrc	est
esources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:UserszhpliDesktopmyapp	arget	est-classes
[INFO]
  • 插件绑定

  maven生命周期与插件的某个目标绑定,执行具体的构建任务。比如compile生命周期与maven-compiler-plugin插件的compile目标绑定,执行编译任务。

a、maven内置绑定插件

b、自定义绑定插件

  用户可以自己配置某个插件的某个目标绑定生命周期的某个阶段,让maven在构建项目时执行更多富有特色的任务。比如创建项目的源码jar包,内置插件没有涉及这一任务。maven-source-plugin插件的jar-no-fork目标能够将项目的主代码打包成jar文件,可以将该目标绑定到default生命周期的verify阶段上,即在执行完成测试后和安装构件之前创建源码jar包。

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>
  • 插件配置

a、全局配置

  该插件使用java1.8编译,并生成jvm1.8兼容的字节码文件。maven-compiler-plugin插件已经绑定的生命周期的阶段均使用该配置。

<build>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.1</version>
        <configuration>   该插件的整体配置,各个目标均使用该配置
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
</build>

b、局部配置(任务配置)

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
              <id>ant-validate</id>
              <phase>validate</phase>
              <goals>
                <goal>run</goal>
              </goals>
              <configuration> 该插件目标的特有配置
                <tasks>
                  <echo>I am bound verify phase</echo>   输出信息
                </tasks>
              </configuration>
          </execution>
          <execution>
            <id>ant-verify</id>
            <phase>verify</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>  该插件目标的特有配置
              <tasks>
                <echo>I am bound verify phase</echo>  输出信息
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
 </build>
  • 插件解析机制

  与依赖构件一样,插件构件同样基于坐标存储在Maven仓库中。不同于依赖配置远程仓库使用的repotitories及其repository子元素,插件的远程仓库使用pluginRepositories和pluginRepositoryp配置,其他配置与依赖配置远程仓库一致

略略...

原文地址:https://www.cnblogs.com/shixiemayi/p/9533260.html