nexus是廣為人知的搭建maven私有倉(cāng)庫(kù)的工具睦番。
本文記錄nexus在安裝配置過(guò)程中的一些筆記。
使用docker安裝nexus
docker-compose.yml配置:
version: '2'
services:
nexus:
image: sonatype/nexus3:3.2.0
ports:
- 8081:8081
volumes:
- /mnt/nexus-data:/nexus-data:Z
第一次啟動(dòng)時(shí)會(huì)報(bào)錯(cuò)耍属,提示掛載的目錄寫權(quán)限不足托嚣。需要修改一下掛載目錄的所有者:
mkdir nexus-data && chown -R 200 /mnt/nexus-data
參考文檔:
https://github.com/sonatype/docker-nexus3
在maven配置文件內(nèi),設(shè)置私有倉(cāng)庫(kù)的賬號(hào)密碼厚骗。
有兩個(gè)地方可以修改maven配置注益。
一個(gè)是global配置。
mac下:如果是使用brew安裝:/usr/local/Cellar/maven/3.3.9/libexec/conf/settings.xml
centOS下: /usr/local/apache-maven-3.3.9/conf/settings.xml
Intellij IDE:/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/conf/settings.xml
一個(gè)是當(dāng)前用戶的配置:~/.m2/settings.xml
該文件默認(rèn)不存在溯捆,手動(dòng)創(chuàng)建后, 會(huì)覆蓋global的配置。
創(chuàng)建settings.xml文件提揍,并將代碼拷貝進(jìn)去:
<settings>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
</settings>
將<mirror><url>
標(biāo)簽內(nèi)的地址修改成nexus服務(wù)的地址啤月。
<servers>
標(biāo)簽內(nèi)填寫nexus服務(wù)的賬號(hào)密碼,發(fā)布maven項(xiàng)目到nexus時(shí)劳跃,需要用到谎仲。
<server><id>
下id需要跟<mirror><id>
一致。
拉取maven項(xiàng)目
當(dāng)拉取maven項(xiàng)目時(shí)刨仑,流程是:
- nexus檢查本地是否存在該項(xiàng)目郑诺。
- 如果存在, 直接將該項(xiàng)目返回給客戶端。
- 如果不存在杉武,從maven官方倉(cāng)庫(kù)中拉取項(xiàng)目辙诞,并保存到本地。之后返回給客戶端轻抱。
在nexus的web頁(yè)面上飞涂,可以搜索到之前拉取的項(xiàng)目。
發(fā)布項(xiàng)目到nexus倉(cāng)庫(kù)
首先在項(xiàng)目的pom.xml文件內(nèi)祈搜,指定發(fā)布地址:
<distributionManagement>
<repository>
<id>nexus</id>
<name>Releases</name>
<url>http://{your-nexus-ip}/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Snapshot</name>
<url>http://{your-nexus-ip}/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>
此處注意:release版本要配置到<repository>
標(biāo)簽內(nèi)较店。snapshot版本配置到<snapshotRepository>
標(biāo)簽內(nèi)。
使用命令發(fā)布項(xiàng)目:mvn clean deploy
log顯示上傳發(fā)布的地址:
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ base ---
[INFO] Installing /Users/Franklin/Documents/work/hldh/cloud/cloud-base-repo/pom.xml to /Users/Franklin/.m2/repository/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ base ---
Downloading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml
Downloaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml (591 B at 2.7 KB/sec)
Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-20170107.083838-2.pom
Uploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-20170107.083838-2.pom (6 KB at 24.7 KB/sec)
Downloading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml
Downloaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml (276 B at 2.8 KB/sec)
Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml
Uploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml (591 B at 3.0 KB/sec)
Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml
Uploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml (276 B at 1.1 KB/sec)
說(shuō)明項(xiàng)目已經(jīng)發(fā)布到nexus上了容燕。
這時(shí)候在nexus的web頁(yè)面上, 就能search到剛剛發(fā)布的項(xiàng)目了:
上傳第三方j(luò)ar包到nexus
發(fā)布不帶pom文件的獨(dú)立jar包:
mvn deploy:deploy-file -DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<type-of-packaging> \
-Dfile=<path-to-file> \
-DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
-Durl=<url-of-the-repository-to-deploy>
-DrepositoryId
的值即為在setttings.xml里面配置的server id梁呈。
默認(rèn)情況下,maven會(huì)自動(dòng)為jar包創(chuàng)建pom文件蘸秘,如果只想保留獨(dú)立jar包官卡,可以使用參數(shù)關(guān)閉這個(gè)特性:
-DgeneratePom=false
發(fā)布帶有pom的jar包
mvn deploy:deploy-file -DpomFile=<path-to-pom> \
-Dfile=<path-to-file> \
-DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
-Durl=<url-of-the-repository-to-deploy>
參考:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
使用nexus常見(jiàn)錯(cuò)誤分析
Request Entity Too Large
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.984 s
[INFO] Finished at: 2017-01-08T15:20:02+08:00
[INFO] Final Memory: 12M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact emay:emayclient:jar:4.3.4 from/to nexus (http://nexus.store.com/repository/maven-releases/): Failed to transfer file: http://nexus.store.com/repository/maven-releases/emay/emayclient/4.3.4/emayclient-4.3.4.jar. Return code is: 413, ReasonPhrase: Request Entity Too Large. -> [Help 1]
原因:nexus服務(wù)使用了nginx做反向代理,jar包的大小超過(guò)了nginx所允許的范圍秘血。
解決方法:修改nginx.conf配置味抖,將client_max_body_size
設(shè)置為一個(gè)較大的值:
server {
client_max_body_size 10M;
listen 80; server_name localhost;
location / { proxy_pass http://127.0.0.1:8000/; }
}
- maven編譯時(shí),報(bào)錯(cuò):
Failure to find org.jfrog.maven.annomojo:maven-plugin-anno:jar:1.4.0 in http://myrepo:80/artifactory/repo
was cached in the local repository, resolution will not be reattempted until the update interval of MyRepo has elapsed or updates are forced -> [Help 1]
原因:服務(wù)器之前是使用的官方maven庫(kù)拉取依賴灰粮,本地已經(jīng)存在jar包仔涩,配置了nexus倉(cāng)庫(kù)之后,跟之前本地的jar包產(chǎn)生了沖突粘舟。
解決方法:刪除~/.m2/repository目錄下對(duì)應(yīng)的jar包熔脂。 或者干脆從新download一遍所有jar包。
mvn clean install -U
-U表示強(qiáng)制更新所有依賴
- 拉取到本地的第三方庫(kù)柑肴,只有l(wèi)astUpdated文件霞揉,卻不見(jiàn)pom和jar文件:
本地報(bào)錯(cuò):
[WARNING] The POM for xxx.jar is missing, no dependency information available
我一直以為是nexus有bug,代理maven中央倉(cāng)庫(kù)時(shí)出錯(cuò)晰骑。
最后找了好久适秩,發(fā)現(xiàn)原來(lái)是這個(gè)庫(kù)的groupId改了,而且它還刪除了maven中央倉(cāng)庫(kù)的groupId對(duì)應(yīng)的包,maven找不到對(duì)應(yīng)的pom和jar包秽荞,就只會(huì)創(chuàng)建lastUpdated文件骤公。
這次更堅(jiān)信了nexus的代理機(jī)制是很健壯的,出問(wèn)題一般都是自己本地的問(wèn)題扬跋。
nexus官方文檔:
http://books.sonatype.com/nexus-book/reference3/maven.html#maven-sect-single-group