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

1067

被折叠的 条评论
为什么被折叠?



