文章目录
- 一、plugin的定义与配置
- 1. 自定义plugin
- 2. 绑定goal到maven执行周期
- 3. 定义goal默认的maven周期
- 4. 命令执行当前pom定义的execution
- 5. plugin运行时采用最新dependencies
- 6. 插件配置不在子pom生效
- 二、扩展
一、plugin的定义与配置
1. 自定义plugin
参考: 官网
- 基本定义
其中@Mojo.name
为goal的名称,@Parameter
为configuraiton
中配置的定义
@Mojo( name = "query" ) //定义goal的名称
public class MyQueryMojoextends AbstractMojo
{@Parameter(property = "query.url", required = true)private String url;@Parameter(property = "timeout", required = false, defaultValue = "50")private int timeout;@Parameter(property = "options")private String[] options;public void execute()throws MojoExecutionException{...}
}
使用插件
<project>...<build><plugins><plugin><artifactId>maven-myquery-plugin</artifactId><version>1.0</version><configuration><url>http://www.foobar.com/query</url><timeout>10</timeout><options><option>one</option><option>two</option><option>three</option></options></configuration></plugin></plugins></build>...
</project>
- 定义配置项类型转换
配置值到具体类型配置项的转换
Parameter Class | Conversion from String |
---|---|
Boolean | Boolean.valueOf(String) |
Byte | Byte.decode(String) |
Character | Character.valueOf(char) of the first character in the given string |
Class | Class.forName(String) |
java.util.Date | SimpleDateFormat.parse(String) for the following patterns: yyyy-MM-dd hh:mm:ss.S a , yyyy-MM-dd hh:mm:ssa , yyyy-MM-dd HH:mm:ss.S or yyyy-MM-dd HH:mm:ss |
Double | Double.valueOf(String) |
Enum | Enum.valueOf(String) |
java.io.File | new File(String) with the file separators normalized to File.separatorChar . In case the file is relative, is is made absolute by prefixing it with the project’s base directory. |
Float | Float.valueOf(String) |
Integer | Integer.decode(String) |
Long | Long.decode(String) |
Short | Short.decode(String) |
String | n/a |
StringBuffer | new StringBuffer(String) |
StringBuilder | new StringBuilder(String) |
java.net.URI | new URI(String) |
java.net.URL | new URL(String) |
- 配置项为复杂对象时
通过xml标签的缩进来体现
<project>
...
<configuration><person><firstName>Jason</firstName><lastName>van Zyl</lastName></person>
</configuration>
...
</project>
- 配置项为接口
具体通过实现类体现
<project>
...
<configuration><person implementation="com.mycompany.mojo.query.SuperPerson"><firstName>Jason</firstName><lastName>van Zyl</lastName></person>
</configuration>
...
</project>
- 配置项为集合
public class MyAnimalMojoextends AbstractMojo
{@Parameter(property = "animals")private List<String> animals;public void execute()throws MojoExecutionException{...}
}
<project>...<build><plugins><plugin><artifactId>maven-myanimal-plugin</artifactId><version>1.0</version><configuration><animals><animal>cat</animal><animal>dog</animal><animal>aardvark</animal></animals></configuration></plugin></plugins></build>...
</project>
<project>...<build><plugins><plugin><artifactId>maven-myanimal-plugin</artifactId><version>1.0</version><configuration><animals>cat,dog,aardvark</animals></configuration></plugin></plugins></build>...
</project>
- 配置项为map
...@Parameterprivate Map<String,String> myMap;
...
<project>
...<configuration><myMap><key1>value1</key1><key2>value2</key2></myMap></configuration>
...
</project>
- 配置项为properties
...@Parameterprivate Properties myProperties;
...
<project>
...<configuration><myProperties><property><name>propertyName1</name><value>propertyValue1</value></property><property><name>propertyName2</name><value>propertyValue2</value></property></myProperties></configuration>
...
</project>
2. 绑定goal到maven执行周期
- 绑定到单个goal
注意: 第二个goal未绑定(除非定义时有默认的绑定周期)
<project>...<build><plugins><plugin><artifactId>maven-myquery-plugin</artifactId><version>1.0</version><executions><execution><id>execution1</id><phase>test</phase><configuration><url>http://www.foo.com/query</url><timeout>10</timeout><options><option>one</option><option>two</option><option>three</option></options></configuration><goals><goal>query</goal></goals></execution><execution><id>execution2</id><configuration><url>http://www.bar.com/query</url><timeout>15</timeout><options><option>four</option><option>five</option><option>six</option></options></configuration><goals><goal>query</goal></goals></execution></executions></plugin></plugins></build>...
</project>
- 绑定到多个周期
<project>...<build><plugins><plugin>...<executions><execution><id>execution1</id><phase>test</phase>...</execution><execution><id>execution2</id><phase>install</phase><configuration><url>http://www.bar.com/query</url><timeout>15</timeout><options><option>four</option><option>five</option><option>six</option></options></configuration><goals><goal>query</goal></goals></execution></executions></plugin></plugins></build>...
</project>
3. 定义goal默认的maven周期
@Mojo( name = "query", defaultPhase = LifecyclePhase.PACKAGE )
public class MyBoundQueryMojoextends AbstractMojo
{@Parameter(property = "query.url", required = true)private String url;@Parameter(property = "timeout", required = false, defaultValue = "50")private int timeout;@Parameter(property = "options")private String[] options;public void execute()throws MojoExecutionException{...}
}
也可以换绑到其他周期,原来的周期不再生效
<project>...<build><plugins><plugin><artifactId>maven-myquery-plugin</artifactId><version>1.0</version><executions><execution><id>execution1</id><phase>install</phase><configuration><url>http://www.bar.com/query</url><timeout>15</timeout><options><option>four</option><option>five</option><option>six</option></options></configuration><goals><goal>query</goal></goals></execution></executions></plugin></plugins></build>...
</project>
4. 命令执行当前pom定义的execution
mvn myquery:query@execution1
5. plugin运行时采用最新dependencies
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.2</version>...<dependencies><dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.7.1</version></dependency><dependency><groupId>org.apache.ant</groupId><artifactId>ant-launcher</artifactId><version>1.7.1</version></dependency></dependencies></plugin></plugins></build>...
</project>
6. 插件配置不在子pom生效
默认是传播的,设置inherited=false即可
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.2</version><inherited>false</inherited>...</plugin></plugins></build>...
</project>
二、扩展
- 插件使用方式推荐
父pom定义版本管理,子pom直接引用
<project>...<build><!-- To define the plugin version in your parent POM --><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version></plugin>...</plugins></pluginManagement><!-- To use the plugin goals in your POM or parent POM --><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId></plugin>...</plugins></build>...
</project>
- maven的默认周期对应的goal
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
pre-integration-test
integration-test
post-integration-test
verify
install
deploy