生命周期
主要有以下几步:
清理、初始化、编译、测试、打包、集成测试、验证、部署和站点等
三套独立的生命周期:
- clean:清理项目
- default:构建项目
- site:建立项目站点
每套生命周期里由分一些阶段
(官网详细介绍:官网生命周期介绍link)
- clean
1. pre-clean:执行一些清理前需要的准备工作
2. clean:清理上一次构建的文件
3. post-clean:执行清理后的一些工作
- default
1. validate:验证是否正确和必要的情报验证
2. initialize:初期化
3. generate-sources:
4. process-sources:处理一些source code, /src/main/reources
5. generate-resources:
6. process-resources:处理一些资源文件,如copy、打包文件夹建立
7. compile:编译项目的source code,/src/main/java
8. process-classes:
9. generate-test-sources
10. process-test-sources: 如:/src/test/reources
11. generate-test-resources
12. porcess-test-resources:
13. test-compile:如:/src/test/java
14. process-test-classes
15. test:运行测试,测试代码不会被打包或部署
16. prepare-package
17. package:接受编译好的 代码,打包成发布的格式
18. pre-integration-test
19. integration-test
20. post-integration-test
21. verify
22. install:将报安装到本地仓库
23. deploy:将最终的包复制到远程仓库中,供其他开发人员和maven项目使用
- site
1. pre-site:执行生成项目站点前准备工作
2. site:生成项目站点文档
3. post-site:执行生成项目站点后需要完成的工作
4. site-deploy:将生成的项目站点发布到服务器上
命令与生命周期的关系:
每套生命周期中,后面的阶段必须依赖前面的阶段(前面的阶段执行了,才会执行后面的阶段)。而三套每套生命周期没有必然的联系,执行default不会执行clean中的阶段。
如:
mvn clean:会执行clean中的1,2(pre-clean,clean)
mvn test: 会执行default中1-15(validate到test)
mvn clean install:会执行clean中的1,2(pre-clean,clean)和 default中1-22(validate到install)
mvn clean deploy site-deploy:会执行clean中的1,2 和 default中1-22 和 site中的1-4
插件:
Maven的核心仅仅定义了抽象的生命周期,具体工作必须由插件完成。插件以独立的形式存在,所以maven的核心包只有几M的大小。
Maven的生命周期与插件相互绑定,才能完成构建任务。
绑定:
绑定分为两种:内置绑定,手动绑定(自定义绑定)
内置绑定
使用已经绑定好的插件。生命周期 阶段(phase) 插件目标 备注 clean pre-clean clean clean maven-clean-plugin:clean clean post-clean default process-resources maven-resources-plugin:resources copy主资源文件到输出目录 default compile maven-compiler-plugin:compile 编译主代码至主输出目录 default process-test-resources maven-resources-plugin:testResources copy测试主资源文件到测试输出目录 default test-compile maven-compiler-plugin:testCompile 编译测试代码值测试输出目录 default test maven-surefire-plugin:test 执行测试用例 default package maven-jar-plugin:jar 创建项目jar包 default install maven-install-plugin:install 将项目输出构件安装到本地仓库 default deploy maven-deploy-plugin:deploy 将项目输出构件部署到远程仓库 site pre-site site site maven-site-plugin:site site post-site site siete-deploy maven-site-plugin:deploy 说明:default生命周期还有其他的阶段,默认它们没有绑定任何插件,因此也没有任何实际行为。
手动绑定
自己手动修改绑定。通过<build>里<plugins><plugin>标签来配置。案列一:代码打包:相关官网link
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <configuration> <outputDirectory>/absolute/path/to/the/output/directory</outputDirectory> <finalName>filename-of-generated-jar-file</finalName> <attach>false</attach> </configuration> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
配置了一个id为attach-sources的任务,通过配置<phase>类将其绑定到default生命周期的verify阶段,再通过<goals>
指定执行的插件目标。
其中<phase>可以不用指定,因为有默认绑定到package中。官网对其属性说明如下: Attributes: Requires a Maven project to be executed. The goal is thread-safe and supports parallel builds. Since version: 2.0.3. Binds by default to the lifecycle phase: package. Invokes the execution of the lifecycle phase generate-sources prior to executing itself. 其中:Binds by default to the lifecycle phase: package.说明了,默认为package。
案列二:war包的配置:相关官网link
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <webResources> <resource> <!-- this is relative to the pom.xml directory --> <directory>resource2</directory> </resource> </webResources> </configuration> </plugin> </plugins> </build>
配置:
命令行配置
例如:maven-surefire-plugin提供了maven.test.skip的参数,当设置为true,就可以跳过执行测试。cmd命令:mvn install -Dmaven.test.skip=true
说明:-D是java自带,功能:通过命令行设置一个系统属性。
POM中全局配置
使用jdk1.8<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <verbose>true</verbose> <fork>true</fork> <executable>${JAVA_1_8_HOME}/bin/javac</executable> <compilerVersion>1.8</compilerVersion> </configuration> </plugin> </plugins> </build>
POM中任务配置
如:antrun的不同task配置(具体可以看官网的例子):
https://maven.apache.org/plugins/maven-antrun-plugin/
总结:主要讲了生命周期和插件。生命周期分为clean、default、site,每个里面又有不同的阶段(phase)。maven的的实际行为是通过插件目标来执行的,而插件需要与生命周期进行绑定才会有实际意义,绑定有分为内置绑定和手动绑定。