Maven只是一套框架,它的功能基于全部依赖于插件来实现。因此可以通过插件开发来定制Maven。
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
Maven 官方的插件命名为:maven-
第三方插件命名要求为:
<dependencies>
<dependency>
<groupId>org.apache.mavengroupId>
<artifactId>maven-plugin-apiartifactId>
<version>3.0version>
dependency>
<dependency>
<groupId>org.apache.maven.plugin-toolsgroupId>
<artifactId>maven-plugin-annotationsartifactId>
<version>3.4version>
<scope>providedscope>
dependency>
dependencies>
maven-plugin
<packaging>maven-pluginpackaging>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-plugin-pluginartifactId>
<version>3.6.0version>
<configuration>
<goalPrefix>zzwgoalPrefix>
<skipErrorNoDescriptorsFound>trueskipErrorNoDescriptorsFound>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>3.3.0version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>trueaddDefaultImplementationEntries>
manifest>
archive>
configuration>
plugin>
1、添加maven-plugin-plugin
插件依赖,这个包可以使插件支持JDK1.8以上的版本;
[Ref] Maven插件开发教程
2、addDefaultImplementationEntries
会在生成的jar包的META-INF
目录下的MANIFEST.MF
文件里生成版本信息
[Ref] maven项目代码获取<version>版本号(通过jar包获取)
[Ref] Maven maven-jar-plugin 配置详情
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
@Mojo(name = "sayhi")
public class GreetingMojo extends AbstractMojo {
public void execute() throws MojoExecutionException {
getLog().info("Hello, world.");
}
}
<groupId>org.examplegroupId>
<artifactId>test-maven-pluginartifactId>
<version>1.0-SNAPSHOTversion>
[Ref] mvn 插件groupId:插件artifactId[:插件版本]:插件目标名称
<plugin>
<groupId>org.examplegroupId>
<artifactId>test-maven-pluginartifactId>
<version>1.0-SNAPSHOTversion>
<executions>
<execution>
<id>test1id>
<phase>compilephase>
<goals>
<goal>sayhigoal>
goals>
execution>
executions>
plugin>
phase
标签里面定义要绑定的生命周期,id
用于命令,可以自己定义。
我们可以看到,我们的自定义插件顺利随着compile生命周期执行了。
[Ref] 把自定义插件绑定在项目的生命周期中
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
@Mojo(name = "sayhi")
public class GreetingMojo extends AbstractMojo {
@Parameter(property = "username", defaultValue = "moutory")
private String userName;
@Parameter(property = "status", defaultValue = "happy")
private String status;
public void execute() throws MojoExecutionException {
getLog().info("Hello, world.");
System.out.println("userName " + userName + " status " + status);
}
}
<build>
<plugins>
<plugin>
<groupId>org.examplegroupId>
<artifactId>test-maven-pluginartifactId>
<version>1.0-SNAPSHOTversion>
<configuration>
<userName>JimuserName>
<status>goodstatus>
configuration>
plugin>
plugins>
build>
4.0.0
org.example
test-maven-plugin
1.0-SNAPSHOT
test-maven-plugin
test-maven-plugin
maven-plugin
8
org.apache.maven
maven-plugin-api
3.0
org.apache.maven.plugin-tools
maven-plugin-annotations
3.4
provided
org.apache.maven.plugins
maven-plugin-plugin
3.6.0
zzw
true
org.apache.maven.plugins
maven-jar-plugin
3.3.0
true
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
@Mojo(name = "sayhi")
public class GreetingMojo extends AbstractMojo {
@Parameter(property = "username", defaultValue = "moutory")
private String userName;
@Parameter(property = "status", defaultValue = "happy")
private String status;
public void execute() throws MojoExecutionException {
getLog().info("Hello, world.");
System.out.println("userName " + userName + " status " + status);
}
}
[Ref] maven 中的 goal 是什么
Error resolving version for plugin ‘XXX‘ from the repositories,Plugin not found in any plugin reposi
Failed to parse plugin descriptor for org.apache.maven:maven-plugin-api:4.0.0-alpha-10
[Ref] Maven插件开发
[Ref] 此文罗列了部分官方文档翻译
[Ref] Mojo配置方式