marcelo
marcelo

Reputation: 138

Import zip file as maven repository to Nexus OSS 3

Is it possible to import a zip file as a Maven repository in Nexus OSS 3? There is a Documentation section about migrating (https://books.sonatype.com/nexus-book/reference/migrating.html) but it does not seem to apply to Nexus 3 since the blob storage is, well, a blob... On Artifactory it is as easy as uploading the zip file to an existing repo... I reckon it can be done via scripting, but I would like to know if there's an easier way...

Upvotes: 0

Views: 1806

Answers (1)

Gernot Grames
Gernot Grames

Reputation: 41

#!/bin/sh
#$1 - filepath of the local repo where the tree begins
#$2 - repo id like in settings.xml e.g. myrepo
#$3 - url of the repo eg. http://my-nexus.at:8081/repository/myrepo_releases/

MAVEN_EXECUTABLE="/apache-maven-3.3.9/bin/mvn"

if [ $# -eq 4 ]
then
    echo "Search in repo: $1"
    find $1 -type f -iname "*.jar" -print0 | while IFS= read -r -d $'\0' line; do
    relative_path_name="${line/$1/}"
    echo "RelativePathName: $relative_path_name"

    IFS='/' read -r -a array <<< "$relative_path_name"

    length=${#array[@]}

    pos=0;
    artifactidends=$((length -1 -2))    #-1 because auf array starts at 0 and length returns the count

    versionends=$((length -1 - 1))      #-1 because auf array starts at 0 and length returns the count
    version="${array[$versionends]}"
    filenameends=$((length -1))
    filename="${array[$filenameends]}"

    artifactIdVersion=${array[$artifactidends]}"-"$version

    ## find out classification
    classification="${filename/.jar/''}"
    classification="${classification/$artifactIdVersion/''}"

    if [[ ${classification:0:1} == "-" ]]
    then
        classification=${classification:1}
    fi

    groupends=$((length - 3))

    group=

    for element in "${array[@]}"
    do
        if [ $pos -lt $groupends ]
        then
            echo "GroupPart: ${array[$pos]}"
            group=$group"."${array[$pos]}
        fi
        pos=$((pos + 1))

    done
    group=${group:1}
    echo "Group: $group"

    if [ $group == "$4" ]
    then
        echo "$relative_path_name"
        echo "ArtifactId: ${array[$artifactidends]}"
        echo "Version: $version"
        echo "Classification: $classification"
        echo "Group: $group"

        mavencmd="$MAVEN_EXECUTABLE deploy:deploy-file -DgroupId=$group -DartifactId=${array[$artifactidends]} -Dversion=$version -Dclassifier=$classification -Dpackaging=jar -Dfile=./$relative_path_name -DrepositoryId=$2 -Durl=$3"
        echo "MavenCMD: $mavencmd"
        eval $mavencmd
        echo "upload done"
    else
        echo "upload not done (group does not match: $group)"
    fi
    echo -e ""
    echo -e ""
done
else
    echo "USAGE"
    echo -e ""
    echo "   * Call the command with params:"
    echo "      ./upload_all_to_nexus.sh localRepoFilepath repoId repoUrl groupToConsider"
    echo -e ""
    echo "   * Example:"
    echo "      ./upload_all_to_nexus.sh /tmp/not_local_repository/ myrepo http://my-nexus.at:8081/repository/repo my.group"

fi

Upvotes: 2

Related Questions