Maven
第一章 為什么使用Maven
-
獲取jar包
- 使用Maven之前,自行在網路中下載jar包,效率較低,如【谷歌、百度、CSDN....】
- 使用Maven之后,統一在一個地址下載資源jar包【阿里云鏡像服務器等...】
-
添加jar包
- 使用Maven之前,將jar復制到專案工程中,jar包添加到專案中,相對浪費存盤空間
- 使用Maven之后,jar包統一存盤Maven本地倉庫,使用坐標方式將jar包從倉庫引入到專案中

- 使用Maven便于解決jar包沖突及依賴問題
第二章 什么是Maven
- Maven字面意:專家、內行
- Maven是一款自動化構建工具,專注服務于Java平臺的專案構建和依賴管理,
- 依賴管理:jar之間的依賴關系,jar包管理問題統稱為依賴管理
- 專案構建:專案構建不等同于專案創建
- 專案構建是一個程序【7步驟組成】,專案創建是瞬間完成的
- 清理:mvn clean
- 編譯:mvn compile
- 測驗:mvn test
- 報告:
- 打包:mvn package
- 安裝:mvn install
- 部署:
- 專案構建是一個程序【7步驟組成】,專案創建是瞬間完成的
第三章 Maven基本使用
3.1 Maven準備
注意:IDEA2019.1.x 最高支持Maven的3.6.0
- 下載地址:http://maven.apache.org/
- Maven底層使用Java語言撰寫的,所有需要配置JAVA_HOME環境變數及Path
- 將Maven解壓非中文無空格目錄下
- 配置MAVEN_HOME環境變數及Path
- 輸入【cmd】,進入命令列視窗,輸入【mvn -v】 ,檢查Maven環境是否搭建成功
3.2 Maven基本配置
-
Maven組態檔位置:maven根目錄/conf/settings.xml
-
設定本地倉庫【默認:C:/用戶家目錄/.m2/repository】
<!-- 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>E:\SG_220106\LocalRepository</localRepository> -
設定阿里云鏡像服務器
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors> -
設定使用JDK版本【1.8|JDK8】
<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> </profiles>
3.3 Maven之Helloworld
約束>配置>代碼
-
Maven工程目錄結構約束
- 專案名
- src【書寫源代碼】
- main【書寫主程式代碼】
- java【書寫java源代碼】
- resources【書寫組態檔代碼】
- test【書寫測驗代碼】
- java【書寫測驗代碼】
- main【書寫主程式代碼】
- pom.xml【書寫Maven配置】
- src【書寫源代碼】
- 專案名
-
測驗步驟
- 進入專案名根目錄【在根目標輸入cmd即可】
- mvn clean
- mvn compile
- mvn test-compile
- mvn test
- mvn package
- mvn install
第四章 Maven及Idea的相關應用
4.1 將Maven整合到IDEA中


4.2 在IDEA中新建Maven工程


第五章 Maven核心概念
5.1 Maven的POM
-
POM全稱:Project Object Model【專案物件模型】,將專案封裝為物件模型,便于使用Maven管理【構建】專案
-
pom.xml常用標簽
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 設定父工程坐標--> <parent> <artifactId>maven_demo</artifactId> <groupId>com.atguigu</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>maven_helloworld</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
5.2 Maven約定的目錄結構
- 專案名
- src【書寫java源代碼】
- main【書寫java主程式代碼】
- java【書寫java代碼】
- resources【書寫組態檔代碼】
- test【書寫測驗代碼】
- java【書寫測驗java代碼】
- main【書寫java主程式代碼】
- pom.xml【書寫組態檔代碼】
- target【編譯后目錄結構】
- src【書寫java源代碼】
5.3 Maven生命周期
- Maven生命周期:按照順序執行各個命令,Maven生命周期包含以下三個部分組成
- Clean LifeCycle:在進行真正的構建之前進行一些清理作業,
- Default LifeCycle:構建的核心部分,編譯,測驗,打包,安裝,部署等等,
- Site LifeCycle:生成專案報告,站點,發布站點,

5.4 Maven插件和目標
- 插件:插件本質是由jar包和組態檔組成
- 目標:每個插件都能實作多個功能,每個功能就是一個插件目標,
5.5 Maven的倉庫【重要】
- 倉庫分類
- 本地倉庫:為當前計算機提供maven服務
- 遠程倉庫:為其他計算機也可以提供maven服務
- 私服:架設在當前局域網環境下,為當前局域網范圍內的所有Maven工程服務,
- 中央倉庫:架設在Internet上,為全世界所有Maven工程服務,
- 中央倉庫的鏡像:架設在各個大洲,為中央倉庫分擔流量,減輕中央倉庫的壓力,同時更快的回應用戶請求,
- 倉庫中的檔案型別【jar包】
- Maven的插件
- 第三方框架或工具的jar包
- 自己研發的專案或模塊
5.6 Maven的坐標【重要】
-
作用:使用坐標引入jar包
-
坐標由g-a-v組成
[1]groupId:公司或組織的域名倒序+當前專案名稱
[2]artifactId:當前專案的模塊名稱
[3]version:當前模塊的版本
-
注意
- g-a-v:本地倉庫jar包位置
- a-v:jar包全名
-
坐標應用
-
坐標參考網址:http://mvnrepository.com
-
語法,示例
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.17</version> </dependency> </dependencies>
-
第六章 Maven的依賴管理
6.1 依賴范圍
- 依賴語法:<scope>
- compile【默認值】:在main、test、Tomcat【服務器】下均有效,
- test:只能在test目錄下有效
- junit
- provided:在main、test下均有效,Tomcat【服務器】無效,
- servlet-api
6.2 依賴傳遞性
-
路徑最短者有先【就近原則】
-
先宣告者優先
-
注意:Maven可以自動解決jar包之間的依賴問題
第七章 Maven中統一管理版本號
-
語法
<properties> <spring-version>5.3.17</spring-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> </dependencies>
第七章 Maven的繼承
7.1 為什么需要繼承
- 如子工程大部分都共同使用jar包,可以提取父工程中,使用【繼承原理】在子工程中使用
- 父工程打包方式,必須是pom方式
7.2 Maven繼承方式一
-
在父工程中的pom.xml中匯入jar包,在子工程中統一使用,【所有子工程強制引入父工程jar包】
-
示例代碼
<packaging>pom</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
7.3 Maven繼承方式二
-
在父工程中匯入jar包【pom.xml】
<packaging>pom</packaging> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> -
在子工程引入父工程的相關jar包
<parent> <artifactId>maven_demo</artifactId> <groupId>com.atguigu</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> -
注意:在子工程中,不能指定版本號
第八章 Maven的聚合
-
為什么使用Maven的聚合
- 優勢:只要將子工程聚合到父工程中,就可以實作效果:安裝或清除父工程時,子工程會進行同步操作,
- 注意:Maven會按照依賴順序自動安裝子工程
-
語法
<modules> <module>maven_helloworld</module> <module>HelloFriend</module> <module>MakeFriend</module> </modules>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543980.html
標籤:其他
下一篇:RabbitMQ-訊息中間鍵
