Maven扫盲
# 1:Maven官网(官方文档是学习工具的最好参考书)
Maven官网 (opens new window) 菜鸟教程-maven教程 (opens new window)
# 2:maven官网定义
官方解释:Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM),Maven can manage a project's build, reporting and documentation from a central piece of information (Apache Maven是一个软件项目管理和理解工具 。基于项目对象模型(POM)的概念,Maven可以从一个中心信息管理项目的构建,报告和文档)
我目前所用到的Maven的主要作用就是:
- 添加第三方jar包;
- jar包之间的依赖关系;
- 获取第三方jar包;
- 将整个项目拆分成多个工程模块;
- 部署,测试报告;
但是maven是一个强大工具( 远远不止我提到的这几个基础的功能,其中就包含项目的构建,报告和文档,官方解释地址:https://maven.apache.org/maven-features.html (opens new window))
# 3:Maven下载地址
https://maven.apache.org/download.cgi (opens new window)
# 4:maven安装
# 4.1:解压部署Maven核心程序
检查JAVA_HOME环境变量 首先确保你的JDK已经正确安装;
解压Maven的核心程序 将apache-maven-3.5.3-bin.zip(在项目中以上传元安装包)解压到一个非中文无空格的目录下。例如:D: \DevInstall\apache-maven-3.5.3
配置环境变量 M2_HOME D:\DevInstall\apache-maven-3.5.3 path D:\DevInstall\apache-maven-3.5.3\bin
查看Maven版本信息验证安装是否正确 C:\Windows\System32>mvn -v Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00) Maven home: D:\Maven\apache-maven-3.5.3\bin.. Java version: 1.8.0_131, vendor: Oracle Corporation Java home: D:\Program Files\Java\jdk1.8.0_131\jre Default locale: zh_CN, platform encoding: GBK OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
# 4.2:修改本地仓库
- 默认本地仓库位置:~.m2\repository,~表示当前用户的家目录,例如:C:\Users[你当前登录系统的用户名]
- 指定本地仓库位置的配置信息文件:apache-maven-3.5.3\conf\settings.xml
- 在根标签settings下添加如下内容:<localRepository>[本地仓库路径,也就是RepMaven.zip的解压目录]
# 5:官网五分钟教程
# 5.1:保证安装好Maven(参考本文4:maven安装)
# 5.2:首先你要知道Maven的一个项目结构
my-app
|-- pom.xml project's Project Object Model, or POM. 项目对象模型文件
|-- src
|-- main
|-- java Application/Library sources 应用程序/库源
|-- resources Application/Library resources 应用程序/库资源
|-- filters Resource filter files 资源过滤文件
|-- webapp Web application sources Web应用程序源
|-- test
|--java Test sources 测试来源
|--resources Test sources 测试资源
|--filters Test resource filter files 测试资源过滤器文件
|--it Integration Tests (primarily for plugins) 集成测试(主要用于插件)
|--assembly Assembly descriptors 程序装配集成描述
|--site Site
LICENSE.txt Project's license 项目许可证
NOTICE.txt Notices and attributions required by libraries that the project depends on 项目依赖的库和其他注意的问题
README.txt 项目提要
# 5.3:在建成你的项目之后,一定记住拷贝一份官方的pom.xml放到其指定的位置
eg:pom.xml
<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>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
该项目的POM目录下打开命令行窗口执行一段shell脚本:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
第一次运行可能会花销一些时间下载很多maven依赖的包
# 5.4.POM文件的关键元素解释:
<project> This is the top-level element in all Maven pom.xml files.
(maven项目中最高级别的的pom.xml文件元素节点)
<modelVersion> This element indicates what version of the object model this POM
is using.The version of the model itself changes very infrequently but it is mandatory
in order to ensure stability of use if and when the Maven developers deem it necessary
to change the model.(工程对象模型的版本号,不经常改变但是开发者可以根据需要改变,存在就是为了确保项目的稳定性)
<groupId> This element indicates the unique identifier of the organization or group
that created the project.The groupId is one of the key identifiers of a project and
is typically based on the fully qualified domain name of your organization.For example
org.apache.maven.plugins is the designated groupId for all Maven plugins.
(创建项目的组织或者组的唯一标识符,一般都是用组织的全限定域名)
<artifactId> This element indicates the unique base name of the primary artifact being
generated by this project.The primary artifact for a project is typically a JAR file.
Secondary artifacts like source bundles also use the artifactId as part of their final name.
A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension>
(for example, myapp-1.0.jar).
(由项目生成的唯一主要工件的基本名称(模块名称),主要比如我们打包的时候的生成的包名)
<packaging> This element indicates the package type to be used by this artifact
(e.g. JAR, WAR, EAR, etc.).This not only means if the artifact produced is JAR, WAR,
or EAR but can also indicate a specific lifecycle to use as part of the build process.
(The lifecycle is a topic we will deal with further on in the guide. For now, just keep
in mind that the indicated packaging of a project can play a part in customizing the build
lifecycle.) The default value for the packaging element is JAR so you do not have to specify
this for most projects.(就是我们打包的类型,eg: jar包,war包.ear包,默认是jarb包)
<version> This element indicates the version of the artifact generated by the project.
Maven goes a long way to help you with version management and you will often see the SNAPSHOT
designator in a version,which indicates that a project is in a state of development. We will
discuss the use of snapshots and how they work further on in this guide.
(由项目生成的工件版本号,有很多版本:eg SNAPSHOT)
<name> This element indicates the display name used for the project. This is often used
in Maven's generated documentation.(项目的显示名称)
<url> This element indicates where the project's site can be found. This is often used in Maven's
generated documentation.(项目的站点位置地址)
<description> This element provides a basic description of your project. This is often used in Maven's
generated documentation.(项目的接本基本描述)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
更多POM.xml元素解释描述文档参考地址:https://maven.apache.org/ref/3.6.0/maven-model/maven.html (opens new window)
# 6:maven常用命令
# 6.1:Maven的常用命令(*注意:运行Maven命令时一定要进入pom.xml文件所在的目录)演示:
mvn compile 编译
编译的内容命令行展示窗口展示内容
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'mirrors' (position: START_TAG seen ...</mirror>\n-->\n\t \n<mirrors>... @160:14)
@ D:\Maven\apache-maven-3.5.3\bin\..\conf\settings.xml, line 160, column 14
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< maven_3:maven_3 >---------------------------
[INFO] Building maven_3 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/4.0/junit-4.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.0/junit-4.0.pom (210 B at 129 B/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_3 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven_3 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\ArnoldSu\Maven-learnning\maven_3_maven项目与常用命令\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.092 s
[INFO] Finished at: 2019-02-01T10:37:11+08:00
[INFO] ------------------------------------------------------------------------
mvn clean 清理
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'mirrors' (position: START_TAG seen ...</mirror>\n-->\n\t \n<mirrors>... @160:14)
@ D:\Maven\apache-maven-3.5.3\bin\..\conf\settings.xml, line 160, column 14
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< maven_3:maven_3 >---------------------------
[INFO] Building maven_3 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven_3 ---
[INFO] Deleting D:\ArnoldSu\Maven-learnning\maven_3_maven项目与常用命令\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.572 s
[INFO] Finished at: 2019-02-01T11:55:52+08:00
[INFO] ------------------------------------------------------------------------
mvn test 测试
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'mirrors' (position: START_TAG seen ...</mirror>\n-->\n\t \n<mirrors>... @160:14)
@ D:\Maven\apache-maven-3.5.3\bin\..\conf\settings.xml, line 160, column 14
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< maven_3:maven_3 >---------------------------
[INFO] Building maven_3 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven_3 ---
[INFO] Deleting D:\ArnoldSu\Maven-learnning\maven_3_maven项目与常用命令\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.572 s
[INFO] Finished at: 2019-02-01T11:55:52+08:00
[INFO] ------------------------------------------------------------------------
mvn package 打包
打包的内容命令行展示窗口展示内容
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'mirrors' (position: START_TAG seen ...</mirror>\n-->\n\t\n<mirrors>...
@160:14) @ D:\Maven\apache-maven-3.5.3\bin\..\conf\settings.xml, line 160, column 14
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< maven_3:maven_3 >---------------------------
[INFO] Building maven_3 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_3 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven_3 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven_3 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\ArnoldSu\Maven-learnning\maven_3_maven项目与常用命令\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven_3 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven_3 ---
[INFO] Surefire report directory: D:\ArnoldSu\Maven-learnning\maven_3_maven项目与常用命令\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.boommob.www.HelloWorldTest
Hello litingwei!Hello litingwei!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.176 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven_3 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.588 s
[INFO] Finished at: 2019-02-01T10:57:07+08:00
[INFO] ------------------------------------------------------------------------
# 6.2:在执行完相应的maven命令之后会在该项目的路径下生成target文件夹:
target
|-- classes 编译之后的类文件
|-- maven-archeiver maven打包时候生成的得一个工程对象模型属性文件里面记载的是该项目的POM.xml的一些相关属性
|-- maven-status maven状态,一些maven编译的插件信息
|-- surefire-reports maven打包生成的一个报告,测试报告
|-- test-classes 编译之后的测试类文件
# 7:maven依赖
# 7.1:首先项目POM.xml添加本文步骤6:maven常用命令中的的依赖,这是我们本地库的一个模块依赖,如果直接在命令行直接执行:mvn compile 的话会报错, 此处需要先在本文步骤6上执行mvn install,将模块安装到本地,在结合maven依赖进行编译.
# 7.2:POM.xml元素解释
dependencies:(Many) This element describes all of the dependencies associated with a project. These dependencies are used to construct a classpath for your project during the build process. They are automatically downloaded from the repositories defined in this project.See the dependency mechanism for more information. ( 此元素描述与项目关联的所有依赖项。这些依赖项用于在构建过程中为项目构造类路径。它们会自动从此项目中定义的存储库下载,参考依赖机制:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html (opens new window));
# 7.3:Dependency Mechanism(依赖机制)
Transitive Dependencies(传递依赖) Dependency mediation(依赖调解),多个版本依赖项nearest definition (最近定义原则:表示所使用的版本将是依赖关系树中与项目最接近的版本。例如,如果A,B和C 的依赖关系定义为A - > B - > C - > D 2.0和A - > E - > D 1.0,则构建A时将使用D 1.0, 因为A的路径到D到E更短.您可以在A中向D 2.0明确添加依赖项以强制使用D 2.0);
Dependency management(依赖管理) ,在3.1的示例中,依赖项直接添加到A,即使它不是由A直接使用。相反,A可以在其dependencyManagement部分中包含D作为依赖项,并直接控制在何时使用D的哪个版本;
Dependency scope(依赖范围),Dependency scope(排除依赖项) ,如果项目X依赖于项目Y,项目Y依赖于项目Z,则项目X的所有者可以使用exclusion元素将项目Z明确地排除为依赖项;
Optional dependencies(可选择的依赖项) ,如果项目Y依赖于项目Z,项目Y的所有者可以使用optional元素,将项目Z标记为可选依赖项。当项目X依赖于项目Y时,X将仅依赖于Y而不依赖于Y的可选依赖项Z.项目X的所有者可以在它的选项中明确地添加对Z的依赖性;
Maven提供dependency:analyze插件,可以帮助最佳实现依赖机制.
# 7.4:Dependency scope(依赖范围)
项目 | 说明 |
---|---|
compile | 默认范围,编译依赖项在项目的所有类路径中都可用,这些依赖项将传播到其他依赖项目 |
provided | 和compile很相似,但是表示您希望JDK或容器在运行时提供依赖项。例如,在为Java Enterprise Edition构建Web应用程序时,您可以将Servlet API和相关Java EE API的依赖关系设置 |
runtime | 此范围表示编译不需要依赖项,但是用于执行。它位于运行时和测试类路径中,但不是编译类路径。 可执行不需要编译的依赖 |
test | 测试编译和执行的依赖,依赖不传递 |
system | 和provided很相似只是您必须提供明确包含它的JAR。可以使用但是不能在库中找到 |
import | import,仅在POM.xml中引入<dependencyManagement >元素才支持此作用域。并且一旦标注为import,就不参与传递依赖 |
每个依赖范围(import除外)以不同方式影响传递依赖性,如下表所示。如果依赖项设置为左列中的作用域,则该依赖项与顶行中作用域的传递依赖性将导致主项目中的依赖项,并在交集处列出作用域。如果未列出范围,则表示将省略依赖关系。
# 7.5:Dependency Management(依赖管理)
集中依赖信息管理,官方图示来说明其机制:两个扩展同一父级的POM 项目A共同依赖group-a,但是第一个依赖排除对grouo-c的依赖,依赖于artifact-a模块,第二个依赖是group-a下的artifact-b
项目B,第一个依赖grouo-c下的artifact-b,第二个依赖是group-a下的artifact-b
用依赖管理<dependencyManagemen>放在其父级项目POM.xml
两个子项目的配置就会简单很多
注意:其中子项目用Dependency Management之后需要指明 < type > 的类型,因为Dependency Management 只针对{groupId, artifactId, type, classifier}为最小单位进行依赖管理
还有个重要的特性就是模块版本控制上面,并且依赖管理的依赖级别是:dependency management transitive dependencies除了继承依赖传递,还可以通过 <import>来导入依赖,在较大的项目中比较实用,并且讲究谁先申明谁且本身pom并未申明版本号,就引用第一个申明的版本号作为当前的版本号.
# 7.6:System Dependencies(系统依赖,已经弃用)
本文的maven记录在github地址:https://github.com/ArnoldShu/Maven-learnning (opens new window)
本文的maven记录在gitee地址:https://gitee.com/ArnoldSu/Maven-learnning (opens new window)