《Maven官方文档》插件程序配置引导
插件程序配置引导
1.通用配置
1.帮助目标
2.配置参数
1.匹配简单对象
2.匹配复杂对象
3.匹配集合对象
1.匹配Lists
2.匹配Maps
3.匹配Poperties
2.配置构建插件
1.使用 <executions>标签
2.使用 <dependencies>标签
3.使用 <inherited>标签
3.配置报表插件
1.使用 <reporting>标签 对比 <build>标签
2.使用 <reportSets>标签
3.使用 <inherited>标签
介绍
在Maven,有构建和报表插件:
- 构建插件:在构建的时候执行,应该配置在<build/>标签里。
- 报表插件:在生成网站时候执行,应该配置在<reporting/>标签里。
所有插件至少需要的信息: groupId ,artifactId 和 verion。
重要笔记:推荐定义每个版本插件,来确保构建过程中可重用性。好的实践就是在构建过程中指定它们到<build><pluginManagement/></build>节点内(通常,你会定义一个<pluginManagement/>的节点在父级的pom中)。对于报表插件,你应该指定每个版本插件到<reporting><plugins/></reporting>节点 (当然在<build><pluginManagement/></build>节点也是可以的)。
通用配置
/** * @goal query */ public class MyQueryMojo extends AbstractMojo { /** * @parameter expression="${query.url}" */ private String url; /** * @parameter default-value="60" */ private int timeout; /** * @parameter */ 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>
对于mojos预期被执行可以直接通过命令行,他们的参数提供配置的方式来替换pom中<configuration>的项目。这些插件文档为了参数可以列到配置文件的表达式中。在mojo之上,参数url与表达式${query.url}关联起来了,意味着可以通过化工系统属性指定query.url的值,如下:
mvn myquery:query -Dquery.url=http://maven.apache.org
帮助指令
- mvn javadoc:help -Ddetail -Dgoal=javadoc
配置参数
匹配简单对象
<configuration> <myString>a string</myString> <myBoolean>true</myBoolean> <myInteger>10</myInteger> <myDouble>1.0</myDouble> <myFile>c:\temp</myFile> <myURL>http://maven.apache.org</myURL> </configuration>
配置复杂对象
<configuration> <myString>a string</myString> <myBoolean>true</myBoolean> <myInteger>10</myInteger> <myDouble>1.0</myDouble> <myFile>c:\temp</myFile> <myURL>http://maven.apache.org</myURL> </configuration>
<configuration> <person> <firstName>Jason</firstName> <lastName>van Zyl</lastName> </person> </configuration></pre> </div> <div>
匹配集合类型
匹配Lists
public class MyAnimalMojo extends AbstractMojo{ /** * @parameter */ private List 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>
Mapping Maps
/** * My Map. * * @parameter */ private Map myMap;
<configuration> <myMap> <key1>value1</key1> <key2>value2</key2> </myMap> </configuration>
Mapping Properties
/** * My Properties. * * @parameter */ private Properties myProperties;
<configuration> <myProperties> <property> <name>propertyName1</name> <value>propertyValue1</value> <property> <property> <name>propertyName2</name> <value>propertyValue2</value> <property> </myProperties> </configuration>
配置构建插件
使用<executions>标签
<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>
/** * @goal query * @phase package */ public class MyBindedQueryMojo extends AbstractMojo { /** * @parameter expression="${query.url}"; */ private String url; /** * @parameter default-value="60" */ private int timeout; /** * @parameter */ privateString[] 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>
</div> <div>mvn myqyeryplugin:<a href="mailto:queryMojo@execution1">queryMojo@execution1</a></div> <div>
使用 <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>
在构建插件中使用 <inherited>标签
<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>
配置报表插件
使用<reporting>标签和<build>标签
使用<reportSets>标签
<project> ... <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>2.1.2</version> <reportSets> <reportSet> <reports> <report>project-team</report> </reports> </reportSet> </reportSets> </plugin> </plugins> </reporting> ... </project>
<reportSets> <reportSet> <reports/> </reportSet> </reportSets>
在报表插件中使用 <inherited> 标签
<project> ... <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>2.1.2</version> <inherited>false</inherited> </plugin> </plugins> </reporting> ... </project> <div>
原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: 《Maven官方文档》插件程序配置引导


暂无评论