로컬 Gradle 캐시 경로 구조(~/.gradle/caches/modules-2/files-2.1/{groupId}/{artifactId}/{version}/)에서 GroupId를 자동으로 추출해 정확한 GAV로 Nexus에 업로드하는 스크립트를 완성했습니다. 사용자님의 Gradle 환경에 최적화된 버전입니다.
완전 자동화 Bash 스크립트 (권장)
Gradle 캐시 전체를 순회하며 경로에서 GAV를 파싱하고 업로드합니다.
bash
#!/bin/bash
# auto-upload-gradle-cache.sh <repoId> <nexus-url>
REPO_ID=$1
NEXUS_URL=$2
CACHE_DIR=”${HOME}/.gradle/caches/modules-2/files-2.1/”
if [[ ! -d “$CACHE_DIR” ]]; then
echo “Gradle 캐시 디렉토리 없음: $CACHE_DIR”
exit 1
fi
find “$CACHE_DIR” -type d -name ‘[0-9]*’ | while read version_dir; do
# 경로 파싱: …/files-2.1/group/artifact/version/
group_path=$(realpath –relative-to=”$CACHE_DIR” “$(dirname “$(dirname “$version_dir”)”)”)
artifact=$(basename “$(dirname “$version_dir”)”)
version=$(basename “$version_dir”)
# groupId 경로를 .으로 치환 (com/example/foo -> com.example.foo)
group_id=$(echo “$group_path” | tr ‘/’ ‘.’)
jar_file=”$version_dir/${artifact}-${version}.jar”
if [[ -f “$jar_file” ]]; then
echo “업로드: $group_id:$artifact:$version”
mvn deploy:deploy-file \
-DgroupId=”$group_id” \
-DartifactId=”$artifact” \
-Dversion=”$version” \
-DgeneratePom=true \
-Dpackaging=jar \
-DrepositoryId=”$REPO_ID” \
-Durl=”$NEXUS_URL” \
-Dfile=”$jar_file”
fi
done
사용법: ./auto-upload-gradle-cache.sh libs-releases http://nexus:8081/repository/libs-releases/

Gradle 태스크 완성본
build.gradle에 추가하면 gradle uploadAllCache로 전체 캐시 업로드.
text
task uploadAllCache {
doLast {
def cacheDir = “${System.getProperty(‘user.home’)}/.gradle/caches/modules-2/files-2.1/”
def nexusUrl = ‘http://your-nexus:8081/repository/maven-releases/’
def repoId = ‘nexus-releases’
file(cacheDir).traverse(type: groovy.io.FileType.FILES) { file ->
if (file.name.endsWith(‘.jar’) && file.name =~ /(.+)-(.+)\.jar/) {
def jarName = file.name
def versionDir = file.parent
def artifactDir = versionDir.parentFile
def groupPath = artifactDir.parentFile.parentFile.absolutePath.replace(cacheDir, ”).split(‘/’)*.replace(‘/’, ‘.’).join(‘.’)
def matcher = (jarName =~ /(.+)-(.+)\.jar/)
def artifactId = matcher[0][1]
def version = matcher[0][2]
println “업로드: ${groupPath}:${artifactId}:${version}”
exec {
workingDir file.parentFile
commandLine ‘mvn’, ‘deploy:deploy-file’,
“-DgroupId=${groupPath}”,
“-DartifactId=${artifactId}”,
“-Dversion=${version}”,
‘-DgeneratePom=true’,
‘-Dpackaging=jar’,
“-DrepositoryId=${repoId}”,
“-Durl=${nexusUrl}”,
“-Dfile=${file.absolutePath}”
}
}
}
}
}
특정 프로젝트 캐시만 업로드
현재 프로젝트의 build/dependency-cache/ 또는 gradle/dependency-cache/ 대상:
bash
# 프로젝트 루트에서 실행
find .gradle/dependency-cache -type f -name “*.jar” | while read jar; do
group_id=$(echo $jar | sed ‘s|^\./||; s|/|.|g; s|\([^/]*\)-\([^/]*\)\.jar$|\1|’)
# … (위 로직 동일)
done
설정 및 실행 체크리스트
settings.xml: <server><id>libs-releases</id><username>deployer</username><password>***</password></server>
Nexus 권한: nx-deploy 롤 확인
테스트: find ~/.gradle/caches/modules-2/files-2.1/ -name “*.jar” | head -5으로 샘플 확인
로그: -X 플래그로 디버깅 (mvn … -X)
이제 GroupId, ArtifactId, Version 모두 파일 경로에서 자동 추출되어 Nexus에 정확히 업로드됩니다.