Maven自定义插件简要流程

本文介绍了如何创建并使用Maven自定义插件的简单步骤:新建Maven工程,设置POM打包类型,编写插件代码,然后在其他工程中引入并配置执行阶段和目标,最后进行验证测试。

AI 驱动代码审查实战

Claude code-review 插件深度解析,把 AI 智能审查接进 CI/CD 流水线

1. 新建一个Maven工程;
2. 修改POM打包方式为packing:
<?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>me.richard</groupId>
<artifactId>maven-plugin-suffix</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
</project>

3. 编写代码:

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;

@Mojo(name = "suffix",defaultPhase = LifecyclePhase.PACKAGE)
public class MavenPlginSuffix extends AbstractMojo {
    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println("This is zhaoy's first test plugin.");
    }
} 

4. 在需要使用的工程中引入,并指定对应的phase和goal:

<build>
    <finalName>learnjava</finalName>
    <plugins>
      <plugin>
        <groupId>me.richard</groupId>
        <artifactId>maven-plugin-suffix</artifactId>
        <version>1.0-SNAPSHOT</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>suffix</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

5. 验证测试:

D:\ideaspace\learnjava>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building learnjava Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ learnjava ---
[WARNING] Using platform encoding (GBK 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) @ learnjava ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ learnjava ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\ideaspace\learnjava\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ learnjava ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ learnjava ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ learnjava ---
[INFO]
[INFO] --- maven-plugin-suffix:1.0-SNAPSHOT:suffix (default) @ learnjava ---
This is zhaoy's first test plugin.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.690 s
[INFO] Finished at: 2018-01-25T22:25:43+08:00
[INFO] Final Memory: 11M/167M
[INFO] ------------------------------------------------------------------------

是不是很简单呢。

AI 驱动代码审查实战

Claude code-review 插件深度解析,把 AI 智能审查接进 CI/CD 流水线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值