zoukankan      html  css  js  c++  java
  • Maven插件开发

    #### 读者指南

    目的:理解Maven构建周期和目标,熟悉Mojo开发
    参考文档:
    Plugin概念 http://maven.apache.org/guides/introduction/introduction-to-plugins.html#
    First Mojo http://maven.apache.org/guides/plugin/guide-java-plugin-development.html# 

    #### Mojo是什么

    Maven plain Old Java Object,一个Mojo是一个执行目标。
    Mojo包含三部分内容,执行目标,绑定的阶段,可接收的参数。Mojo执行的环境是POM context。
    Mojo有三种形式:注解,Java类,Beanshell script

    #### 什么是Plug-ins

    Maven的plugins有两种,build plugins 和 reporting plugins。
    build plugins在pom中使用<build/>标签标示,在build生命周期中执行。
    reporting plugins在pom中使用<reporting/>标签标示,在site生命周期中执行。
    一个Plugins包含一到多个Mojo。

    #### 再次定义Maven

    Maven是一个插件管理和执行框架。官方原文如下:
    "Maven" is really just a core framework for a collection of Maven Plugins.

    #### Maven、Plugins、Mojo的关系

    Maven是Plugins框架,管理和执行Plugins
    一个Plugins包含多个相关的Mojo,一个Mojo有自己的执行目标,可以接受参数并绑定到某个阶段 Lifecycle phase。

    #### Maven Lifecycle

    Maven定义了三套生命周期:clean、default、site,每个生命周期都包含了一些阶段(phase)。三套生命周期相互独立,但各个生命周期中的phase却是有顺序的,且后面的phase依赖于前面的phase。执行某个phase时,其前面的phase会依顺序执行,但不会触发另外两套生命周期中的任何phase。
    * clean生命周期
    pre-clean :执行清理前的工作;
    clean :清理上一次构建生成的所有文件;
    post-clean :执行清理后的工作
    * default生命周期
    default生命周期是最核心的,它包含了构建项目时真正需要执行的所有步骤。
    validate
    initialize
    generate-sources
    process-sources
    generate-resources
    process-resources :复制和处理资源文件到target目录,准备打包;
    compile :编译项目的源代码;
    process-classes
    generate-test-sources
    process-test-sources
    generate-test-resources
    process-test-resources
    test-compile :编译测试源代码;
    process-test-classes
    test :运行测试代码;
    prepare-package
    package :打包成jar或者war或者其他格式的分发包;
    pre-integration-test
    integration-test
    post-integration-test
    verify
    install :将打好的包安装到本地仓库,供其他项目使用;
    deploy :将打好的包安装到远程仓库,供其他项目使用;
    * site生命周期
    pre-site
    site :生成项目的站点文档;
    post-site
    site-deploy :发布生成的站点文档

    #### Maven默认插件

    通过 mvn help:effective-pom查看
    ```
    <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>/Users/kyh/code/useplugin/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>/Users/kyh/code/useplugin/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>/Users/kyh/code/useplugin/target/site</outputDirectory>
    <reportPlugins>
    <reportPlugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-project-info-reports-plugin</artifactId>
    </reportPlugin>
    </reportPlugins>
    </configuration>
    </plugin>
    ```

    #### 执行mvn构建的两种方式

    指定一个生命周期阶段 例如:mvn post-clean
    指定目标 mvn <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>
    或者 <plugin-prefix>:<goal>)
    Maven插件命名规范是
    <yourplugin>-maven-plugin
    maven-<yourplugin>-plugin
    其中后者是apache官方使用的标准名称 
  • 相关阅读:
    MySQL复制表结构和内容到另一张表中的SQL
    Page Cache(页缓存)
    mmap 与 munmap
    Shenandoah 与 ZGC
    InfluxDB入门
    SparkSQL 疫情Demo练习
    CyclicBarrier 解读
    mysql存储过程
    Kibana7.3.2与ElasticSearch7.3.2的集成
    Greenplum简介
  • 原文地址:https://www.cnblogs.com/afraidToForget/p/12849983.html
Copyright © 2011-2022 走看看