Skip to content

Instantly share code, notes, and snippets.

@alexkubica
Last active June 15, 2020 12:30
Show Gist options
  • Save alexkubica/6bf7e3bdb807c2c247434911aab0778a to your computer and use it in GitHub Desktop.
Save alexkubica/6bf7e3bdb807c2c247434911aab0778a to your computer and use it in GitHub Desktop.
Download maven artifacts with sources and javadocs into a chosen destination

NEW WAY:

Make sure you have JDK and maven installed.
Create a pom.xml file with:

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.cooldomain -DartifactId=mvn-whiten

It will create a new maven project but you only need the POM file.
Search your packages in the central maven repository and add them to the POM's dependencies.
Run on the same directory:

mvn de.qaware.maven:go-offline-maven-plugin:resolve-dependencies -DdownloadSources -DdownloadJavadoc -Dmaven.repo.local=.

Enjoy!
For more info on the plugin see: https://github.com/qaware/go-offline-maven-plugin

OLD SCRIPT:

Before you start make sure you have jdk8, maven 3.5.0 and sh installed.

Save the next file as mvn-whiten.sh:

#!/bin/bash

# Prints usage message
BASENAME="$0"
usage ()
{
    echo "Usage : (basename "$BASENAME") [options]"
    echo ""
    echo "Description: Downloads artifacts from maven central repository with sources and javadocs"
    echo ""
    echo "Options:"
    echo "-d,--dest <arg>        Destination for downloaded artifacts which should be relative to"
    echo "                       current directory, defaults to 'whiten'"
    echo "-a,--artifact <arg>    Artifact's identifier, should be groupId:artifactName:version"
    echo "-f,--file <arg>        Get artifact identifiers list from file"
    echo "-h,--help              Display help information"
}

download_artifact()
{
    # Download artifact with sources and javadocs
    mvn dependency:get -Dmaven.repo.local="$1" -Dartifact="$2:jar"
    mvn dependency:get -Dmaven.repo.local="$1" -Dartifact="$2:jar:sources"
    mvn dependency:get -Dmaven.repo.local="$1" -Dartifact="$2:jar:javadoc"

    # Download artifact's dependencies' sources and javadocs
    download_artifact_dependencies_sources_and_javadocs $1 $2
}

download_artifact_dependencies_sources_and_javadocs()
{
   # Save current working directory path
   WORKING_DIR=$(pwd)

   # Split artifact id to array of groupId, artifactName and version
   IFS=':' read -r -a ARTIFACT_PARTS <<< "$2"

   # Move to downloaded directory
   cd "$1/${ARTIFACT_PARTS[0]//\./\/}/${ARTIFACT_PARTS[1]}/${ARTIFACT_PARTS[2]}"

   # Copy artifact's POM file to pom.xml
   cp "${ARTIFACT_PARTS[1]}-${ARTIFACT_PARTS[2]}.pom" pom.xml

   # Run sources and resolve for sources and javadocs of dependencies
   mvn dependency:sources -Dmaven.repo.local="$WORKING_DIR/$1" -DincludeParents=true
   mvn dependency:resolve -Dmaven.repo.local="$WORKING_DIR/$1" -Dclassifier=javadoc -DincludeParents=true

   # Remove pom.xml file
   rm pom.xml

   # Return to working directory
   cd $WORKING_DIR
}

# Get arguments
DEST='whiten'
while [ "$1" != "" ]; do
    case $1 in
        -a | --artifact ) shift
                          ARTIFACT=$1
                          ;;
        -f | --file )     shift
                          FILE=$1
                          ;;
        -d | --dest )     shift
                          DEST=$1
                          ;;
        -h | --help )     usage
                          exit
                          ;;
        * )               usage
                          exit 1
    esac
    shift
done

# Validate arguments
if [ "$DEST" = "" ]; then
    usage
    exit 1
fi
if [ "$ARTIFACT" = "" ]; then
    if [ "$FILE" = "" ]; then
        usage
        exit 1
    else
        while read -r line || [[ -n "$line" ]]; do
            download_artifact $DEST $line
        done < "$FILE"
    fi
else
    download_artifact $DEST $ARTIFACT
fi

Make it executable with:

chmod +x ./mvn-whiten.sh

Make sure the script and artifacts list files are saved with unix line endings! (Or you'll have problems on windows)

To do so open them with vim and write with :w ++ff=unix.

Good luck!

@Schniz
Copy link

Schniz commented Jan 27, 2017

💯!!

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