流程
首先, 你需要创建一个Maven项目,推荐用idea 创建项目 会自动配置插件
pom.xml文件中添加以下配置:
<project> <!-- 项目的基本信息 --> <groupId>com.example</groupId> <artifactId>my-maven-plugin</artifactId> <version>1.0.0</version> <packaging>maven-plugin</packaging> <!-- 项目的依赖 --> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>3.9.5</version> </dependency><dependency><groupId>org.apache.maven.plugin-tools</groupId><artifactId>maven-plugin-annotations</artifactId><version>3.2</version><scope>provided</scope></dependency></dependencies><!-- 插件的配置 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-plugin-plugin</artifactId> <version>3.10.2</version> <configuration> <!-- 插件的目标类 --> <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound></configuration> <executions> <execution> <id>mojo-descriptor</id> <goals> <goal>descriptor</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
</project>
老版本的依赖
<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId> <artifactId>my-maven-plugin</artifactId><packaging>maven-plugin</packaging><version>1.0.0</version><name>untitled1 Maven Mojo</name><url>http://maven.apache.org</url><dependencies><!-- 老版本的依赖方式 --><dependency><groupId>org.apache.maven</groupId><artifactId>maven-plugin-api</artifactId><version>2.0</version></dependency><dependency><groupId>org.apache.maven.plugin-tools</groupId><artifactId>maven-plugin-annotations</artifactId><version>3.2</version><scope>provided</scope></dependency><!-- 老版本的依赖方式 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><!-- 插件的配置 --><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-plugin-plugin</artifactId><version>3.2</version><configuration><!-- 插件的目标类 --><skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound></configuration><executions><execution><id>mojo-descriptor</id><goals><goal>descriptor</goal></goals></execution></executions></plugin></plugins></build>
</project>
接下来,你需要创建一个实现了Mojo接口的Java类。Mojo是Maven Old Java Object的缩写,代表一个Maven插件的目标(goal)。在这个类中,你可以定义插件的具体行为。以下是一个简单的示例:
package com.example; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import java.util.List; @Mojo(name = "my-goal", defaultPhase = LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo { @Parameter(property = "message", defaultValue = "Hello, World!") private String message; @Parameter(property = "names") private List<String> names; @Override public void execute() throws MojoExecutionException { getLog().info(message); for (String name : names) { getLog().info("Name: " + name); } }
}
在这个示例中,我们创建了一个名为MyMojo的类,并使用@Mojo注解来指定插件的目标名称为my-goal,以及默认的生命周期阶段为PACKAGE。我们还定义了两个参数:message和names,分别使用@Parameter注解进行配置。在execute()方法中,我们简单地打印了这些参数的值。现在,你可以构建并安装你的Maven插件。在命令行中,导航到项目的根目录,并执行以下命令:
mvn clean install
这将编译并打包你的插件,并将其安装到本地Maven仓库中。一旦安装完成,你就可以在其他Maven项目中使用你的插件了。以下是一个使用示例:在另一个Maven项目的pom.xml文件中,添加以下配置:
<project> <!-- 项目的其他配置 --> <build> <plugins> <plugin> <groupId>com.example</groupId> <artifactId>my-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <message>Hello from my plugin!</message> <names> <name>Alice</name> <name>Bob</name> <name>Charlie</name> </names> </configuration> <executions> <execution> <goals> <goal>my-goal</goal> <!-- 指定插件的目标 --> </goals> </execution> </executions> </plugin> </plugins> </build>
</project>
ideal 运行配置这个插件
command line "com.example:my-maven-plugin:1.0.0:my-goal" 命令行太长,我们可以通过 增加 goalPrefix 属性来,修改命令前缀,具体做法如下:
修改原有的 MyMojo
@Mojo(name = "my-goal", defaultPhase = LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo {@Parameter(property = "goalPrefix", defaultValue = "my-prefix")private String goalPrefix;@Parameter(property = "message", defaultValue = "Hello, World!")private String message;@Parameter(property = "names")private List<String> names;@Overridepublic void execute() throws MojoExecutionException {getLog().info(message);for (String name : names) {getLog().info("Name: " + name);}}
}
修改 my-maven-plugin
插件pom.xml文件,增加goalPrefix
<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/maven-v4_0_0.xsd">... <!-- 插件的配置 --><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-plugin-plugin</artifactId><version>3.10.2</version><configuration><!-- 插件的目标类 --><skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound><goalPrefix>my-prefix</goalPrefix></configuration><executions><execution><id>mojo-descriptor</id><goals><goal>descriptor</goal></goals></execution></executions></plugin></plugins></build>
</project>
重新打包,然后在引用项目中 ,通过ideal 修改如下运行配置:
my-prefix:my-goal
对应的maven 命令行
mvn my-prefix:my-goal