通过mvn 插件名称:插件目标运行
,如要运行原型插件的create目标
mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook
其中插件由maven库提供,也可以自己写插件,并搭建maven库。
默认的mvn库提供了一些常用插件。
每个插件都包含了多个目标,可以根据自己需要决定使用哪个目标
生命周期(Lifecycle)
maven管理的项目由多个生命周期阶段组成整个生命周期,生命周期中的阶段是有序的,可以根据需要,决定当前需要运行的寿命周期,maven默认配置提供了一组默认生命周期阶段。
生命周期阶段实际上是内部封装了对插件目标的调用。
常见的有
resources:resources
Resources插件的resources目标绑定到了 resources 阶段。这个目标复制src/
main/resources下的所有资源和其它任何配置的资源目录,到输出目录。
compiler:compile
Compiler插件的compile目标绑定到了 compile 阶段。这个目标编译src/main/
java下的所有源代码和其他任何配置的资源目录,到输出目录。
resources:testResources
Resources插件的testResources目标绑定到了 test-resources 阶段。这个目标复
制src/test/resources下的所有资源和其它任何的配置的测试资源目录,到测试
输出目录。
compiler:testCompile
Compiler插件的testCompile目标绑定到了 test-compile 阶段。这个目标编
译src/test/java下的测试用例和其它任何的配置的测试资源目录,到测试输出
目录。
surefire:test
Surefire插件的test目标绑定到了 test 阶段。这个目标运行所有的测试并且创
建那些捕捉详细测试结果的输出文件。默认情况下,如果有测试失败,这个目标
会终止。
jar:jar
Jar插件的jar目标绑定到了 package 阶段。这个目标把输出目录打包成JAR文
件。
清理生命周期 (clean)
运行mvn clean将调用清理
生命周期,它包含了三个生命周期阶段:
• pre-clean
• clean
• post-clean
Clean插件的clean目标
( clean:clean)被绑定到清理生命周期中的clean阶段。目标clean:clean通过删除构建
目录删除整个构建的输出。如果你没有自定义构建目录位置,那么构建目录就是定义
在超级POM中的target
它包含了三个生命周期阶段:
• pre-clean
• clean
• post-clean
例如,假设你想要在pre-clean的时候触发一个antrun:run目标任务
<project> ... <build> <plugins>... <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>file-exists</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <!-- adds the ant-contrib tasks (if/then/else used below) --> <taskdef resource="net/sf/antcontrib/antcontrib.properties" /> <available file="/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/content-zh/target/book.jar" property="file.exists" value="true" /> <if> <not> <isset property="file.exists" /> </not> <then> <echo>No book.jar to delete</echo> </then> <else> <echo>Deleting book.jar</echo> </else> </if> </tasks> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>ant-contrib</groupId> <artifactId>ant-contrib</artifactId> <version>1.0b2</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
也可以自定义clean目标的行为来完成
<project> <modelVersion>4.0.0</modelVersion> ... <build> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <configuration> <filesets> <fileset> <directory>target-other</directory> <includes> <include>*.class</include> </includes> </fileset> </filesets> </configuration> </plugin> </plugins> </build> </project>
默认生命周期(default)
默认生命周期是一个软件应用程序构建过程的总
体模型。第一个阶段是validate,最后一个阶段是deploy。
他包含了如下阶段
生命周期阶段 | 描述 |
validate | 验证项目是否正确,以及所有为了完整构建必要的信息是否可用 |
generate-sources | 生成所有需要包含在编译过程中的源代码 |
process-sources | 处理源代码,比如过滤一些值 |
generate-resources | 生成所有需要包含在打包过程中的资源文件 |
process-resources | 复制并处理资源文件至目标目录,准备打包 |
compile | 编译项目的源代码 |
process-classes | 后处理编译生成的文件,例如对Java类进行字节码增强bytecode enhancement) |
generate-test-sources | 生成所有包含在测试编译过程中的测试源码 |
process-test-sources | 处理测试源码,比如过滤一些值 |
generate-test-resources | 生成测试需要的资源文件 |
process-test-resources | 复制并处理测试资源文件至测试目标目录 |
test-compile | 编译测试源码至测试目标目录 |
test | 使用合适的单元测试框架运行测试。这些测试应该不需要代码被打包或发布 |
prepare-package | 在真正的打包之前,执行一些准备打包必要的操作。这通常会产生一个包的展开的处理过的版本(将会在Maven 2.1+中实现) |
package | 将编译好的代码打包成可分发的格式,如JAR,WAR,或者EA |
pre-integration-test | 执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境 |
integration-test | 如果有必要的话,处理包并发布至集成测试可以运行的环境 |
post-integration-test | 执行一些在集成测试运行之后需要的动作。如清理集成测试环境。 |
verify | 执行所有检查,验证包是有效的,符合质量规范 |
install | 安装包至本地仓库,以备本地的其它项目作为依赖使用 |
deploy | 复制最终的包至远程仓库,共享给其它开发人员和项目(通常和一次正式的发布相关) |
站点生命周期(site)
生成项目文档和报告。
它包含了四个阶段:
1. pre-site
2. site
3. post-site
4. site-deploy
默认绑定到站点生命周期的插件目标是:
1. site - site:site
2. site-deploy -site:deploy
打包生命周期
绑定到每个阶段的特定目标默认根据项目的打包类型设置。一个打包类型为jar的项
目和一个打包类型为war的项目拥有不同的两组默认目标。 packaging元素影响构建一个项目需要的步骤。举个打包如何影响构建的例子,考虑有两个项目:一个打包类型是pom,另外一个是jar。在package阶段,打包类型为pom的项目会运行site:attachdescriptor目标,而打包类型为jar的项目会运行jar:jar目标。
JAR是默认的打包类型,是最常用的,因此也就是生命周期配置中最经常遇到的打包类型。JAR生命周期默认的插件目标如
生命周期阶段 | 目标 |
process-resources | resources:resource |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | jar:jar |
install | install:install |
deploy | deploy:deploy |
POM是最简单的打包类型。不像一个JAR,SAR,或者EAR,它生成的构件只是它本身。没有代码需要测试或者编译,也没有资源需要处理。打包类型为POM的项目的默认目标
生命周期阶段 | 目标 |
package | site:attach-descriptor |
install | install:install |
deploy | deploy:deploy |
Maven Plugin
该打包类型和JAR打包类型类似,除了三个目
标: plugin:descriptor, plugin:addPluginArtifactMetadata,
和plugin:updateRegistry。这些目标生成一个描述文件,对仓库数据执行一些修改。
生命周期阶段 | 目标 |
generate-resources | plugin:descriptor |
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | jar:jar,plugin:addPluginArtifactMetadata |
install | install:install,plugin:updateRegistry |
deploy | deploy:deploy |
EJB
忽略,特么
WAR
打包类型和JAR以及EJB类似。例外是这里的package目标是war:war。注意war:war插件需要一个web.xml配置文件在项目的src/main/webapp/WEB-INF目录中。
生命周期阶段 | 目标 |
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | war:war |
install | install:install |
deploy | deploy:deploy |
其它打包类型
可以上打包插件官网查看
坐标
Maven坐标定义了一组标识,它们可以用来唯一标识一个项目,一个依赖,或者Maven
POM里的一个插件。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--本项目坐标--> <groupId>cgodo</groupId> <artifactId>cgodo_lang</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>cgodo_lang</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <!--引用项目坐标--> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
一个项目的groupId:artifactId:version使之成为一个独一无二的项目;你不能同
时有一个拥有同样的groupId, artifactId和version标识的项目。
虽然‘.’在groupId中很常用,而你应该避免在artifactId中使用
它。因为在解析一个完整限定名字至子模块的时候,这会引发问题。
packaging项目类型,默认是jar,如果打包后的项目是其他类型如war,则写war
maven仓库
默认使用的是http://repo1.maven.org/maven2/
未下载的依赖会下载到/${user.home}/.m2/repository
依赖
具有传递性
site
site生命周期只生成站点,不进行编译,是一个独立的生命周期阶段。该站点根据pom生产对应的项目描述信息