官方文档:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
<yourplugin>-maven-plugin
mvn archetype:generate -DgroupId=cn.lsj -DartifactId=hello-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo
不管使用哪种方式,都差不多,项目结构如下:
原本的maven-plugin-api
模板可能不是最新的,建议升级到较新的版本。
<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>cn.lsj</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.7</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
默认生成的MyMojo.java
文件无法直接跑起来,先改为如下:
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* Goal which touches a timestamp file.
*
* @goal touch
* @phase process-sources
*/
@Mojo(name = "hello") // 插件的全局名称
public class MyMojo extends AbstractMojo {
/**
* Location of the file.
*
* @parameter expression="${project.build.directory}"
* @required
*/
private File outputDirectory;
public void execute() throws MojoExecutionException {
getLog().info("插件生效");
// File f = outputDirectory;
//
// if (!f.exists()) {
// f.mkdirs();
// }
//
// File touch = new File(f, "touch.txt");
//
// FileWriter w = null;
// try {
// w = new FileWriter(touch);
//
// w.write("touch.txt");
// } catch (IOException e) {
// throw new MojoExecutionException("Error creating file " + touch, e);
// } finally {
// if (w != null) {
// try {
// w.close();
// } catch (IOException e) {
// // ignore
// }
// }
// }
}
}
进入pom.xml
所在的目录,执行命令。
// 格式: mvn groupId:artifactId:version:goalName
mvn cn.lsj:hello-maven-plugin:1.0-SNAPSHOT:hello -X
进入pom.xml
所在的目录,执行命令:mvn install
。将插件安装到本地仓库。
本地项目通过下面配置可以引入刚才安装的插件。在编译的时候,插件生效。
<build>
<plugins>
<plugin>
<groupId>cn.lsj</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<!-- 指定phase为compile值,表示在编译阶段插件生效。 -->
<phase>compile</phase>
<goals>
<!-- 插件全局名称 -->
<goal>hello</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[INFO] xxxxxxxxxxxxxxxxx
[INFO] --- hello-maven-plugin:1.0-SNAPSHOT:hello (default) @ docking-client ---
[INFO] 插件生效
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.749 s
[INFO] Finished at: 2023-01-08T15:13:10+08:00
[INFO] ------------------------------------------------------------------------