注意:
右键 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>