对Maven生命周期和插件的一些总结

前言

Maven使用了很久,但是对maven的插件确是理解不深刻,作一篇文章来记录一下,文章分三部分
1.maven的生命周期
2.插件详解
3.插件配置

生命周期

在这里插入图片描述

  • 认识生命周期
      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阶段。三套生命周期是互相独立的,每种生命周期的阶段是前后依赖的。执行某个阶段,则会先依次执行该生命周期的前面阶段。

  • 命令行执行生命周期
    1.mvn clean 仅执行clean生命周期的pre-clean和clean阶段
    2.mvn test   执行default生命周期的validate、compile、test阶段
    3.mvn clean package  执行clean生命周期的pre-clean和clean阶段以及default生命周期的validate、compile、test、package阶段
    4.mvn clean install site-deploy  执行三种生命周期

插件详解

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

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

   spring-boot-maven-plugin
   
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 使用Maven插件直接将应用打包为一个Docker镜像 -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                    <repository>${
    
    docker.image.prefix}/${
    
    docker.image.midfix}/${
    
    project.artifactId}</repository>
                    <tag>${
    
    project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>${
    
    project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                    <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                </configuration>
                <!-- 镜像构建完毕之后自动推送到仓库 -->
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

自定义插件 dockerfile-maven-plugin ,在install阶段的目标是build和push

在这里插入图片描述

插件配置

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>

猜你喜欢

转载自blog.csdn.net/Tomcat_king/article/details/121682604