Skip to content

Instantly share code, notes, and snippets.

@SanSan-
Last active October 11, 2023 09:32
Show Gist options
  • Save SanSan-/3acc532687df60cfcc037cc79baedd92 to your computer and use it in GitHub Desktop.
Save SanSan-/3acc532687df60cfcc037cc79baedd92 to your computer and use it in GitHub Desktop.
How to mass upload maven artifacts to nexus
#!/bin/bash
files="./files.out"
releasefiles="./release.out"
snapshotfiles="./snapshot.out"
username="admin"
password="admin123"
nexusurl="http://nexus/content/repositories/thirdparty/"
snapshoturl="http://nexus/content/repositories/Snapshots/"
releaseurl="http://nexus/content/repositories/Releases/"
find . -name '*.*' -type f | cut -c 3- | grep "/" > $files
find . -name '*.*' -type f | cut -c 3- | grep "SNAPSHOT" | grep "/" > $snapshotfiles
find . -name '*.*' -type f | cut -c 3- | grep -v "SNAPSHOT" | grep "/" > $releasefiles
# while read i; do
# echo "upload $i to $nexusurl"
# curl -v -u $username:$password --upload-file $i "$nexusurl$i"
# done <$files
while read i; do
echo "upload $i to $snapshoturl"
curl -v -u $username:$password --upload-file $i "$snapshoturl$i"
done <$snapshotfiles
while read i; do
echo "upload $i to $releaseurl"
curl -v -u $username:$password --upload-file $i "$releaseurl$i"
done <$releasefiles
@yottta
Copy link

yottta commented Jun 23, 2020

Thanks for the initial draft!
For nexus 3.x I used this instead:

while read i; do
  group=$(echo $i | cut -d'/' -f2)
  artifact=$(echo $i | cut -d'/' -f3)
  version=$(echo $i | cut -d'/' -f4)
  echo "upload $i with group: $group; artifact: $artifact; version: $version"

# a 400 error means that the artifact already exists
  mvn deploy:deploy-file \
    -DgroupId=$group \
    -DartifactId=$artifactName \
    -Dversion=$version \
    -Dpackaging=jar \
    -Dfile=$i \
    -DgeneratePom=true \
    -DupdateReleaseInfo=true \
    -Durl="https://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_URL}/repository/maven-repository"
done < <(find . -name '*.jar' -type f)

@SanSan-
Copy link
Author

SanSan- commented Jun 23, 2020

@yottta I'm glad that it helped to you.

@jvillavi
Copy link

Quite useful, congrats!

@jaapspiering
Copy link

Still works like a charm on our Nexus 2 repository. Great time saver, thanks!

@m-serag-lab
Copy link

I did another one in python after the inspiration from you 😄
I hope it works for you
https://gist.github.com/m-serag-lab/adf41e89e967646b22047bfe2145ab14

@kschuemann
Copy link

kschuemann commented Apr 5, 2023

Just in case someone ist stumbeling over this thread. I found my setup more suitable:
this script needs a "artifacts" file right next to it. Create it by using following script in your .m2-folder

find -iname "*.pom" -printf "%h\n" > files; find -iname "*.jar" -printf "%h\n" >> files; cat files | sort | uniq -u > artifacts; rm files

Then use following script to upload the collected artifacts:

NEXUS_USERNAME="admin"
NEXUS_PASSWORD="nexus"
NEXUS_URL="localhost:8081"

cat artifacts | while read i; do

	pompath=$(find $i -name *.pom)
	jarpath=$(find $i -name *.jar)
	
	# extracting metainformations from pom
	groupId=$(echo $pompath | xargs xpath -e 'project/groupId/text()')
	artifactId=$(echo $pompath | xargs xpath -e 'project/artifactId/text()')
	version=$(echo $pompath | xargs xpath -e 'project/version/text()')
	if test -z "$groupId"
	then 
		echo "project-groupId is empty - using parent/groupId"
		groupId=$(echo $pompath | xargs xpath -e 'project/parent/groupId/text()')
	fi
	
	if test -z "$version"
	then 
		echo "project-version of jar-pom is empty - using parent/version"
		version=$(echo $pompath | xargs xpath -e 'project/parent/version/text()')
	fi


	# choosing upload-strategy, preferring jar-upload
	if test -z "$jarpath"
	then
		echo "uploading $artifactId as pom"
		# a 400 error means that the artifactId already exists
		mvn deploy:deploy-file \
		 -DgroupId=$groupId \
		 -DartifactId=$artifactId \
		 -Dversion=$version \
		 -Dpackaging=pom \
		 -Dfile=$pompath \
		 -Durl="http://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_URL}/repository/maven-releases/"
		echo "uploading $pompath with groupId: $groupId; artifactId: $artifactId; version: $version"
	else 
		echo "uploading $artifactId as jar"
		# a 400 error means that the artifactId already exists
		mvn deploy:deploy-file \
		 -DgroupId=$groupId \
		 -DartifactId=$artifactId \
		 -Dversion=$version \
		 -Dpackaging=jar \
		 -DgeneratePom=true \
		 -Dfile=$jarpath \
		 -Durl="http://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_URL}/repository/maven-releases"		
		echo "uploading $jarpath with groupId: $groupId; artifactId: $artifactId; version: $version"
	fi 
  
done 

echo 'done uploading artifacts'

@Mallsey
Copy link

Mallsey commented Oct 11, 2023

Thanks all and in particular kschuemann.
I found kschuemann's script really useful for this but I had to adapt it to upload a load of thrid party packages, please see nrxm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment