maven拥有3套相互独立的生命周期,分别为clean,default,site
clean生命周期目的是清理项目,
default生命周期是构件项目
site生命周期是建立项目站点。
每个生命周期包含一些阶段(phase),这些阶段都是有序的,且后面的阶段依赖前面的阶段。用户和maven最直接的交互方式是调用这些生命周期阶段。
clean生命周期
pre-clean:执行一些清理前需要完成的工作
clean:清理上一次构件生成的文件
post-clean:执行一些清理后需要完成的工作
default生命周期
validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile 编译项目
process-classes
generate-test-resources
process-test-sources
generate-test-resources
process-test-resources
test-compile
test 使用单元测试框架测试项目
prepare-package
package 打包好发布格式
pre-integration-test
integration-test
post-integration-test
verify
install 安装到本地仓库
deploy 部署到远程仓库
site生命周期
pre-site:执行生成站点之前需要完成工作
site:生成项目站点文档
post-site:执行生成站点之后需要完成的工作
site-deploy:将生成的站点发布到服务器上
插件目标
maven的核心仅仅定义了抽象的生命周期,具体的任务由插件完成,maven会在需要的时候下载并使用插件。
插件绑定
maven的生命周期与插件相互绑定,用以完成实际的构件任务。
自定义绑定
除了内置绑定外,用户还能选择某个插件目标绑定到生命周期的某个阶段上
<build>
<
plugins>
<
plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.1</version>
<
executions>
<
execution>
<id>attach-sources</id>
<
phase>verify</phase>
<
goals>
<
goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在POM的build元素下的plugins子元素中声明插件的使用。
<execution>子元素用来配置一个执行任务。
<phase>元素将插件绑定到指定生命周期上,
通过goals指定要执行的插件目标
获取插件信息
在线插件信息
主要的Maven插件都来自Apache(http://maven.apache.org/plugins/index.html)和Codehaus(http://mojo.codehaus.org/plugins.html)
使用maven-help-plugin查看插件信息
如:$ mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1
这里执行的是maven-help-plugin的discribe目标
在描述插件的时候可以省略版本信息
$ mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin
也可以使用插件前缀(Goal Prefix)替换坐标
$ mvn help:describe -Dplugin=compiler
仅仅查看插件目标信息
$ mvn help:describe -Dplugin=compiler -Dgoal=compile
查看插件详细信息
$ mvn help:describe -Dplugin=compiler -Ddetail
插件仓库
maven会区别对待依赖的远程仓库与插件的远程仓库,配置插件的远程仓库使用<pluginRepositories><pluginRepository>元素
<
pluginRepositories>
<
pluginRepository>
<id>central</id>
<url>http://repo1.maven.org.maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>