Welcome everyone

maven Plugins 和 Archetype

java 汪明鑫 664浏览 0评论

maven插件

Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成。

 

Maven is – at its heart – a plugin execution framework; all work is done by plugins.

 

There are the build and the reporting plugins:

  • Build plugins will be executed during the build and they should be configured in the <build/> element from the POM.
  • Reporting plugins will be executed during the site generation and they should be configured in the <reporting/> element from the POM. Because the result of a Reporting plugin is part of the generated site, Reporting plugins should be both internationalized and localized.

 

 

 

通过maven插件编译、打包、测试

 

我们大多使用的就是<build> …<plugins> …<plugin>

插件也可以指定多个goal(目标)

 

插件通常提供了一个目标的集合,并且可以使用下面的语法执行:

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

例如,一个 Java 工程可以使用 maven-compiler-plugin 的 compile-goal 编译,使用以下命令:

mvn compiler:compile

 

 

 

 

maven生命周期是抽象的,其实际行为都由插件完成【模版方法模式】

功能聚集在一个插件里,每个功能就是一个插件目标

 

本地maven插件

maven插件:E:\maven\repository\org\apache\maven\plugins

 

maven官方插件列表

http://maven.apache.org/plugins/index.html

 

使用findbugs插件

findbugs

静态分析工具,发现一些bug

 

我们在maven项目中引入插件

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>3.0.5</version>
        <configuration>
          <!-- 设置分析工作的等级,可以为Min、Default和Max -->
          <effort>Low</effort>
          <!-- Low、Medium和High (Low最严格) High只扫描严重错误。建议用Medium-->
          <threshold>Medium</threshold>
          <failOnError>true</failOnError>
          <includeTests>true</includeTests>
        </configuration>
        <executions>
          <execution>
            <id>run-findbugs</id>
            <!-- 在package(也可设为compile) 阶段触发执行findbugs检查,比如执行 mvn clean package -->
            <phase>package</phase>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

 

 

在idea terminal执行  $ mvn clean package

找到代码中的bug和对应的行数,着实给力

 

 

查看bug详情

mvn findbugs:gui

 

会弹出一个新的窗口,显示bug详情

 

相信有了这个例子,你已经感受到插件的牛逼之处了,即插即用,灵活扩展

 

插件前缀解析

比如 mvn dependency:tree

怎么根据dependency前缀找到对应的插件

 

/Users/mac/.m2/repository/org/apache/maven/plugins/maven-metadata.xml

根据prefix 找到 artifactId

groupId(默认插件仓库的和自己配置的) + artifactId 得到release版version

 

完整流程如下:

 

自定义插件

我们也可以自己手动写一个简单的插件,并且使用他

 

创建一个maven项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>pers.wmx</groupId>
    <artifactId>first-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--1.这里packaging设置为maven-plugin-->
    <packaging>maven-plugin</packaging>

    <dependencies>
        <!--2.1.编写maven插件所需要的依赖 maven-plugin-api-->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.5.0</version>
        </dependency>
        <!--2.2.编写maven插件所需要的依赖 maven-plugin-annotations-->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

 

创建一个类:

package pers.wmx;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * @author wmx
 * @date 2019-12-04
 */
@Mojo(name = "xinyeMojo", defaultPhase = LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo {

    @Parameter
    private String msg;

    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println(msg);
    }
}

"xinyeMojo" 就是goal

 

执行 mvn clean install

打包到本地仓库,供本地其他项目使用

如果别人要使用,可发布到私服

 

在另一个maven项目中引用自己写的插件

<plugin>
        <groupId>pers.wmx</groupId>
        <artifactId>first-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <configuration>
          <msg>老子写的第一个maven插件</msg>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>
                xinyeMojo
              </goal>
            </goals>
          </execution>
        </executions>
      </plugin>

goal目标(功能)就是插件类上注解@Mojo(name = "xinyeMojo", defaultPhase = LifecyclePhase.PACKAGE)

这样就完成了生命周期和插件的绑定

 

我们运行 mvn clean package

执行到我们的插件,牛逼哇

 

自定义脚手架

 

我们创建一个maven项目时,可以选择脚手架,会帮我们自动生成一下东西,这样就比较方便

就类似前端脚手架,生成一些基础的文件

 

我们来一起自定义一个简单的脚手架,并在创建另一个maven项目时使用

创建一个maven项目

 

把你想要的东西放进去,类啊,依赖啊,配置文件啊等等

这里为了简便,我就加点类和依赖演示下

 

在terminal 执行  mvn archetype:create-from-project

 

进入生成的/target/generated-sources/archetype目录下

执行 mvn claen install

把脚手架安装在本地

 

使用脚手架创建项目

mvn archetype:generate -DarchetypeCatalog=local

 

这个命令执行目录要在我们放项目代码的目录下执行(我的目录/Users/mac/Documents/code),

如果直接在刚才的target目录下执行会报错

Unable to add module to the current project as it is not of packaging type ‘pom’ -> [Help 1]

 

 

MacBook-Pro:code mac$ pwd
/Users/mac/Documents/code
MacBook-Pro:code mac$ mvn archetype:generate -DarchetypeCatalog=local
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> pers.wmx.archetype:first-archetype-archetype (first-archetype-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Downloading from tb-mirror: http://mvnrepo.alibaba-inc.com/mvn/repository/pers/wmx/archetype/first-archetype-archetype/1.1-SNAPSHOT/maven-metadata.xml
Downloading from ele-mirror: http://maven.elenet.me/nexus/content/groups/public/pers/wmx/archetype/first-archetype-archetype/1.1-SNAPSHOT/maven-metadata.xml
Define value for property 'groupId': xinye.shuaiqi
Define value for property 'artifactId': wmx
Define value for property 'version' 1.0-SNAPSHOT: : 
Define value for property 'package' xinye.shuaiqi: : 
Confirm properties configuration:
groupId: xinye.shuaiqi
artifactId: wmx
version: 1.0-SNAPSHOT
package: xinye.shuaiqi
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: first-archetype-archetype:1.1-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: xinye.shuaiqi
[INFO] Parameter: artifactId, Value: wmx
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: xinye.shuaiqi
[INFO] Parameter: packageInPathFormat, Value: xinye/shuaiqi
[INFO] Parameter: package, Value: xinye.shuaiqi
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: xinye.shuaiqi
[INFO] Parameter: artifactId, Value: wmx
[WARNING] Don't override file /Users/mac/Documents/code/wmx/.idea/inspectionProfiles/Project_Default.xml
[INFO] Project created from Archetype in dir: /Users/mac/Documents/code/wmx
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  14.238 s
[INFO] Finished at: 2019-12-04T18:14:25+08:00
[INFO] ------------------------------------------------------------------------

我们利用脚手架创建的项目就成功了

 

 

生成的项目和我们的脚手架模版长得一样

转载请注明:汪明鑫的个人博客 » maven Plugins 和 Archetype

喜欢 (0)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz