本地maven仓库上传到私有仓库(批量)

注意:
	右键 GIT BASH HERE ,执行 sh 脚本
   1.你的包 如果是  *-snapshots, REMOTE_URL 需要变更为 snapshots 仓库地址
   2.win新建文件修改后缀为.sh,在执行时可能会失败,由于与linux编码不一致问题,需要
   
在Windows 10下使用Git Bash执行脚本时遇到`#!/bin/bash no such file or directory`错误。这是一个典型的Windows和Unix/Linux换行符不兼容问题。

1. **使用dos2unix工具转换**(推荐)
# 在Git Bash中安装dos2unix
pacman -S dos2unix  # 如果是完整版Git Bash
# 或者
curl -o dos2unix.exe https://github.com/git-for-windows/git/raw/master/mingw64/bin/dos2unix.exe
# 转换文件
dos2unix 你的脚本文件名.sh

2. **使用sed命令转换**
```bash
sed -i 's/\r$//' 你的脚本文件名.sh

新建 sh 后缀文件 并执行 chmod +x youshname.sh 付权限

#!/bin/bash

# 定义变量 - 使用Windows路径格式
LOCAL_REPO="D:/zzwork/maven/flowableRepository"  # 注意路径分隔符改为正斜杠
REMOTE_URL="http://私有仓库地址/repository/maven-releases/"
REPO_ID="私有仓库ID"

echo "开始批量上传,本地仓库: $LOCAL_REPO"

# 创建临时文件存储jar列表
temp_file=$(mktemp)
find "$LOCAL_REPO" -name "*.jar" | grep -v "sources.jar" | grep -v "javadoc.jar" > "$temp_file"

# 显示找到的jar数量
jar_count=$(wc -l < "$temp_file")
echo "找到 $jar_count 个JAR文件"

# 读取文件而不是使用管道,避免Windows下的子shell问题
while read -r jarFile; do
  # 构建POM文件路径 
  pomFile="${jarFile%.jar}.pom"
  
  # 如果POM存在则部署
  if [ -f "$pomFile" ]; then
    echo "准备上传: $jarFile"
    
    # 从POM文件中提取groupId (更可靠的方法)
    groupId=$(grep -m1 "<groupId>" "$pomFile" | sed -e 's/.*<groupId>\(.*\)<\/groupId>.*/\1/' -e 's/[[:space:]]//g')
    # 如果POM中没有直接的groupId,则尝试从parent中提取
    if [ -z "$groupId" ]; then
      groupId=$(grep -A1 "<parent>" "$pomFile" | grep "<groupId>" | sed -e 's/.*<groupId>\(.*\)<\/groupId>.*/\1/' -e 's/[[:space:]]//g')
    fi
    
    # 正确提取artifactId(版本号目录的父目录名)
	artifactId=$(basename $(dirname $(dirname "$jarFile")))
    version=$(basename "$jarFile" | sed 's/.*-\([0-9][0-9a-zA-Z.-]*\)\.jar$/\1/')
    
    echo "GroupID: $groupId"
    echo "ArtifactID: $artifactId"
    echo "Version: $version"
    
    # 执行上传命令
    mvn deploy:deploy-file \
      -s "你的文件地址 D:/path/to/your/settings.xml"
      -DgroupId="$groupId" \
      -DartifactId="$artifactId" \
      -Dversion="$version" \
      -Dpackaging=jar \
      -Dfile="$jarFile" \
      -DpomFile="$pomFile" \
      -Durl="$REMOTE_URL" \
      -DrepositoryId="$REPO_ID"
    
    # 添加延迟,避免服务器负载过高
    sleep 1
  fi
done < "$temp_file"

# 删除临时文件
rm "$temp_file"

echo "批量上传完成"
REPO_ID 对应这里的id,找到对应账号密码
<servers>
    <server>
        <id>sna	pshots</id>
        <username>admin</username>
        <password>******</password>
    </server>
    <server>
        <id>release</id>
        <username>admin</username>
        <password>******</password>
    </server>
</servers>
 这里面的 url 就是 REMOTE_URL 对应仓库地址
 <repositories>
      <repository>
          <id>releases</id>
          <url>http://私有仓库地址/repository/maven-releases/</url>
          <snapshots>
              <enabled>true</enabled>
          </snapshots>
          <releases>
              <enabled>true</enabled>
          </releases>
      </repository>

  </repositories>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值