1、创建插件项目
- 创建项目
- 修改pom依赖
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.2 </version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
- 编写自定义插件执行goal
// hello执行的goal名称。name对应配置参数
@Mojo(name = "hello", defaultPhase = LifecyclePhase.NONE)
public class DemoMojo extends AbstractMojo {
@Parameter(name = "name", defaultValue = "kiwi")
private String name;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("hello:" + name);
}
}
- 安装到本地仓库测试install
2、插件测试
- 在目标项目中引入插件
<plugin>
<groupId>com.link</groupId>
<artifactId>demo-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
<!--执行对应插件名称-->
<goal>hello</goal>
</goals>
<!--执行阶段-->
<phase>clean</phase>
</execution>
</executions>
<configuration>
<!--参数配置-->
<name>tom</name>
</configuration>
</plugin>
效果如下:
- 配置插件goal为hello,在clean阶段执行,参数为tom