[关闭]
@adonia 2016-04-04T16:13:03.000000Z 字数 3930 阅读 183

Maven插件开发

maven plugin


新建Maven Plugin工程

  1. <dependency>
  2. <groupId>org.apache.maven</groupId>
  3. <artifactId>maven-core</artifactId>
  4. <version>3.2.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.maven</groupId>
  8. <artifactId>maven-plugin-api</artifactId>
  9. <version>3.2.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.maven.plugin-tools</groupId>
  13. <artifactId>maven-plugin-annotations</artifactId>
  14. <version>3.3</version>
  15. </dependency>

Tips:
1). maven-core中定义Maven Project相关的类,如org.apache.maven.project.MavenProject,包含了groupId、artifactId等。
2) maven-plugin-annotations提供了通过注解的方式引用maven plugin中的相关参数,可参考Maven Project.

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-plugin-plugin</artifactId>
  6. <version>3.2</version>
  7. <configuration>
  8. <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
  9. </configuration>
  10. <executions>
  11. <execution>
  12. <id>mojo-descriptor</id>
  13. <goals>
  14. <goal>descriptor</goal>
  15. </goals>
  16. </execution>
  17. <execution>
  18. <id>generated-helpmojo</id>
  19. <goals>
  20. <goal>helpmojo</goal>
  21. </goals>
  22. </execution>
  23. </executions>
  24. </plugin>
  25. </plugins>
  26. </build>

参考maven-plugin-plugin
Tips: 如果在IDE中,上述插件报错,可直接按照IDE中的提示信息屏蔽即可。


代码开发

Maven Plugin最基础的是Mojo(Maven plain Old Java Object),开发Maven插件,也就是自定义Mojo的过程。定义Mojo类,继承自org.apache.maven.plugin.AbstractMojo,实现核心方法:

  1. /**
  2. * Perform whatever build-process behavior this <code>Mojo</code> implements.
  3. * <br/>
  4. * This is the main trigger for the <code>Mojo</code> inside the <code>Maven</code> system, and allows
  5. * the <code>Mojo</code> to communicate errors.
  6. *
  7. * @throws MojoExecutionException if an unexpected problem occurs.
  8. * Throwing this exception causes a "BUILD ERROR" message to be displayed.
  9. * @throws MojoFailureException if an expected problem (such as a compilation failure) occurs.
  10. * Throwing this exception causes a "BUILD FAILURE" message to be displayed.
  11. */
  12. void execute() throws MojoExecutionException, MojoFailureException;

代码示例

  1. @Mojo(name = "start")
  2. public class StartMojo extends BuildMojo
  3. {
  4. /**
  5. * 端口映射,container port <---> host port,两者之间以冒号(:)隔开,例如 8080/tcp:18080.
  6. * 如果包含多对,以逗号(,)分隔。
  7. *
  8. * 也可通过System Property指定。
  9. */
  10. @Parameter(property = "portBindings", alias = "port.bindings")
  11. private String portBindings;
  12. @Override
  13. public void execute() throws MojoExecutionException, MojoFailureException
  14. {
  15. super.execute();
  16. final String id = createConatiner();
  17. startContainer(id);
  18. }
  19. ...
  20. }

BuildMojo继承自org.apache.maven.plugin.AbstractMojo,其他的方法不做介绍。

说明:
* @Mojo(name = "start")指定了以什么方式触发该插件。
* @Parameter(property = "portBindings", alias = "port.bindings")即使用maven plugin annotations的方式来引用插件中的参数定义,即<configuration></configuration>中的参数列表。注意:property需要跟定义的java变量名称一致,alias指定了该变量的别名。

使用插件

  1. 在目标工程中添加上述插件:
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.huawei.cmp</groupId>
  5. <artifactId>com.huawei.cmp.maven.plugin</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <configuration>
  8. <docker.archive>target/frontend-package.tar.gz</docker.archive>
  9. <docker.server>http://10.137.206.65:15000</docker.server>
  10. <docker.api.version>v1.22</docker.api.version>
  11. <port.bindings>8080/tcp:19002</port.bindings>
  12. </configuration>
  13. </plugin>
  14. </plugins>
  15. </build>

Tips:
* <configuration>中即定义了该插件需要使用到的参数,就是通过@Parameter()注解引用的。
* 通过mvn groupId:artifactId:version:goal就可触发插件的执行了,例如mvn:com.huawei.cmp:com.huawei.cmp.maven.plugin:1.0-SNAPSHOT:startgoal即通过@Mojo(name = '')指定的。

也可以将插件包含在maven的阶段中执行,比如如果想要在执行mvn clean package时,自动触发上述插件的执行,可以通过按照如下的方式配置:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.huawei.cmp</groupId>
  5. <artifactId>com.huawei.cmp.maven.plugin</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <configuration>
  8. <docker.archive>target/frontend-package.tar.gz</docker.archive>
  9. <docker.server>http://10.137.206.65:15000</docker.server>
  10. <docker.api.version>v1.22</docker.api.version>
  11. <port.bindings>8080/tcp:19002</port.bindings>
  12. </configuration>
  13. <executions>
  14. <execution>
  15. <phase>package</phase>
  16. <goals>
  17. <goal>start</goal>
  18. </goals>
  19. </execution>
  20. </executions>
  21. </plugin>
  22. </plugins>
  23. </build>

Tips:
* <phase>package</phase>即指明在指定的阶段。
* 在执行mvn clean package时,com.huawei.cmp.maven.plugin也会同步执行。
* maven plugin会按照配置的顺序执行(同一阶段范畴),如果需要在自定义的插件之后做些准备工作,在其上定义其他插件即可。

注:
1. 插件的开发,参考docker-maven-plugin.
2. 插件的使用,参考docker-plugin-demo.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注