在Maven 使用指南(4): Maven Build 的阶段时介绍过Maven的阶段命令最终是通过Maven的插件来运行的。
Maven采用插件方式的好处是灵活(可以配置),可以扩展(可以开发插件以满足项目Build的需求,比如编译打包项目后,可以通过插件将应用部署到远程服务器等)。
Maven预先定义了很多插件,可以参见http://maven.apache.org/plugins/ ,如果需要另外开发插件,可以参见http://maven.apache.org/plugin-developers/index.html
在Maven中使用插件是通过在pom.xml 中使用plugins 定义,比如 修改缺省的compiler 插件使用Java编译的版本。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
可以看出使用plugin 的方法和定义依赖的非常类似,其中configuration部分用来配置插件参数。具体可以参见插件文档。
Mavne的插件分为两类:
- Build plugins 在Build生命周期中执行,必须定义在<build/>元素下.
- Reporting plugins 在生产site阶段运行,定义在<reporting/> 元素。
和定义依赖一样,使用插件也必须定义groupId, artifactId 和version。