[https://maven.apache.org/plugin-developers/index.html(https://maven.apache.org/plugin-developers/index.html)
概述
timer plugin, 提供4个goal:
- currentTime
- currentDate
- currentMonth
- currentYear
打包
命令
maven clean install
常见错误
goalPrefix MISSING
错误信息
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.13.1:helpmojo (help-goal) on project timer-plugin: You need to specify a goalPrefix as it can not be correctly computed -> [Help 1]
原因
自动生成的配置中,有一个属性goalPrefix
,这个属性用于配置插件的缩写名,默认情况下,符合前面命名要求的时候,不需要配置.
- maven 官方插件:
maven-XXX-plugin
- 非官方插件:
XXX-maven-plugin
如果不满足上述约定,则需要手动指定goalPrefix
,在当前repo 文件下,添加如下配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.13.1</version>
<configuration>
<goalPrefix>timer-plugin</goalPrefix>
</configuration>
</plugin>
</plugins>
</build>
exec
命令行执行
mvn cn.jhs.maven.plugins:timer-plugin:1.0-SNAPSHOT:currentTime cn.jhs.maven.plugins:timer-plugin:1.0-SNAPSHOT:currentYear
绑定maven-phase执行
在其他的maven项目中,指定当前插件:
<build>
<plugins>
<plugin>
<groupId>cn.jhs.maven.plugins</groupId>
<artifactId>timer-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>timer-pre-clean</id>
<goals>
<goal>currentYear</goal>
<goal>currentMonth</goal>
<goal>currentDate</goal>
</goals>
<phase>pre-clean</phase>
</execution>
<execution>
<id>timer-clean</id>
<goals>
<goal>currentTime</goal>
</goals>
<phase>clean</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
best practice
添加descriptor + helpmojo
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
后续执行命令 mvn cn.jhs.maven.plugins:timer-plugin:1.0-SNAPSHOT:help
可以看到这个plugin的完整说明.
code
CurrentDateMojo
@Mojo(name="currentDate",defaultPhase= LifecyclePhase.CLEAN)
public class CurrentDateMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info(this.getClass().getSimpleName()+" plugin is running,current date is " + LocalDate.now().getYear());
}
}