1、安裝maven以及nexus
maven的作用:依賴管理+項目一鍵建構
1.1、安裝maven
直接去官網下載下傳,或:
百度雲:連結:https://pan.baidu.com/s/12zO0giSNy5zyFXHsHkRPTw
提取碼:nuxr
阿裡雲(不限速):https://www.aliyundrive.com/s/jw7taoSL7Q1
注意,網盤下載下傳的版本是3.6.0的,裡面有一些配置已經配置好,也可以根據下面的步驟自己配置。
下載下傳完成後,直接解壓就可以使用了。
如果想在DOS指令視窗使用,則配置一下環境變量:

最後在dos指令視窗輸入,出現下圖則表示安裝配置成功。
mvn -version
1.2、安裝nexus私服
去官網下載下傳,或者網盤下載下傳。
提取碼:9a8u
- 解壓到自己的安裝目錄;
安裝配置maven以及私服nexus - 進入bin目錄的dos視窗
安裝配置maven以及私服nexus - 執行 nexus.bat install(如果拒絕通路就以管理者身份運作cmd)
安裝配置maven以及私服nexus - 安裝成功,啟動nexus:在剛才的指令視窗輸入 nexus.bat start或者右鍵這裡啟動。
安裝配置maven以及私服nexus - 通路:http://localhost:8081/nexus/
安裝配置maven以及私服nexus - 點選右上角login,預設賬号密碼:admin/admin123
2、maven及nexus的配置
2.1、配置maven
- 找到下面的配置檔案
安裝配置maven以及私服nexus
2.1.1、配置本地倉庫的位置(之後所有依賴下載下傳的位置)
不修改的話預設是在:C:User(使用者)/${user.home}/.m2/repository
2.1.2、配置阿裡鏡像
maven預設的下載下傳位址是國外的倉庫,可以換成國内的阿裡鏡像提高下載下傳速度。下圖這兩個都可以用。
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿裡雲公共倉庫</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
2.1.3、設定項目的預設jdk版本
<!-- 配置maven編譯jdk版本 -->
<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>
2.1.4、idea配置maven
2.2、nexus私服相關配置
3、maven的介紹
3.1、maven的生命周期
第一套:
Clean Lifecycle 在進行真正的建構之前進行一些清理工作。
第二套:
Default Lifecycle 建構的核心部分,編譯,測試,打包,部署等等。
第三套:
Site Lifecycle 生成項目報告,站點,釋出站點。
3.2、maven依賴的作用範圍
A 依賴 B,需要在 A 的 pom.xml 檔案中添加 B 的坐标,添加坐标時需要指定依賴範圍,依賴範圍包括:
- compile:編譯範圍,指 A 在編譯時依賴 B,此範圍為預設依賴範圍。編譯範圍的依賴會用在編譯、測試、運作,由于運作時需要是以編譯範圍的依賴會被打包。
- provided:provided 依賴隻有在當 JDK 或者一個容器已提供該依賴之後才使用, provided 依 賴在編譯和測試時需要,在運作時不需要,比如:servlet api 被 tomcat 容器提供。
- runtime:runtime 依賴在運作和測試系統的時候需要,但在編譯的時候不需要。比如:jdbc的驅動包。由于運作時需要是以 runtime 範圍的依賴會被打包。
-
test:test 範圍依賴 在編譯和運作時都不需要,它們隻有在測試編譯和測試運作階段可用,
比如:junit。由于運作時不需要是以 test範圍依賴不會被打包。
-
system:system 範圍依賴與 provided 類似,但是你必須顯式的提供一個對于本地系統中 JAR
檔案的路徑,需要指定 systemPath 磁盤路徑,system依賴不推薦使用。
總結:
- 預設引入 的 jar 包 ------- compile 【預設範圍 可以不寫】(編譯、測試、運作 都有效 )
- servlet-api 、jsp-api ------- provided (編譯、測試 有效, 運作時無效 防止和 tomcat 下 jar 沖突)
- jdbc 驅動 jar 包 ---- runtime (測試、運作 有效 )
- junit ----- test (測試有效)
- 依賴範圍由強到弱的順序是:compile>provided>runtime>test
3.3、pom檔案的主要标簽
pom.xml 是 Maven 項目的核心配置檔案,位于每個工程的根目錄,基本配置如下:
<project > :檔案的根節點 .
<modelversion > : pom.xml 使用的對象模型版本
<groupId > :項目名稱,一般寫項目的域名
<artifactId > :子產品名稱,子項目名或子產品名稱
<version > :産品的版本号 .
<packaging > :打包類型,一般有 jar、war、pom 等
<name > :項目的顯示名,常用于 Maven 生成的文檔。
<description > :項目描述,常用于 Maven 生成的文檔
<dependencies> :項目依賴構件配置,配置項目依賴構件的坐标
<build> :項目建構配置,配置編譯、運作插件等。
4、nexus的介紹
4.1、倉庫類型
倉庫的位置預設是在安裝目錄下的 sonatype-work 目錄中
1. hosted,宿主倉庫,部署自己的 jar 到這個類型的倉庫,包括 releases 和 snapshot 兩部
分,Releases 公司内部釋出版本倉庫、 Snapshots 公司内部測試版本倉庫
2. proxy,代理倉庫,用于代理遠端的公共倉庫,如 maven 中央倉庫,使用者連接配接私服,私
服自動去中央倉庫下載下傳 jar 包或者插件。
3. group,倉庫組,用來合并多個 hosted/proxy 倉庫,通常我們配置自己的 maven 連接配接倉
庫組。
4. virtual(虛拟):相容 Maven1 版本的 jar 或者插件
4.2、将項目釋出到nexus私服
- 1、先配置maven的settings.xml,可以配置多個,但是id不能重複
安裝配置maven以及私服nexus
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
- 2、在自己的maven項目pom檔案中配置如下資訊,這裡的id要和剛才maven的配置檔案中的id對相應上。
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
4.3、從nexus私服下載下傳jar包
- 1、配置settings.xml檔案
安裝配置maven以及私服nexus
<profile>
<!--profile 的 id-->
<id>dev</id>
<repositories>
<repository>
<!--倉庫 id,repositories 可以配置多個倉庫,保證 id 不重複-->
<id>nexus</id>
<!--倉庫位址,即 nexus 倉庫組的位址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下載下傳 releases 構件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下載下傳 snapshots 構件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件倉庫,maven 的運作依賴插件,也需要從私服下載下傳插件 -->
<pluginRepository>
<!-- 插件倉庫的 id 不允許重複,如果重複後邊配置會覆寫前邊 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
- 2、配置項目的pom檔案
<repositories>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
5、将第三方jar包安裝到maven和nexus倉庫
5.1、安裝到maven倉庫
參數說明:
mvn install:install-file
# windows下jar包所在的目錄
-Dfile=C:\Users\華碩\Desktop\ojdbc7.jar
# 分組id
-DgroupId=com.oracle
# artifact的名稱
-DartifactId=ojdbc14
# jar 包版本,根據自己的情況定義
-Dversion=10.2.0.4.0
# 包類型如jar/zip
-Dpackaging=jar
# 是否建立pom檔案
-DgeneratePom=true
注意:每個 -D 前面有個空格
5.2、安裝到私服
- 先在maven的settingsxml中配置私服第三方倉庫的相關資訊。
安裝配置maven以及私服nexus
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
- 執行安裝指令
mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=ojdbc7.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
參數說明
mvn deploy:deploy-file
# windows下jar包所在的目錄
-Dfile=C:\Users\華碩\Desktop\ojdbc7.jar
# 分組id
-DgroupId=com.oracle
# artifact的名稱
-DartifactId=ojdbc14
# jar 包版本,根據自己的情況定義
-Dversion=10.2.0.4.0
# 包類型如jar/zip
-Dpackaging=jar
# 倉庫位址
Durl=http://localhost:8081/nexus/content/repositories/thirdparty
# 倉庫id
-DrepositoryId=thirdparty
注意:每個 -D 前面有個空格