用於實現Maven構建私人倉庫的一個工具。html
可使用nexus 統一管理咱們到依賴,再也不須要再從中央倉庫下載對應的jar包java
私服是在局域網的一種特殊的遠程倉庫,目的是代理遠程倉庫及部署第三方構件。有了私服以後,當 Maven 須要下載jar包時,先請求私服,私服上若是存在則下載到本地倉庫。不然,私服直接請求外部的遠程倉庫,將jar包下載到私服,再提供給本地倉庫下載。apache
<!-- more -->緩存
簡介:依賴是maven最爲用戶熟知的特性之一,單個項目的依賴管理並不難,可是要管理幾個或者幾十個模塊的時,那這個依賴應該怎麼管理服務器
依賴的傳遞性oracle
依賴的做用範圍maven
compileide
provided工具
runtime測試
test
system
系統範圍與provided相似,不過你必須顯式指定一個本地系統路徑的JAR,此類依賴應該一直有效,Maven也不會去倉庫中尋找它。
<project> ... <dependencies> <dependency> <groupId>sun.jdk</groupId> <artifactId>tools</artifactId> <version>1.5.0</version> <scope>system</scope> <systemPath>${java.home}/../lib/tools.jar</systemPath> </dependency> </dependencies> ... </project>
import
依賴的兩大原則
路徑近者優先
A > B > C-1.0 A > C-2.0
第一聲明優先
A > B > D-1.0 A > C > D-2.0
依賴的管理
依賴排除
依賴可選
簡介:當出現jar包衝突時,咱們應該如何快速定位和處理jar包衝突問題
mvn dependency:tree -Dverbose
簡介:介紹nexus服務器預置的倉庫
類型介紹
預置倉庫
「GET PREPOSITORY OSS」
OSS2
cmd
, 進入目錄D:\java\Nexus\nexus-latest-bundle\nexus-2.14.15-01\bin
nexus.bat start
,能夠發現會拒絕訪問
使用管理員模式運行
nexus.bat install
安裝nexus.bat start
啓動服務Log In
登錄系統admin
以及 admin123
,輸入帳戶名和密碼以後登錄系統add
,選擇 Hosted Repository
改含義會在後面進行解釋save
按鈕便可建立一個倉庫這裏以一個本身的項目做爲測試
Maven依賴以下:
<dependency> <groupId>org.smart4j</groupId> <artifactId>smart-framwork</artifactId> <version>1.0.0</version> </dependency>
咱們須要先登陸Maven Nexus,具有管理員的權限,而後點擊3rd party
(三方依賴),截圖內容以下
依照截圖填入以下的依賴
GAV Definition: 定義GAVAuto Guess:自動猜想
Group:同Maven 到 group定義
Artifact:同Maven 到 Artifact定義
Version:版本號
Packaging:打包方式
下方須要上傳依賴對應到jar包
上傳以後須要添加到maven Nexus
等待上傳,上傳成功以後會給予對應到提示信息
上傳完成以後,咱們能夠點擊Browse Index
看到本身以前上傳的依賴
默認狀況下咱們到公共訪問地址以下:http://localhost:8081/nexus/content/groups/public 公共倉庫的訪問地址
把jar包添加到nexus以後,咱們能夠嘗試讓nexus來管理咱們到項目依賴了
接下來介紹如何使用maven nexus 來關聯咱們到依賴
注意:事先查看nexus公有倉庫是否存在依賴
conf
下面增長配置,以我的爲例進入D:\java\apach\apache-maven-3.6.0\conf
,修改setting.xml
,添加以下內容<!-- 添加 nexus 訪問權限 --> <servers> <server> <id>xdclass</id> <username>admin</username> <password>admin123</password> </server> </servers> <mirrors> <!-- 自定義 nexus --> <mirror> <id>xdclass</id> <mirrorOf>nexus,central</mirrorOf> <url>http://localhost:8081/nexus/content/groups/public/</url> <name>local nexus</name> </mirror> <!-- 阿里雲配置 --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>xd</id> <repositories> <repository> <id>local-nexus</id> <!-- 本地倉庫路徑 --> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <!-- 開啓快照 --> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <!-- 須要 開啓 Profile 配置 --> <activeProfiles> <activeProfile>xd</activeProfile> </activeProfiles>
本節介紹如何把本身的項目發佈到nexus ,這裏單獨開了一個倉庫進行配置
在進行本節內容以前,查看Maven 到 setting.xml
內容
<!-- 用於發佈正式版本 --> <server> <id>public</id> <username>admin</username> <password>admin123</password> </server> <!-- 測試 --> <server> <id>lazytime</id> <username>admin</username> <password>admin123</password> </server>
pom.xml
添加以下內容<build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> </plugins> </pluginManagement> </build> <distributionManagement> <repository> <!-- 此id 必須對應setting 裏面到 server id 不然會沒有權限沒法部署 --> <id>public</id> <url>http://localhost:8081/nexus/content/repositories/lazytime</url> </repository> </distributionManagement>
deloyer
項目到 maven Nexus
運行Maven Deloy
發佈項目到nexus
Maven Nexus
,查看發佈的項目內容若是不能發佈,請查看倉庫是否容許從新部署:
使用上一節辦法存在很大到缺點:
如何解決如上問題呢?
那麼咱們就須要使用快照
,快照至關於項目到一個副本,咱們能夠在開發到時候使用快照,發佈到時候在使用正式到版本號進行處理,使nexus的管理更加方便
Maven
的setting
文件當中添加以下內容,加入到對應的帳號<server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server>
snaphoto
規則,不然部署快照版本會失敗<modelVersion>4.0.0</modelVersion> <groupId>org.smart4j</groupId> <artifactId>smart-framwork</artifactId> <!-- 寫法必定要規範 --> <version>1.0-SNAPSHOT</version>
maven deloy
部署配置注意待發布項目的pom.xml
配置以下
<distributionManagement> <snapshotRepository> <id>snapshots</id> <url>http://localhost:8081/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement>
Maven Nexus
是否發佈成功再次強調,若是部署失敗,請查看倉庫是否容許重複部署
因爲使用了快照版本,須要更改依賴以下:
<dependency> <groupId>org.smart4j</groupId> <artifactId>smart-framwork</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
deploy
,部署快照版本的項目