命名规范
Maven的官方插件命名格式为maven-xxx-plugin
。为了避免侵犯官方商标,我们一般将自己开发的插件命名为xxx-maven-plugin
。遵守这个规范,可以简化插件的运行命令。
创建插件项目
1. 创建一个maven项目
2. 修改pom文件
<groupId>com.jx.toolmall</groupId>
<artifactId>copyjar-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0.0</version>
packaging
为maven-plugin
3. 添加依赖
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
4. 业务逻辑
/**
* @author qiudx created by 2020/5/9 10:27
*/
@Mojo(name = "copy", defaultPhase = LifecyclePhase.PACKAGE)
public class CopyjarMojo extends AbstractMojo {
@Parameter(readonly = true, required = true)
private File outputDirectory;
@Parameter(defaultValue = "${file.separator}${project.build.finalName}.${project.packaging}", readonly = true, required = true)
private String targetName;
@Parameter(defaultValue = "${project.build.directory}${file.separator}${project.build.finalName}.${project.packaging}", readonly = true, required = true)
private File source;
public void execute() throws MojoExecutionException {
if (outputDirectory == null) {
throw new MojoExecutionException("This plugin run error, the outputDirectory is null. ");
}
if (!source.exists()) {
throw new MojoExecutionException("This plugin run error, the copy target is null. ");
}
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}
copy(source.getPath(), outputDirectory.getPath() + targetName);
}
public void copy(String source, String target) throws MojoExecutionException {
FileChannel sourceChannel = null;
FileChannel targetChannel = null;
try {
sourceChannel = FileChannel.open(Paths.get(source), StandardOpenOption.READ);
targetChannel = FileChannel.open(Paths.get(target), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
} catch (IOException e) {
this.getLog().error(e.getMessage(), e);
throw new MojoExecutionException("Copying file failure. ");
} finally {
IOUtil.close(sourceChannel);
IOUtil.close(targetChannel);
}
}
}
使用插件
<plugin>
<groupId>com.jx.toolmall</groupId>
<artifactId>copyjar-maven-plugin</artifactId>
<version>${copyjar-maven-plugin.version}</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>doc/back</outputDirectory>
</configuration>
</plugin>
plugin-->executions
配置插件运行的生命周期和运行什么插件phase
当执行mvn install时插件中mojo为copy就会执行configuration
通过pom方式配置mojo中自定义参数
生命周期
validate
: 验证initialize
: 初始化构建状态,例如设置属性或创建目录generate-sources
: 生成包含在编译中的任何源代码process-sources
: 处理源代码generate-resources
: 生成包含在包中的资源process-resources
: 将资源复制并处理到目标目录中,准备打包compile
: 编译项目的源代码process-classes
: 处理编译后生成的文件generate-test-sources
: 生成包含在编译中的任何测试源代码process-test-sources
: 处理测试源代码generate-test-resources
: 创建测试资源process-test-resources
: 将资源复制并处理到测试目标目录中test-compile
: 将测试源代码编译到测试目标目录中process-test-classes
: 处理编译后产生的测试文件test
: 测试prepare-package
: 预打包package
: 打包verify
: 验证install
: 安装deploy
: 部署
属性
1.内置属性
Maven预定义,用户可以直接使用
${basedir}
表示项目根目录,即包含pom.xml文件的目录;${version}
表示项目版本;${project.basedir}
同${basedir};${project.baseUri}
表示项目文件地址;${maven.build.timestamp}
表示项目构件开始时间;${maven.build.timestamp.format}
表示属性${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm,可自定义其格式,其类型可参考java.text.SimpleDateFormat。用法如下:
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>
2.pom属性
pom属性
${project.build.directory}
表示主源码路径,默认为xxx/target/;${project.build.sourceEncoding}
表示主源码的编码格式;${project.build.sourceDirectory}
表示主源码路径,默认为xxx/src/main/java/;${project.version}
表示项目版本,与${version}相同;${project.build.finalName}
项目打包输出文件的名称,默认为${project.artifactId}${project.version}
3.系统属性
${file.separator}
不同系统对应的分隔符${settings.localRepository}
表示本地仓库的地址${env.JAVA_HOME}
表示JAVA_HOME环境变量的值;
….
参数的使用
1.系统内置参数
@Parameter(defaultValue = "${project.basedir}")
private File baseDir;
2.使用自定义属性
@Parameter(readonly = true, required = true)
private File outputDirectory;
给outputDirectory赋值的两种方式:
使用-D指定
mvn copyjar:copy -DoutputDirectory=xxx
通过pom文件
plugin-->configuration
里配置<plugin>
<groupId>com.jx.toolmall</groupId>
<artifactId>copyjar-maven-plugin</artifactId>
<version>${copyjar-maven-plugin.version}</version>
...
<configuration>
<outputDirectory>../doc/back</outputDirectory>
</configuration>
</plugin>
3.系统参数使用
@Parameter(defaultValue = "${file.separator}")
private File separator;