Skip to content

Instantly share code, notes, and snippets.

@merikan
Last active July 6, 2020 02:32
Show Gist options
  • Save merikan/40f1bce7e962fec17bc98a5eb00bf5f3 to your computer and use it in GitHub Desktop.
Save merikan/40f1bce7e962fec17bc98a5eb00bf5f3 to your computer and use it in GitHub Desktop.
Migrate Maven artifacts from git repository to Nexus
#!/bin/bash
# Comments by Peter Merikan:
#
# This script is based on Steven Wheeler excellent script
# see https://www.steventwheeler.com/java/2018/10/30/migrate-artifactory-to-nexus.html
#
# Notes:
# 1. This script does not work on Macos, use Linux
# 2. Added option -k to curl command
#
# Usage:
# 1. $ mkdir migrate_to_nexus && cd migrate_to_nexus
# 2. $ git clone git@bitbucket.org:invanartjanster/form-mvnrepo.git
# 3. $ cd form-mvnrepo
# // first snapshots then releases
# 4. $ git checkout snapshots
# 5. Start a Linux container to run the script inside.
# $ docker run -i -t -v "$PWD":/tmp/migrate centos
# 6. # cd /tmp/migrate/
# // Important to verify that branch and path matches, snapshots or releases.
# 7. # ./import.sh --repo-url https://my-nexus-host/nexus/repository/maven-snapshots/ --username my-nexus-user --password *** --group se.inera.formengine
# Define the default option values.
username="anonymous"
password="anonymous"
base_dir="$(pwd)"
# If there was an error then exit, getopt will generate an error message.
if ! options=$(getopt -u -o r:u:p:g:b:h -l repo-url: -l username: -l password: -l group: -l base-dir: -l help -- "$@"); then
exit 1
fi
# Process the options.
usage="Usage: $0 -r|--repo-url url -u|--username username -p|--password password -g|--group group -b|--base-dir [-h|--help]"
set -- $options
while [ $# -gt 0 ]; do
case "$1" in
-r|--repo-url) repo_url="$2"; shift;;
-u|--username) username="$2"; shift;;
-p|--password) password="$2"; shift;;
-g|--group) group="$2"; shift;;
-b|--base-dir) base_dir="$2"; shift;;
-h|--help) echo "${usage}" 1>&2; exit 0;;
(--) shift; break;;
(-*) echo -e "Unknown option '$1'\n\n${usage}" 1>&2; exit 1;;
(*) break;;
esac
shift
done
# Ensure that the required options were provided.
[[ "x${repo_url}" != "x" ]] || { echo -e "Missing required option: -r|--repo-url\n\n${usage}" 1>&2; exit 1; }
[[ "x${group}" != "x" ]] || { echo -e "Missing required option: -g|--group\n\n${usage}" 1>&2; exit 1; }
# Convert the dot separated group identifier into a path fragment.
group_path="$(echo "${group}" | sed 's%\.%/%g')"
for file in $(find "${base_dir}" -type f -path "*${group_path}*"); do
base_file="$(echo "${file}" | sed -r "s%^.*(${group_path}.*)$%\1%g")"
echo "Uploading '${file}'..."
curl -k -u "${username}:${password}" -X PUT -v -T "${file}" "${repo_url}/${base_file}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment