命名规范

Maven的官方插件命名格式为maven-xxx-plugin。为了避免侵犯官方商标,我们一般将自己开发的插件命名为xxx-maven-plugin。遵守这个规范,可以简化插件的运行命令。

创建插件项目

1. 创建一个maven项目

2. 修改pom文件

  1. <groupId>com.jx.toolmall</groupId>
  2. <artifactId>copyjar-maven-plugin</artifactId>
  3. <packaging>maven-plugin</packaging>
  4. <version>1.0.0</version>

packaging为maven-plugin

3. 添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.maven</groupId>
  4. <artifactId>maven-plugin-api</artifactId>
  5. <version>3.6.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.maven.plugin-tools</groupId>
  9. <artifactId>maven-plugin-annotations</artifactId>
  10. <version>3.6.0</version>
  11. <scope>provided</scope>
  12. </dependency>
  13. </dependencies>

4. 业务逻辑

  1. /**
  2. * @author qiudx created by 2020/5/9 10:27
  3. */
  4. @Mojo(name = "copy", defaultPhase = LifecyclePhase.PACKAGE)
  5. public class CopyjarMojo extends AbstractMojo {
  6. @Parameter(readonly = true, required = true)
  7. private File outputDirectory;
  8. @Parameter(defaultValue = "${file.separator}${project.build.finalName}.${project.packaging}", readonly = true, required = true)
  9. private String targetName;
  10. @Parameter(defaultValue = "${project.build.directory}${file.separator}${project.build.finalName}.${project.packaging}", readonly = true, required = true)
  11. private File source;
  12. public void execute() throws MojoExecutionException {
  13. if (outputDirectory == null) {
  14. throw new MojoExecutionException("This plugin run error, the outputDirectory is null. ");
  15. }
  16. if (!source.exists()) {
  17. throw new MojoExecutionException("This plugin run error, the copy target is null. ");
  18. }
  19. if (!outputDirectory.exists()) {
  20. outputDirectory.mkdirs();
  21. }
  22. copy(source.getPath(), outputDirectory.getPath() + targetName);
  23. }
  24. public void copy(String source, String target) throws MojoExecutionException {
  25. FileChannel sourceChannel = null;
  26. FileChannel targetChannel = null;
  27. try {
  28. sourceChannel = FileChannel.open(Paths.get(source), StandardOpenOption.READ);
  29. targetChannel = FileChannel.open(Paths.get(target), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
  30. sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
  31. } catch (IOException e) {
  32. this.getLog().error(e.getMessage(), e);
  33. throw new MojoExecutionException("Copying file failure. ");
  34. } finally {
  35. IOUtil.close(sourceChannel);
  36. IOUtil.close(targetChannel);
  37. }
  38. }
  39. }

使用插件

  1. <plugin>
  2. <groupId>com.jx.toolmall</groupId>
  3. <artifactId>copyjar-maven-plugin</artifactId>
  4. <version>${copyjar-maven-plugin.version}</version>
  5. <executions>
  6. <execution>
  7. <phase>install</phase>
  8. <goals>
  9. <goal>copy</goal>
  10. </goals>
  11. </execution>
  12. </executions>
  13. <configuration>
  14. <outputDirectory>doc/back</outputDirectory>
  15. </configuration>
  16. </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。用法如下:
  1. <properties>
  2. <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
  3. </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.系统内置参数

  1. @Parameter(defaultValue = "${project.basedir}")
  2. private File baseDir;

2.使用自定义属性

  1. @Parameter(readonly = true, required = true)
  2. private File outputDirectory;

给outputDirectory赋值的两种方式:

  • 使用-D指定mvn copyjar:copy -DoutputDirectory=xxx

  • 通过pom文件plugin-->configuration里配置

    1. <plugin>
    2. <groupId>com.jx.toolmall</groupId>
    3. <artifactId>copyjar-maven-plugin</artifactId>
    4. <version>${copyjar-maven-plugin.version}</version>
    5. ...
    6. <configuration>
    7. <outputDirectory>../doc/back</outputDirectory>
    8. </configuration>
    9. </plugin>

3.系统参数使用

  1. @Parameter(defaultValue = "${file.separator}")
  2. private File separator;