maven批量上传

一、批量上传

https://blog.csdn.net/xinle0320/article/details/124263558
https://blog.csdn.net/liusong0605/article/details/110004803
https://help.sonatype.com/en/components-api.html


创建repository:maven2 hosted类型,layout:permissive/Deployment policy:Allow redeploy;Strict Content Type validation取消

错误1)curl 上传 提示405错误,改用put方法,注意deploy只能选择hosted类型的
错误2)cannot upload SNAPSHOT content to RELEASE repositories,这个错误表明您尝试将 SNAPSHOT 版本的构件上传到 Nexus 的 Release 仓库,但这是不被允许的。Nexus 严格区分 SNAPSHOT(快照)和 RELEASE(发布)仓库。

如下脚本:只上传jar包,自动生成pom: upload_release.sh
#!/bin/bash
REPOKDIR="/home/repository/" #repo目录,注意尾部带上"/"
SUBDIR="all" #只仓库目录下io子目录下的所有jar包,如果设置为all或者ALL,则全部上传
USERPWD="admin:PWD"
REPONAME="abc-releases"
REQURL="http://12.0.0.1:1102/service/rest/v1/components"

upload() {
	GROUP_ID=$1
	ARTIFACT_ID=$2
	VERSION=$3
	JARPATH=$4
	curl -s -u $USERPWD -F "maven2.generate-pom=true" -F "maven2.groupId=$GROUP_ID" -F "maven2.artifactId=$ARTIFACT_ID" -F "maven2.packaging=jar" -F "version=$VERSION" -F "maven2.asset1=@$JARPATH" -F "maven2.asset1.extension=jar" "$REQURL?repository=$REPONAME"
}

getsubfile() {
	SUBDIR=$1
	GLOBAL=$2
	echo "[GLOBAL] function getsubfile ==>"
	sum=`find $REPOKDIR/$SUBDIR -type f -name *.jar |wc -l`
	index=0

	for f in `find $REPOKDIR/$SUBDIR -type f -name *.jar`;do 
		let index++
		tmpname=`echo $f |sed "s@$REPOKDIR@@g" |sed 's@^/@@g'`
		gname1=`echo $f | sed "s@$REPOKDIR@@g" | awk -F'/'  '{for (i=1; i<NF; i++) {if (i<=NF-3) printf "%s/",$i}}'`
		gname2=`echo $f |sed "s@$REPOKDIR@@g" |sed "s@$gname1@@g"`
		GROUP_ID=`echo $gname1 |sed 's@/@.@g' |sed 's/\.$//g;s/^\.//g'`
		ARTIFACT_ID=`echo $gname2 | sed 's/\.$//g;s/^\.//g' |awk -F'/' '{print $1}'`
		VERSION=`echo $gname2 | sed 's/\.$//g;s/^\.//g' |awk -F'/' '{print $2}'`
		JARPATH=$f
		echo -ne "[GLOBAL:$GLOBAL][SUB:$index/$sum]\t$GROUP_ID\t$ARTIFACT_ID\t$VERSION\t$JARPATH \n"	
		upload $GROUP_ID $ARTIFACT_ID $VERSION $JARPATH
	done
}

getallfile() {
	sum2=`find $REPOKDIR -type d -maxdepth 1 2> /dev/null  |sed "s@$REPOKDIR@@g" |sed 's@^/@@g'  |grep -v '^$' |wc -l `
	id=0
	for m in `find $REPOKDIR -type d -maxdepth 1 2> /dev/null  |sed "s@$REPOKDIR@@g" |sed 's@^/@@g'  |grep -v '^$'` ;do 
		let id++
		echo "=====================================>[GLOBAL:$id/$sum2]<=====================================>]"
		getsubfile $m $id/$sum2
	done 
}

if [ $SUBDIR == "all" ] || [ $SUBDIR == "ALL" ] ;then 
	getallfile
else
	getsubfile $SUBDIR
fi


上述脚本对SNAPSHOT的会提示错误因为这里只针对release可以,SNAPSHOT单独创建个仓库处理
Version policy mismatch, cannot upload SNAPSHOT content to RELEASE repositories for file 
需要在项目目录内:
mvn deploy:deploy-file -DgroupId=XXX -DartifactId=XXX -Dversion=XXX -Dpackaging=jar -Dfile=./abc/targets/XXX-1.0.0-SNAPSHOT.jar -Durl=$REQURL  -DrepositoryId=abc-snapshots 

在生成snapshot jar对应的项目目录内,执行,并且前提是:默认的settings.xml是可以正常打包的,-Dfil要用当前项目目录下的jar包


二、配置文件参考

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  
  <!-- 本地仓库目录 -->
  <localRepository>/home/repository/</localRepository>
  
  <!-- 服务器认证配置 -->
  <servers>
    <server>
      <id>aaa-releases</id>
      <username>admin</username>
      <password>pwd</password>
    </server>
    <server>
      <id>aaa-snapshots</id>
      <username>admin</username>
      <password>pwd</password>
    </server>
  </servers>

  <!-- 镜像配置(可选) -->
  <mirrors>
    <mirror>
      <id>aaa-central-mirror</id>
      <name>aaa Central Mirror</name>
      <url>http://127.0.0.1.249:1102/repository/aaa-releases/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>

  <!-- 仓库配置 -->
  <profiles>
    <profile>
      <id>aaa-repositories</id>
      <repositories>
        <repository>
          <id>aaa-releases</id>
          <name>aaa Releases</name>
          <url>http://127.0.0.1.249:1102/repository/aaa-releases/</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
        <repository>
          <id>aaa-snapshots</id>
          <name>aaa Snapshots</name>
          <url>http://127.0.0.1.249:1102/repository/aaa-snapshots/</url>
          <releases>
            <enabled>false</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      
      <pluginRepositories>
        <pluginRepository>
          <id>aaa-releases</id>
          <name>aaa Plugin Releases</name>
          <url>http://127.0.0.1.249:1102/repository/aaa-releases/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </pluginRepository>
        <pluginRepository>
          <id>aaa-snapshots</id>
          <name>aaa Plugin Snapshots</name>
          <url>http://127.0.0.1.249:1102/repository/aaa-snapshots/</url>
          <releases>
            <enabled>false</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <!-- 激活的profile -->
  <activeProfiles>
    <activeProfile>aaa-repositories</activeProfile>
  </activeProfiles>

</settings>


snapshot上传:
上述脚本对SNAPSHOT的会提示错误因为这里只针对release可以,SNAPSHOT单独创建个仓库处理
Version policy mismatch, cannot upload SNAPSHOT content to RELEASE repositories for file 
需要在项目目录内:
mvn deploy:deploy-file -DgroupId=XXX -DartifactId=XXX -Dversion=XXX -Dpackaging=jar -Dfile=./abc/targets/XXX-1.0.0-SNAPSHOT.jar -Durl=$REQURL  -DrepositoryId=abc-snapshots 

在生成snapshot jar对应的项目目录内,执行,并且前提是:默认的settings.xml是可以正常打包的,-Dfile要用当前项目目录下的jar包


结论:
1)hosted类型的仓库,使用version policy为snapshot或者mixed,(mvn deloy后面带不带参数效果都一样)上传后都会带有日期的子目录
mvn deploy:deploy-file --settings /usr/local/apache-maven-3.8.6/conf/settings.xml -DgroupId=com.iwhale.abc -DartifactId=dp-uc-server -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile=./dp-uc-server/target/dp-uc-server-1.0.0-SNAPSHOT.jar  -Durl=http://127.0.0.1:1102/repository/abc-snapshots -DrepositoryId=abc-snapshots
2)使用仓库version policy为mixed可以手动上传jar包,手动上传的是 不带时间的版本,但是默认下载的到底是哪个版本?待测试
3)mvn deploy -DuniqueVersion=false无效,因为maven3主要还是让你使用使用时间戳

snatpshot上传方法2:
主pom.xml要配置好
------------------------------------------------------------------------------------
    <!-- 远程仓库地址 -->
    <pluginRepositories>
        <pluginRepository>
            <id>abc-releases</id>
            <name>abc-releases</name>
            <url>http://127.0.0.1:1102/repository/abc-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <!-- 配置远程发布到私服,mvn deploy -->
<distributionManagement>
    <repository>
        <id>abc-releases</id>
        <name>abc Releases</name>
        <url>http://127.0.0.1:1102/repository/abc-releases/</url>
    </repository>
    <snapshotRepository>
        <id>abc-snapshots</id>
        <name>abc Snapshots</name>
        <url>http://127.0.0.1:1102/repository/abc-snapshots/</url>
    </snapshotRepository>
</distributionManagement>
------------------------------------------------------------------------------------
子pom.xml这里要打开
    <properties>
        <maven.deploy.skip>false</maven.deploy.skip>
    </properties>
------------------------------------------------------------------------------------
posted @ 2025-04-22 10:07  MT_IT  阅读(68)  评论(0)    收藏  举报