文章目錄
- 1. 搭建私服(windows)
- 2.上傳依賴
- 3.多個遠程倉庫配置
- 4.其它
1. 搭建私服(windows)
- 軟件下載
https://help.sonatype.com/en/download.html - 修改端口
etc/nexus-default.properties - 啟動程序
管理員身份進入進入bin目錄下執行.\nexus.exe /run - 創建Maven倉庫
- 選擇hosted類型
(如果需要創建npm的js庫也可以選擇npm類型的)
設置類型為Mixed和Allow Redeploy(可以重復deploy倉庫)
如果需要更細粒度的控制用戶權限可以在左側菜單設置
在這里可以指定用戶是否有上傳依賴的權限
2.上傳依賴
- 批量上傳依賴(Linux版本,利用Curl命令上傳Jar包)
#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line paramswhile getopts ":r:u:p:" opt; docase $opt inr) REPO_URL="$OPTARG";;u) USERNAME="$OPTARG";;p) PASSWORD="$OPTARG";;\?) echo "Invalid option -$OPTARG" >&2;;esac
doneecho $USERNAME
echo $PASSWORD
echo $REPO_URLfind . -type f -not -path './mavenImport.sh' -not -path '*/\.*' -not -path '*/\archetype-catalog.xml' -not -path '*/\maven-metadata-local.xml' -not -path '*/\maven-metadata-deployment.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
將腳本復制到依賴的根目錄下,執行命令
sh mavenImport.sh -uadmin -rpassword -p倉庫地址
- Maven Deploy
如果是開源依賴建議使用批量腳本上傳到倉庫中,如果自己開發的依賴需要上傳到倉庫中則建議通過deploy
在項目中配置倉庫地址
在pom中設置發布的倉庫
<distributionManagement><repository><id>test</id><url>http://localhost:7088/repository/test/</url></repository></distributionManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
在settings.xml中設置倉庫的用戶密碼(倉庫id要對應上)
<servers><server><id>test</id><username>admin</username><password>馬賽克</password></server></servers>
然后在IDEA里面deploy
可以看到已經成功上傳到nexus
3.多個遠程倉庫配置
目標就是解決如何將自己搭建的nexus私服和開源依賴配合使用,因為apache是沒有我們自己上傳的依賴,而我們自己的nexus私服中有沒有開源依賴。(當然也可以將緩存的開源依賴通過上面的批量腳本上傳到nexus私服中,內網開發如果無法聯網可以參考該做法)
在此之前需要了解倉庫和鏡像的區別
倉庫分為本地倉庫,遠程倉庫,中央倉庫。本地倉庫很好理解,遠程倉庫就是公司或者組織自己搭建的倉庫,比如本文上面用nexus搭建的倉庫,而中央倉庫就是apache的倉庫,默認配置好了,不用顯式配置。
如果我們的settings.xml什么都不配置,就會默認使用Maven自帶的倉庫,缺點是速度慢。
鏡像就是提供與原始倉庫相同的內容,通常位于地理位置更近或網絡條件更好的服務器上,用于加速依賴下載,比如阿里云鏡像倉庫,鏡像本質上也是倉庫,但是阿里云的鏡像只是apache中央倉庫一個復制品,他們不會將公司內部的依賴上傳上去。
所以鏡像使用和多倉庫還是有區別的,如果我們只為了加快開源依賴的緩存速度可以使用一個鏡像(遠程倉庫)去cover中央倉庫,比如阿里云鏡像的配置
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf>
<name>阿里云公共倉庫</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
但是maven中央倉庫和阿里云鏡像是不會有我們自己上傳的私有依賴的,還需要配置自己的倉庫
在pom中配置自己搭建的遠程倉庫(settings.xml中配置也可以)
<repositories><repository><id>test</id><url>http://localhost:7088/repository/test/</url></repository><repository>
</repositories>
這里同時在另一個項目中引入上面的私有依賴和開源依賴
<dependencies><!-- Spring Boot Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><artifactId>spring-boot-starter-logging</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions></dependency><dependency><groupId>com.cll.jtool.single</groupId><artifactId>jtool-single</artifactId><version>0.0.1</version></dependency></dependencies><!-- 定義版本屬性 --><repositories><repository><id>test</id><url>http://localhost:7088/repository/test/</url></repository></repositories>
完整的settings.xml配置
<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd"><servers><server><id>test</id><username>admin</username><password>馬賽克</password></server></servers><mirrors><mirror><id>alimaven</id><mirrorOf>central</mirrorOf><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url></mirror> </mirrors>
<repositories><repository><id>test</id><url>http://localhost:7088/repository/test/</url></repository></repositories>
</settings>
在這個項目的repo中可以看到所以私有依賴和緩存依賴都被加載下來了
通過_remote.repositories可以看到jar的來源
開源依賴的來源為aliyun倉庫
注意上面的阿里云鏡像倉庫僅僅覆蓋了maven中央倉庫,而不包括nexus私服test,如果設置如下,那么maven在加載私服的依賴時就不會去真正的私服加載而是去阿里云鏡像倉庫加載,最終也會緩存失敗。
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共倉庫</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
清空本地倉庫重新加載就會報錯
Could not find artifact com.cll.jtool.single:jtool-single:pom:0.0.1 in aliyunmaven (https://maven.aliyun.com/repository/public)
4.其它
- 開啟debug模式
即使pom正確配置,idea也可能因為本地緩存問題導致加載依賴緩慢,這時候可以直接執行
有時候因為pom的語法出現問題,可以借助debug模式快速定位
尤其是當pom文件過大借助debug模式可以快速定位問題