1 新建java普通的jar项目
2 添加maven-plugin的pom依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
</dependencies>
3 编写插件代码
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.util.List;
@Mojo(name="compile", defaultPhase= LifecyclePhase.COMPILE)
public class MavenPluginCompileMojo extends AbstractMojo {
@Parameter
private String describe;
@Parameter
private List<String> extentParams;
@Parameter
private String path;
@Parameter(defaultValue="${project}", readonly=true, required=true)
private MavenProject project;
@Parameter(defaultValue="${session}", readonly=true, required=true)
private MavenSession session;
/**
* POM属性
* 项目构建输出目录: 默认target/
*/
@Parameter(defaultValue="${project.build.directory}", required=true)
private String buildDirectory;
/**
* POM属性
* 项目主源码目录: 默认 src/mian/java
*/
@Parameter(defaultValue="${project.build.sourceDirectory}", readonly=true)
private String sourceDirectory;
/**
* POM属性
* 项目代码编译输出目录: 默认 target/classes/
*/
@Parameter(defaultValue="${project.build.outputDirectory}", readonly=true)
private String outputDirectory;
/**
* POM属性
* 打包输出项目名: 默认 ${project.artifactId}-${project.version}
*/
@Parameter(defaultValue="${project.build.finalName}", readonly=true)
private String finalName;
/**
* POM属性
*/
@Parameter(defaultValue="${project.build.sourceEncoding}",readonly = true)
private String resourceEncoding;
/**
* POM属性
*/
@Parameter(defaultValue="${project.groupId}", readonly=true)
private String groupId;
/**
* POM属性
*/
@Parameter(defaultValue="${project.artifactId}", readonly=true)
private String artifactId;
/**
* POM属性
* 项目版本
*/
@Parameter(defaultValue = "${project.version}",readonly = true)
private String projectVersion;
/**
* 内置属性
* 项目根目录
*/
@Parameter(defaultValue = "${basedir}",readonly = true)
private String projectBaseDir;
/**
*自定义属性
*
*<properties>
* <jar.useDefault>true</jar.useDefault>
*</properties>
*
*/
@Parameter(property="jar.useDefault", defaultValue="false")
private boolean useDefault;
/**
* java 系统属性
*
* 备注:mvn help:system
*/
@Parameter(defaultValue = "${user.home}",readonly = true)
private String javaUserHome;
/**
* java 系统属性
*
* 备注:mvn help:system
*/
@Parameter(defaultValue = "${java.home}",readonly = true)
private String javaHome;
/**
* 环境变量
*
* 备注:mvn help:system
*/
@Parameter(defaultValue = "${env.JAVA_HOME}",readonly = true)
private String envJavaHome;
@Parameter(defaultValue = "${env.MAVEN_HOME}",readonly = true)
private String envMavenHome;
/**
* 执行入口
* 调试:mvn Debug cn.chy:mavenplugindemo:2.0-SNAPSHOT:compile
* @throws MojoExecutionException
* @throws MojoFailureException
*/
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("MavenPluginCompileMojo | compile");
getLog().info("=======java系统属性=========>");
getLog().info("javaUserHome" + javaUserHome);
getLog().info("javaHome:" + javaHome);
getLog().info("=======环境变量=========>");
getLog().info("JAVA_HOME" + envJavaHome);
getLog().info("MAVEN_HOME:" + envMavenHome);
getLog().info("=======maven POM属性=========>");
getLog().info("buildDirectory:" + buildDirectory);
getLog().info("sourceDirectory:" + sourceDirectory);
getLog().info("outputDirectory:" + outputDirectory);
getLog().info("resourceEncoding:" + resourceEncoding);
getLog().info("finalName:" + finalName);
getLog().info("groupId:" + groupId);
getLog().info("artifactId:" + artifactId);
getLog().info("======maven 内置属性==========>");
getLog().info("projectBaseDir:" + projectBaseDir);
getLog().info("projectVersion:" + projectVersion);
getLog().info("======maven 自定义属性==========>");
getLog().info("useDefault:" + useDefault);
getLog().info("======maven plugin 参数==========>");
getLog().info("describe:" + describe);
if (extentParams != null) {
for (String string : extentParams) {
getLog().info("extentParams:" + string);
}
}
getLog().info("path:" + path);
}
}
4 插件打包上传maven仓库
5 新建test项目引用maven插件
<build>
<plugins>
<plugin>
<groupId>cn.chy</groupId>
<artifactId>mavenplugindemo</artifactId>
<version>2.0-SNAPSHOT</version>
<!-- 全局默认参数 -->
<configuration>
<describe>this is maven plugin demo</describe>
<extentParams>
<manyParam>1</manyParam>
<manyParam>2</manyParam>
</extentParams>
<path>${basedir}/src/main/java</path>
</configuration>
<executions>
<execution>
<!-- 有多个execution 必须有id -->
<id>compileid</id>
<!-- 编译阶段执行 -->
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<!-- 局部参数 会覆盖全局参数 -->
<configuration>
<describe>this is maven plugin compile demo</describe>
<extentParams>
<manyParam>3</manyParam>
<manyParam>4</manyParam>
</extentParams>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
6 test项目执行maven compile