Apache Maven,五:插件

Maven的插件分如下两种:

  • build plugins:该插件在项目构建阶段执行,它们都在<build>标签中设置。
  • reporting plugins : 该插件在网站生成期间执行,他们都在<reporting>标签中设置。

所有的插件至少需要三个属性:groupId, artifactId 和 version。

Configuration

Maven的插件不管是上面的build还是reporting都可以通过<configuration>标签,将<configuration>标签的子标签映射或则设置Mojo字段。查下如下示例:

Mojo

 1 /**
 2  * @goal query
 3  */
 4 public class MyQueryMojo
 5     extends AbstractMojo
 6 {
 7     /**
 8      * @parameter expression="${query.url}"
 9      */
10     private String url;
11  
12     /**
13      * @parameter default-value="60"
14      */
15     private int timeout;
16  
17     /**
18      * @parameter
19      */
20     private String[] options;
21  
22     public void execute()
23         throws MojoExecutionException
24     {
25         ...
26     }
27 }

通过你的POM设置configuration映射url, timeout 和 options属性值到你的Mojo

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <artifactId>maven-myquery-plugin</artifactId>
 7         <version>1.0</version>
 8         <configuration>
 9           <url>http://www.foobar.com/query</url>
10           <timeout>10</timeout>
11           <options>
12             <option>one</option>
13             <option>two</option>
14             <option>three</option>
15           </options>
16         </configuration>
17       </plugin>
18     </plugins>
19   </build>
20   ...
21 </project>

Configuring 参数

映射简单类型,比如Boolean或Integer,非常简单。类似于以下内容:

 1 ...
 2 <configuration>
 3   <myString>a string</myString>
 4   <myBoolean>true</myBoolean>
 5   <myInteger>10</myInteger>
 6   <myDouble>1.0</myDouble>
 7   <myFile>c:\temp</myFile>
 8   <myURL>http://maven.apache.org</myURL>
 9 </configuration>
10 ...

映射一个复杂的对象,可以查看以下示例,映射一个Person对象。

1 ...
2 <configuration>
3   <person>
4     <firstName>Jason</firstName>
5     <lastName>van Zyl</lastName>
6   </person>
7 </configuration>
8 ...

映射一个复杂的对象,需要满足以下规则:

  • 必须要有一个专有的字段与正在映射的对象名称对应。如上例的person标签对应一个Mojo的person对象。
  • 实例化的对象必须与Mojo对象位于同一个包中,那么映射机制将会 将person第一个字母大写去Mojo包中寻找一个Person对象。
  • 如果对象和Moji不在同一个包中可以使用implementation属性指定。

通过implementation属性指定对象所在的包。

...
<configuration>
  <person implementation="com.mycompany.mojo.query.SuperPerson">
    <firstName>Jason</firstName>
    <lastName>van Zyl</lastName>
  </person>
</configuration>
...

映射一个List集合,比如说:private List animals

1 ...
2 <configuration>
3   <animals>
4     <animal>cat</animal>
5     <animal>dog</animal>
6     <animal>aardvark</animal>
7   </animals>
8 </configuration>
9 ...

映射一个Map集合,比如说:private Map myMap

1 ...
2   <configuration>
3     <myMap>
4       <key1>value1</key1>
5       <key2>value2</key2>
6     </myMap>
7   </configuration>
8 ...

映射一个properties,比如说:private Properties myProperties

 1 ...
 2   <configuration>
 3     <myProperties>
 4       <property>
 5         <name>propertyName1</name>
 6         <value>propertyValue1</value>
 7       <property>
 8       <property>
 9         <name>propertyName2</name>
10         <value>propertyValue2</value>
11       <property>
12     </myProperties>
13   </configuration>
14 ...

配置 bulid 插件

使用 executions 标签

除了使用configuration标签配置Mojo属性,还可以使用executions标签,示例如下:

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <artifactId>maven-myquery-plugin</artifactId>
 7         <version>1.0</version>
 8         <executions>
 9           <execution>
10             <id>execution1</id>
11             <phase>test</phase>
12             <configuration>
13               <url>http://www.foo.com/query</url>
14               <timeout>10</timeout>
15               <options>
16                 <option>one</option>
17                 <option>two</option>
18                 <option>three</option>
19               </options>
20             </configuration>
21             <goals>
22               <goal>query</goal>
23             </goals>
24           </execution>
25           <execution>
26             <id>execution2</id>
27             <configuration>
28               <url>http://www.bar.com/query</url>
29               <timeout>15</timeout>
30               <options>
31                 <option>four</option>
32                 <option>five</option>
33                 <option>six</option>
34               </options>
35             </configuration>
36             <goals>
37               <goal>query</goal>
38             </goals>
39           </execution>
40         </executions>
41       </plugin>
42     </plugins>
43   </build>
44   ...
45 </project>

第一个id为execution1的execution标签使用phase设置为测试阶段。而第二个id为execution2的execution没有指定phase标签,那么这个插件就是一个默认阶段。

使用 dependencies 标签

可以使用dependencies标签来修改以来的最新版本。如下示例:

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-antrun-plugin</artifactId>
 8         <version>1.2</version>
 9         ...
10         <dependencies>
11           <dependency>
12             <groupId>org.apache.ant</groupId>
13             <artifactId>ant</artifactId>
14             <version>1.7.1</version>
15           </dependency>
16           <dependency>
17             <groupId>org.apache.ant</groupId>
18             <artifactId>ant-launcher</artifactId>
19             <version>1.7.1</version>
20           </dependency>
21          </dependencies>
22       </plugin>
23     </plugins>
24   </build>
25   ...
26 </project>

使用 inherited 标签

默认情况下插件的配置信息会传递给子POM,为了中断继承的传递性。可以使用inherited标签,如下示例,子POM中就不会继承该插件。

 1 <project>
 2   ...
 3   <build>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-antrun-plugin</artifactId>
 8         <version>1.2</version>
 9         <inherited>false</inherited>
10         ...
11       </plugin>
12     </plugins>
13   </build>
14   ...
15 </project>

配置 Reporting 插件

使用 reportSets 标签

这个标签可以指定Maven 执行mvn site 时只适用于指定的项目。如下示例只是用project team项目。

 1 <project>
 2   ...
 3   <reporting>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-project-info-reports-plugin</artifactId>
 8         <version>2.1.2</version>
 9         <reportSets>
10           <reportSet>
11             <reports>
12               <report>project-team</report>
13             </reports>
14           </reportSet>
15         </reportSets>
16       </plugin>
17     </plugins>
18   </reporting>
19   ...
20 </project>

注:使用<reports/>可以排除所有的项目。

使用 inherited 标签

该标签和build的类似,用于中断继承的传递性的。如下示例,插件不会传递到子POM。

 1 <project>
 2   ...
 3   <reporting>
 4     <plugins>
 5       <plugin>
 6         <groupId>org.apache.maven.plugins</groupId>
 7         <artifactId>maven-project-info-reports-plugin</artifactId>
 8         <version>2.1.2</version>
 9         <inherited>false</inherited>
10       </plugin>
11     </plugins>
12   </reporting>
13   ...
14 </project>