Maven's design philosophy is Convention over configuration (Configuration prior to the convention), in this article, you will learn the following five technical details, these elements I will ask the candidates in a recent interview:
Difference maven clean package, install, deploy the
maven life cycle
maven common plug-ins
maven binding Gitlab Runner executed CI / CD unit test
maven rely exclusion
First, the difference maven clean package, install, deploy the
A watch worth a thousand words, the life of the periodic table
Menu
If using mvn install, new content change does not take effect, then be sure to use mvn clean install to take effect
Plug Description:
maven-war-plugin project will be packaged into war, maven-jar-plugin project will be packaged into jar
The maven-install-plugin will lay the war package into the local development environment maven repository's.
Two, Maven lifecycle
Maven's life cycle is all the build process abstraction and unified , included project to clean up, initialization, compile, test, package, integration testing, validation, deployment, site generates almost all of the processes, etc. ( can be reduced to compile -> test -> packaging -> deployment ). Almost all building projects, can be mapped to such a life cycle.
Maven has three sets of independent life cycle, are clean (clean-up projects), default (building project) and site (establish the project site) , each user can only call clean every stage of the life cycle.
(A) clean life cycle
此周期的目的是清理项目,它包含三个阶段:
(1)pre-clean:执行一些需要在clean之前完成的工作
(2)clean:移除所有上一次构建生成的文件
(3)post-clean:执行一些需要在clean之后立刻完成的工作
(二)default生命周期
最核心的生命周期,构建的核心部分,编译,测试,打包,部署等等。它包含的阶段如下:
初始化、编译、测试、打包、集成测试、验证、部署
1) validate 验证项目是否正确和所有需要的相关资源是否可用
2) initialize 初始化构建
3) generate-sources
4) process-sources 处理源代码
5) generate-resources
6) process-resources 处理项目主资源文件。对src/main/resources目录的内容进行变量替换等工作后,复制到项目输出的主classpath目录中。
7) compile 编译项目的主源代码
8) process-classes
9) generate-test-sources
10) process-test-sources 处理项目测试资源文件
11)generate-test-resources
12) process-test-resources 处理测试的资源文件
13)test-compile 编译项目的测试代码
14)process-test-classes
15) test 使用单元测试框架运行测试,测试代码不会被打包或部署
16)prepare-package 做好打包的准备
17)package 接受编译好的代码,打包成可发布的格式
18) pre-integration-test
19) integration-test
20) postintegration-test
21) verify
22) install 将包安装到Maven本地仓库,供本地其他Maven项目使用
23)deploy 将最终的包复制到远程仓库,供其他开发人员和Maven项目使用
(三)site生命周期
site生命周期的目的是建立和发布项目站点,Maven能够基于POM所包含的信息,自动生成一个友好的站点,方便团队交流和发布项目信息。该生命周期包含如下阶段:
1)pre-site 执行一些在生成项目站点之前需要完成的工作
2)site 生成项目站点文档
3)post-site 执行一些在生成项目站点之后需要完成的工作
4)site-deploy 将生成的项目站点发布到服务器上
三、Maven常用插件
clean插件maven-clean-plugin
resources插件maven-resources-plugin
compile插件maven-compiler-plugin
单元测试插件maven-surefire-plugin
打包插件maven-jar-plugin、maven-war-plugin、maven-assembly-plugin、maven-shade-plugin
发布插件maven-install-plugin
四、maven结合Gitlab Runner执行CI/CD单元测试
Gitlab 除了基本的版本管理功能之外,还有持续集成能力,在项目根目录中编写一段 .gitlab-ci.yml,就能让 Gitlab 按照其中的指示完成持续集成的工作。
Gitlab CI/CD 是 Gitlab 的组件,它利用 Gitlab Runner 来执行具体的构建任务。
具体创建Maven 的 Runner的方式可以参照这篇文章:使用 Maven 运行单元测试
Reproduced in: https: //www.jianshu.com/p/8baad23e7307