@adonia
2016-04-04T16:13:03.000000Z
字数 3930
阅读 183
maven
plugin
packaging
指定为maven-plugin
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.3</version>
</dependency>
Tips:
1). maven-core中定义Maven Project相关的类,如org.apache.maven.project.MavenProject
,包含了groupId、artifactId等。
2) maven-plugin-annotations提供了通过注解的方式引用maven plugin中的相关参数,可参考Maven Project.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<execution>
<id>generated-helpmojo</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
参考maven-plugin-plugin
Tips: 如果在IDE中,上述插件报错,可直接按照IDE中的提示信息屏蔽即可。
Maven Plugin最基础的是Mojo(Maven plain Old Java Object),开发Maven插件,也就是自定义Mojo的过程。定义Mojo类,继承自org.apache.maven.plugin.AbstractMojo
,实现核心方法:
/**
* Perform whatever build-process behavior this <code>Mojo</code> implements.
* <br/>
* This is the main trigger for the <code>Mojo</code> inside the <code>Maven</code> system, and allows
* the <code>Mojo</code> to communicate errors.
*
* @throws MojoExecutionException if an unexpected problem occurs.
* Throwing this exception causes a "BUILD ERROR" message to be displayed.
* @throws MojoFailureException if an expected problem (such as a compilation failure) occurs.
* Throwing this exception causes a "BUILD FAILURE" message to be displayed.
*/
void execute() throws MojoExecutionException, MojoFailureException;
@Mojo(name = "start")
public class StartMojo extends BuildMojo
{
/**
* 端口映射,container port <---> host port,两者之间以冒号(:)隔开,例如 8080/tcp:18080.
* 如果包含多对,以逗号(,)分隔。
*
* 也可通过System Property指定。
*/
@Parameter(property = "portBindings", alias = "port.bindings")
private String portBindings;
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
super.execute();
final String id = createConatiner();
startContainer(id);
}
...
}
BuildMojo
继承自org.apache.maven.plugin.AbstractMojo
,其他的方法不做介绍。
说明:
* @Mojo(name = "start")
指定了以什么方式触发该插件。
* @Parameter(property = "portBindings", alias = "port.bindings")
即使用maven plugin annotations的方式来引用插件中的参数定义,即<configuration></configuration>
中的参数列表。注意:property
需要跟定义的java变量名称一致,alias
指定了该变量的别名。
<build>
<plugins>
<plugin>
<groupId>com.huawei.cmp</groupId>
<artifactId>com.huawei.cmp.maven.plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<docker.archive>target/frontend-package.tar.gz</docker.archive>
<docker.server>http://10.137.206.65:15000</docker.server>
<docker.api.version>v1.22</docker.api.version>
<port.bindings>8080/tcp:19002</port.bindings>
</configuration>
</plugin>
</plugins>
</build>
Tips:
*<configuration>
中即定义了该插件需要使用到的参数,就是通过@Parameter()
注解引用的。
* 通过mvn groupId:artifactId:version:goal
就可触发插件的执行了,例如mvn:com.huawei.cmp:com.huawei.cmp.maven.plugin:1.0-SNAPSHOT:start
。goal
即通过@Mojo(name = '')
指定的。
也可以将插件包含在maven的阶段中执行,比如如果想要在执行mvn clean package
时,自动触发上述插件的执行,可以通过按照如下的方式配置:
<build>
<plugins>
<plugin>
<groupId>com.huawei.cmp</groupId>
<artifactId>com.huawei.cmp.maven.plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<docker.archive>target/frontend-package.tar.gz</docker.archive>
<docker.server>http://10.137.206.65:15000</docker.server>
<docker.api.version>v1.22</docker.api.version>
<port.bindings>8080/tcp:19002</port.bindings>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Tips:
*<phase>package</phase>
即指明在指定的阶段。
* 在执行mvn clean package
时,com.huawei.cmp.maven.plugin
也会同步执行。
* maven plugin会按照配置的顺序执行(同一阶段范畴),如果需要在自定义的插件之后做些准备工作,在其上定义其他插件即可。
注:
1. 插件的开发,参考docker-maven-plugin.
2. 插件的使用,参考docker-plugin-demo.