背景
公司代碼是在內網開發,之前使用的maven離線倉庫分散在不同的開發機器上,版本難以維護。後採用sonatype OSS 做maven nexus,搭建內網私服。
採坑過程
經驗之談
由於在內網開發,不便於直接聯網下載,需要將原maven離線倉庫中jar/pom上傳至nexus。
- 上傳jar文件
mvn deploy:deploy-file -Dfile=<file> -DrepositoryId=<repositoryId> -Durl=<url> -DpomFile=<pomFile> -Dpackaging=jar
# 此處-Dfile=<file> 爲jar文件
此處需要額外注意 -DpomFile=<pomFile> ,如果不指定則需要手動指定groupId、artifactId、version,生成的pom文件中也只包含指定內容,極大可能造成dependencies、parent等關係丟失。
- 上傳pom文件
mvn deploy:deploy-file -Dfile=<file> -DrepositoryId=<repositoryId> -Durl=<url> -DpomFile=<pomFile> -Dpackaging=pom
# 此處-Dfile=<file> 爲pom文件
問題排查
Cannot deploy artifact from the local repository:/path/to/your/loca/repository/jar
如果mvn deploy的jar文件已經在本地maven庫中,可能會報告上述錯誤,將對應上傳的jar/pom文件複製到臨時目錄中,以臨時目錄中的文件執行即可。
腳本
附上shell腳本,方便批量處理
#!/bin/bash
pom_path=$1
# 設置mvn執行時佔用的內存大小,內存充足不設定也沒問題
export MAVEN_OPTS="-Xms32M -Xmx32M"
# 改爲自己的maven nexus地址
NEXUS_URL="xxxxxxx"
REPOSITORY_ID="your nexus repository id"
POMS="poms.txt"
find $pom_path -name "*.pom" | sort > $POMS || exit 1
for pom in `cat $POMS`
do
# 複製pom文件到當前目錄
\cp $pom .
pom_file_name=$(echo "$pom" | awk -F "/" '{print $NF}')
# 將pom文件後綴換成.jar找到對應的jar文件(此處根據約定pom/jar文件只是後綴不同來進行推測)
jar=${pom%".pom"}".jar"
# 如果jar文件存在,則將其複製到當前目錄
if [ -f $jar ]; then
\cp $jar .
jar_file_name=$(echo "$jar" | awk -F "/" '{print $NF}')
# 部署jar文件,附帶自身提供的pom文件
mvn deploy:deploy-file -Dfile=$jar_file_name -Durl=$NEXUS_URL -DrepositoryId=$REPOSITORY_ID -DpomFile=$pom_file_name -Dpackaging=jar
else
# 只部署pom文件
mvn deploy:deploy-file -Dfile=$pom_file_name -Durl=$NEXUS_URL -DrepositoryId=$REPOSITORY_ID -DpomFile=$pom_file_name -Dpackaging=pom
fi
# 清理複製的臨時jar和pom文件
\rm -rf ./$pom_file_name ./$jar_file_name
done
# 清理POM列表文件
\rm -rf $POMS
參考
感謝這篇帖子給的啓發,遇到問題先去官網查文檔。
https://blog.csdn.net/andyzhaojianhui/article/details/82785934