第一節(jié) Why ? 為什么學(xué)習(xí) Maven ?
-
1、Maven 作為依賴管理工具
- ①jar 包的規(guī)模
- ②jar 包的來源
- ③jar 包之間的依賴關(guān)系
-
2、Maven 作為構(gòu)建管理工具
- ①你沒有注意過的構(gòu)建
- ②脫離 IDE 環(huán)境仍需構(gòu)建
3、結(jié)論
1、Maven 作為依賴工具
1. jar 包的規(guī)模
1. 隨著我們使用越來越多的框架,或者框架封裝程度越來越高,項(xiàng)目中使用的jar包也越來越多。項(xiàng)目中,一個(gè)模塊里面用到上百個(gè)jar包是非常正常的。
比如下面的例子,我們只用到 SpringBoot、SpringCloud 框架中的三個(gè)功能:
* Nacos 服務(wù)注冊發(fā)現(xiàn)
* Web 框架環(huán)境
* 圖模板技術(shù) Thymeleaf
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
io.github.openfeign.form:feign-form-spring:jar:3.8.0:compile
io.github.openfeign.form:feign-form:jar:3.8.0:compile
com.netflix.ribbon:ribbon-loadbalancer:jar:2.3.0:compile
org.apache.httpcomponents:httpcore:jar:4.4.13:compile
org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.4.RELEASE:compile
org.slf4j:jul-to-slf4j:jar:1.7.30:compile
com.atguigu.demo:demo09-base-entity:jar:1.0-SNAPSHOT:compile
org.yaml:snakeyaml:jar:1.26:compile
org.springframework.boot:spring-boot-starter-logging:jar:2.3.6.RELEASE:compile
io.reactivex:rxnetty-contexts:jar:0.4.9:runtime
org.apache.httpcomponents:httpclient:jar:4.5.13:compile
io.github.openfeign:feign-core:jar:10.10.1:compile
org.springframework.boot:spring-boot-starter-aop:jar:2.3.6.RELEASE:compile
org.hdrhistogram:HdrHistogram:jar:2.1.9:compile
org.springframework:spring-context:jar:5.2.11.RELEASE:compile
commons-lang:commons-lang:jar:2.6:compile
io.prometheus:simpleclient:jar:0.5.0:compile
ch.qos.logback:logback-core:jar:1.2.3:compile
org.springframework:spring-webmvc:jar:5.2.11.RELEASE:compile
com.sun.jersey:jersey-core:jar:1.19.1:runtime
javax.ws.rs:jsr311-api:jar:1.1.1:runtime
javax.inject:javax.inject:jar:1:runtime
org.springframework.cloud:spring-cloud-openfeign-core:jar:2.2.6.RELEASE:compile
com.netflix.ribbon:ribbon-core:jar:2.3.0:compile
com.netflix.hystrix:hystrix-core:jar:1.5.18:compile
com.netflix.ribbon:ribbon-transport:jar:2.3.0:runtime
org.springframework.boot:spring-boot-starter-json:jar:2.3.6.RELEASE:compile
org.springframework.cloud:spring-cloud-starter-openfeign:jar:2.2.6.RELEASE:compile
com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.3:compile
com.sun.jersey.contribs:jersey-apache-client4:jar:1.19.1:runtime
io.github.openfeign:feign-hystrix:jar:10.10.1:compile
io.github.openfeign:feign-slf4j:jar:10.10.1:compile
com.alibaba.nacos:nacos-client:jar:1.4.2:compile
org.apache.httpcomponents:httpcore-nio:jar:4.4.13:compile
com.sun.jersey:jersey-client:jar:1.19.1:runtime
org.springframework.cloud:spring-cloud-context:jar:2.2.6.RELEASE:compile
org.glassfish:jakarta.el:jar:3.0.3:compile
org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.3:compile
org.springframework.cloud:spring-cloud-commons:jar:2.2.6.RELEASE:compile
org.aspectj:aspectjweaver:jar:1.9.6:compile
com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:jar:2.2.6.RELEASE:compile
com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
com.alibaba.spring:spring-context-support:jar:1.0.10:compile
jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
org.bouncycastle:bcpkix-jdk15on:jar:1.64:compile
com.netflix.netflix-commons:netflix-commons-util:jar:0.3.0:runtime
com.fasterxml.jackson.core:jackson-annotations:jar:2.11.3:compile
com.google.guava:guava:jar:29.0-jre:compile
com.google.guava:failureaccess:jar:1.0.1:compile
org.springframework.boot:spring-boot:jar:2.3.6.RELEASE:compile
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.3:compile
com.atguigu.demo:demo08-base-api:jar:1.0-SNAPSHOT:compile
org.springframework.cloud:spring-cloud-starter-netflix-archaius:jar:2.2.6.RELEASE:compile
org.springframework.boot:spring-boot-autoconfigure:jar:2.3.6.RELEASE:compile
org.slf4j:slf4j-api:jar:1.7.30:compile
commons-io:commons-io:jar:2.7:compile
org.springframework.cloud:spring-cloud-starter:jar:2.2.6.RELEASE:compile
org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.39:compile
io.reactivex:rxnetty:jar:0.4.9:runtime
com.fasterxml.jackson.core:jackson-core:jar:2.11.3:compile
com.google.code.findbugs:jsr305:jar:3.0.2:compile
com.netflix.archaius:archaius-core:jar:0.7.6:compile
org.springframework.boot:spring-boot-starter-web:jar:2.3.6.RELEASE:compile
commons-codec:commons-codec:jar:1.14:compile
com.netflix.servo:servo-core:jar:0.12.21:runtime
com.google.errorprone:error_prone_annotations:jar:2.3.4:compile
org.attoparser:attoparser:jar:2.0.5.RELEASE:compile
com.atguigu.demo:demo10-base-util:jar:1.0-SNAPSHOT:compile
org.checkerframework:checker-qual:jar:2.11.1:compile
org.thymeleaf:thymeleaf-spring5:jar:3.0.11.RELEASE:compile
commons-fileupload:commons-fileupload:jar:1.4:compile
com.netflix.ribbon:ribbon-httpclient:jar:2.3.0:compile
com.netflix.netflix-commons:netflix-statistics:jar:0.1.1:runtime
org.unbescape:unbescape:jar:1.1.6.RELEASE:compile
org.springframework:spring-jcl:jar:5.2.11.RELEASE:compile
com.alibaba.nacos:nacos-common:jar:1.4.2:compile
commons-collections:commons-collections:jar:3.2.2:runtime
javax.persistence:persistence-api:jar:1.0:compile
com.alibaba.nacos:nacos-api:jar:1.4.2:compile
org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile
org.springframework:spring-aop:jar:5.2.11.RELEASE:compile
org.springframework.boot:spring-boot-starter:jar:2.3.6.RELEASE:compile
org.springframework.boot:spring-boot-starter-tomcat:jar:2.3.6.RELEASE:compile
org.springframework.cloud:spring-cloud-netflix-ribbon:jar:2.2.6.RELEASE:compile
org.springframework:spring-expression:jar:5.2.11.RELEASE:compile
org.springframework.cloud:spring-cloud-netflix-archaius:jar:2.2.6.RELEASE:compile
而使用 Maven 引入這些 jar 包 只需要配置三個(gè) 【依賴】
<!-- Nacos 服務(wù)注冊發(fā)現(xiàn)驅(qū)動(dòng) -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Web 啟動(dòng)器依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 視圖模板技術(shù) thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. jar 包的來源
1. 這個(gè)jar包所屬技術(shù)的官網(wǎng)。官網(wǎng)通常是英文界面,網(wǎng)站的結(jié)構(gòu)又不盡相同,甚至找到下載鏈接還發(fā)現(xiàn)需要通過特殊的工具下載。
2. 第三方網(wǎng)站提供下載。問題是不規(guī)范,在使用過程中會出現(xiàn)各種問題。
1. jar包的名稱
2. jar包的版本
3. jar包內(nèi)的具體細(xì)節(jié)
3. 而使用 Maven 后,依賴對應(yīng)的 jar 包能夠**自動(dòng)下載**,方便、快捷又規(guī)范。
3. jar 包之間的依賴關(guān)系
1. 框架中使用的 jar 包,不僅數(shù)量龐大,而且彼此之間存在**錯(cuò)綜復(fù)雜的依賴關(guān)系**。依賴關(guān)系的復(fù)雜程度,已經(jīng)上升到了完全不能靠人力手動(dòng)解決的程度。另外,**jar 包之間**有可能產(chǎn)生**沖突**。進(jìn)一步增加了我們在 jar 包使用過程中的難度。
2. 下面是前面例子中 jar 包之間的依賴關(guān)系:

3. 而實(shí)際上** jar 包之間的依賴關(guān)系是普遍存在的**,如果要由程序員手動(dòng)梳理無疑會增加極高的學(xué)習(xí)成本,而這些工作又對實(shí)現(xiàn)業(yè)務(wù)功能毫無幫助。
4. 而使用 Maven 則幾乎不需要管理這些關(guān)系,極個(gè)別的地方調(diào)整一下即可,**極大的減輕了我們的工作量**。
2、Maven 作為構(gòu)建管理工具
1. 你沒有注意過的構(gòu)建
你可以不使用 Maven,但是構(gòu)建必須要做。當(dāng)我們使用 IDEA 進(jìn)行開發(fā)時(shí),構(gòu)建是 IDEA 替我們做的。
2. 脫離 IDE 環(huán)境仍需構(gòu)建

3、結(jié)論
- 管理規(guī)模龐大的 jar 包,需要專門工具。
- 脫離 IDE 環(huán)境執(zhí)行構(gòu)建操作,需要專門工具。
第二節(jié) What ? 什么是 Maven ?
- 1、構(gòu)建
- 2、依賴
- 3、Maven 的工作機(jī)制
Maven 是 Apache 軟件基金會組織維護(hù)的一款專門為 Java 項(xiàng)目提供構(gòu)建和依賴管理支持的工具。

1、構(gòu)建
Java 項(xiàng)目開發(fā)過程中,構(gòu)建指的是使用『原材料生產(chǎn)產(chǎn)品』的過程。
- 原材料
- Java 源代碼
- 基于 HTML 的 Thymeleaf 文件
- 圖片
- 配置文件
- 。。。。。
- 產(chǎn)品
- 一個(gè)可以在服務(wù)器上運(yùn)行的項(xiàng)目
構(gòu)建過程包含的主要的環(huán)節(jié):
- 清理:刪除上一次構(gòu)建的結(jié)果,為下一次構(gòu)建做好準(zhǔn)備
- 編譯:Java 源程序編譯成** *.class** 字節(jié)碼文件
- 測試:運(yùn)行提前準(zhǔn)備好的測試程序
- 報(bào)告:針對剛才測試的結(jié)果生成一個(gè)全面的信息
-
打包
- 普通的Java工程:jar包
- Web工程:war包
- 安裝:把一個(gè) Maven 工程經(jīng)過打包操作生成的 jar 包或 war 包存入 Maven 倉庫
-
部署
- 部署 jar 包:把一個(gè) jar 包部署到 Nexus 私服服務(wù)器上
- 部署 war 包:借助相關(guān)** Maven 插件**(例如 cargo),將 war 包部署到 Tomcat 服務(wù)器上
2、依賴
如果 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 包會從規(guī)范的遠(yuǎn)程倉庫下載到本地
- jar 包之間的依賴:通過依賴的傳遞性自動(dòng)完成
- jar 包之間的沖突:通過對依賴的配置進(jìn)行調(diào)整,讓某些jar包不會被導(dǎo)入
3、Maven 的工作機(jī)制
