前篇
为什么要学习Maven?
一、Maven可以作为依赖管理的工具
①jar包的规模
随着我们使用越来越多的框架,或者框架封装程度越来越高,项目中使用的jar包也越来越多。比如在一个项目中、一个模块里用到上百个jar包是非常正常的。
例如在下面的例子中,我们只用到 SpringBoot、SpringCloud 框架中的三个功能:
- Nacos 服务注册发现
- Web 框架环境
- 视图模板技术 Thymeleaf
最终却导入了 106 个 jar 包:
org.springframework.security:spring-security-rsa:jar:1.0.9.RELEASE:compile com.netflix.ribbon: ribbon:jar:2.3.0:compile org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.3.6.RELEASE:compile commons-configuration:commons-configuration:jar:1.8:compile org.apache.logging.log4j:log4j-api:jar:2.13.3:compile org.springframework:spring-beans:jar:5.2.11.RELEASE:compile org.springframework.cloud:spring-cloud-starter-netflix-ribbon:jar:2.2.6.RELEASE:compile org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.39:compile com.alibaba.cloud:spring-cloud-alibaba-commons:jar:2.2.6.RELEASE:compile org.bouncycastle:bcprov-jdk15on:jar:1.64:compile org.springframework.security:spring-security-crypto:jar:5.3.5.RELEASE:compile org.apache.httpcomponents:httpasyncclient:jar:4.1.4:compile com.google.j2objc:j2objc-annotations:jar:1.3:compile com.fasterxml.jackson.core:jackson-databind:jar:2.11.3:compile io.reactivex:rxjava:jar:1.3.8:compile ch.qos.logback:logback-classic:jar:1.2.3:compile org.springframework:spring-web:jar:5.2.11.RELEASE:compile io.reactivex:rxnetty-servo:jar:0.4.9:runtime org.springframework:spring-core:jar:5.2.11.RELEASE:compile ...
而如果使用 Maven 来引入这些 jar 包只需要配置三个『依赖』:
<!-- Nacos 服务注册发现启动器 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- web启动器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 视图模板技术 thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
②jar包的来源
jar包在使用过程中当然也涉及到各种问题:
- jar包的名称
- jar包的版本
- jar包内的具体细节
而使用 Maven 后,依赖对应的 jar 包能够自动下载,方便、快捷又规范。
③jar 包之间的依赖关系
框架中使用的 jar 包,不仅数量庞大,而且彼此之间存在错综复杂的依赖关系。依赖关系的复杂程度,已经上升到了完全不能靠人力手动解决的程度。另外,jar 包之间有可能产生冲突,进一步增加了我们在 jar 包使用过程中的难度。
下面是前面例子中 jar 包之间的依赖关系:
而实际上 jar 包之间的依赖关系是普遍存在的,如果要由程序员手动梳理无疑会增加极高的学习成本,而这些工作又对实现业务功能毫无帮助。
而使用 Maven 则几乎不需要管理这些关系,极个别的地方调整一下即可,极大的减轻了我们的工作量。
二、Maven 作为构建管理工具
①你没有注意过的构建
你可以不使用 Maven,但是构建必须要做。当我们使用 IDEA 进行开发时,构建是 IDEA 替我们做的。
②脱离 IDE 环境仍需构建
三、结论
- 管理规模庞大的 jar 包,需要专门工具。
- 脱离 IDE 环境执行构建操作,需要专门工具。
什么是Maven?
Maven 是 Apache 软件基金会组织维护的一款专门为 Java 项目提供构建和依赖管理支持的工具。
一、构建
Java 项目开发过程中,构建指的是使用『原材料生产产品』的过程。
-
原材料
- Java 源代码
- 基于 HTML 的 Thymeleaf 文件
- 图片
- 配置文件
- ……
-
产品
- 一个可以在服务器上运行的项目
构建过程包含的主要的环节:
- 清理:删除上一次过构建的结果,为下一次构建做好准备。
- 编译:将JAVA源程序编译为.class字节码文件。
- 测试:运行提前准备好的测试程序。
- 报告:针对刚才的测试提供一个全面的信息。
- 打包:
- java工程:jar包
- web工程:war包
- 安装:把一个Maven工程经过打包生成的jar包或者war包存入Maven的本地仓库
- 部署:
- 部署jar包:把一个jar包部署到Nexus私服服务器上。
- 部署war包:借助相关Maven插件(比如cargo),将war包部署到Tomcat服务器上。
依赖
如果 A 工程里面用到了 B 工程的类、接口、配置文件等等这样的资源,那么我们就可以说 A 依赖 B。例如:
-
junit-4.12 依赖 hamcrest-core-1.3
-
thymeleaf-3.0.12.RELEASE 依赖 ognl-3.1.26
- ognl-3.1.26 依赖 javassist-3.20.0-GA
-
thymeleaf-3.0.12.RELEASE 依赖 attoparser-2.0.5.RELEASE
-
thymeleaf-3.0.12.RELEASE 依赖 unbescape-1.1.6.RELEASE
-
thymeleaf-3.0.12.RELEASE 依赖 slf4j-api-1.7.26
依赖管理中要解决的具体问题:
- jar 包的下载:使用 Maven 之后,jar 包会从规范的远程仓库下载到本地
- jar 包之间的依赖:通过依赖的传递性自动完成
- jar 包之间的冲突:通过对依赖的配置进行调整,让某些jar包不会被导入
Maven 的工作机制
Maven 核心程序解压与配置
Maven官网:maven.apache.org/
下载页面:maven.apache.org/download.cg…
低版本下载:archive.apache.org/dist/maven/…
具体下载地址:dlcdn.apache.org/maven/maven…
解压Maven核心的程序
核心程序压缩包:apache-maven-3.8.7-bin.zip,解压到非中文、没有空格的目录。例如:
核心程序压缩包:apache-maven-3.8.4-bin.zip,解压到非中文、没有空格的
指定本地仓库
本地仓库默认值:用户家目录/.m2/repository。由于本地仓库的默认位置是在用户的家目录下,而家目录往往是在 C 盘,也就是系统盘。将来 Maven 仓库中 jar 包越来越多,仓库体积越来越大,可能会拖慢 C 盘运行速度,影响系统性能。所以建议将 Maven 的本地仓库放在其他盘符下。配置方式如下:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\maven-repository</localRepository>
本地仓库这个目录,我们手动创建一个空的目录即可。
记住:一定要把 localRepository 标签从注释中拿出来。
注意:本地仓库本身也需要使用一个非中文、没有空格的目录。
配置阿里云提供的镜像仓库
Maven 下载 jar 包默认访问境外的中央仓库,而国外网站速度很慢。改成阿里云提供的镜像仓库,访问国内网站,可以让 Maven 下载 jar 包的时候速度更快。配置的方式是:
<!-- <mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror> -->
加入自己的配置:
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
配置 Maven 工程的基础 JDK 版本
如果按照默认配置运行,Java 工程使用的默认 JDK 版本是 1.5,而我们熟悉和常用的是 JDK 1.8 版本。修改配置的方式是:将 profile 标签整个复制到 settings.xml 文件的 profiles 标签内。
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
配置环境变量
检查 JAVA_HOME 配置是否正确:
C:\Users\Administrator>echo %JAVA_HOME%
D:\software\Java
C:\Users\Administrator>java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
配置 MAVEN_HOME
配置path
验证
C:\Users\Administrator>mvn -v
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\software\apache-maven-3.8.4
Java version: 1.8.0_141, vendor: Oracle Corporation, runtime: D:\software\Java\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"