Skip to content

Instantly share code, notes, and snippets.

@caiofaustino
Created April 15, 2021 11:57

Revisions

  1. caiofaustino created this gist Apr 15, 2021.
    87 changes: 87 additions & 0 deletions upload_artifacts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    #!/bin/bash
    echo "Starting upload script"
    sonatype_user="<your_user>"
    sonatype_password="<your_password>"

    # Path where the files are located
    path=$1
    # Path inside the repo if not root path, (com/package/group)
    repo_path_base=$2

    echo "path: $path"
    echo "repo_path_base: $repo_path_base"

    if [ -d "$path" ]; then
    # Take action if $DIR exists. #
    echo "Path exists!"

    # Your URL could be different if you have a new account
    MAVEN_CENTRAL_STAGINGURL="https://oss.sonatype.org/service/local/staging/deploy/maven2"
    MAVEN_CENTRAL_REPOID="ossrh"

    for file_path in $path/*; do
    #If is a file (with extension)
    if [[ $file_path == *"."[a-z][a-z][a-z] ]]; then

    if [[ $file_path == *".pom" ]]; then
    echo "Is POM fille"
    # Find POM file path
    pom_file=$file_path
    # Loop files again
    for file in $path/*; do
    if [[ $file == *".pom" ]]; then
    echo "Ignoring POM file again"
    elif [[ $file == *".aar" ]]; then
    echo "Uploading AAR file."

    (cd $path && mvn gpg:sign-and-deploy-file \
    -Durl=$MAVEN_CENTRAL_STAGINGURL \
    -DrepositoryId=$MAVEN_CENTRAL_REPOID \
    -DpomFile=$pom_file \
    -Dfile=$file)

    echo ""
    elif [[ $file == *"-javadoc.jar" ]]; then
    echo "Uploading Javadoc file."
    (cd $path && mvn gpg:sign-and-deploy-file \
    -Durl=$MAVEN_CENTRAL_STAGINGURL \
    -DrepositoryId=$MAVEN_CENTRAL_REPOID \
    -DpomFile=$pom_file \
    -Dfile=$file \
    -Dclassifier=javadoc)

    echo ""
    elif [[ $file == *"-sources.jar" ]]; then
    echo "Uploading Sources file."

    (cd $path && mvn gpg:sign-and-deploy-file \
    -Durl=$MAVEN_CENTRAL_STAGINGURL \
    -DrepositoryId=$MAVEN_CENTRAL_REPOID \
    -DpomFile=$pom_file \
    -Dfile=$file \
    -Dclassifier=sources)

    echo ""
    else
    echo "UNEXPECTED FILE - $file"
    echo ""
    fi
    done
    fi
    else
    # If is a folder, call recursively
    dir=$file_path
    dir="$(basename $dir)"

    echo "Starting script again with: $file_path"
    echo $file_path
    new_base="$repo_path_base/$dir"
    echo $new_base
    # CHANGE SCRIPT PATH HERE
    /path/to/script/upload_artifacts.sh $file_path $new_base
    fi
    echo ""
    done
    else
    echo "Path not found, please provide valid path as argument!"
    fi