Coming Up for Air

Import Maven Artifacts to Ceylon Repos

Friday, Nov 22, 2013 |

Import Maven Artifacts to Ceylon Repos

Jason Lee 2013-11-22

In trying to come up to speed on Ceylon, I've run into some issues with module import dependencies. I'm pretty sure they're all pilot error, but it was suggested that I import the jars into the Ceylon repository and specify the dependencies between the modules. This would, effectively, be functionally the same as the <dependencies> element in the Maven POM. In classic geek, over-engineer-the-solution fashion, I cobbled together this shell script. It could be more elegant, but it seems to work, and it was much simpler than a Java implementation using the Maven APIs. :)

Here's the script:

#!/bin/bash

FILE=deps
BASE=http://search.maven.org/remotecontent?filepath=

function download() {
    curl --remote-name -s $1
}

function getRepoDir() {
    GROUP=$1
    ARTIFACT=$2
    VERSION=$3

    REPODIR=`getDir $GROUP/$ARTIFACT`
    echo $HOME/.ceylon/repo/$REPODIR/$VERSION
}

function getUrl() {
    G=$1
    A=$2
    V=$3
    GPATH=`echo $G | sed -e 's/\./\//g'`

    echo "${BASE}$GPATH/$A/$V/$A-$V"
}

function getDir() {
    echo $1 | sed -e 's/\./\//g'
}

function downloadFiles() {
    GROUP=$1
    ARTIFACT=$2
    VERSION=$3

    URL=`getUrl $GROUP $ARTIFACT $VERSION`
    download $URL.pom
    download $URL.jar
}

function importJar() {
    GROUP=$1
    ARTIFACT=$2
    VERSION=$3
    ceylon import-jar --out $HOME/.ceylon/repo $GROUP.$ARTIFACT/$VERSION $ARTIFACT-$VERSION.jar
}

function getDeps() {
    mvn dependency:tree -f *.pom  | grep "\- " | grep -v ":test" | grep "\[INFO\] +- " | cut -c 11- > $FILE
}

function processDeps() {
    REPODIR=$1

    for DEP in `cat $FILE` ; do
        DGROUP=`echo $DEP | cut -f 1 -d ":"`
        DARTIFACT=`echo $DEP | cut -f 2 -d ":"`
        DVERSION=`echo $DEP | cut -f 4 -d ":"`

        echo "$DGROUP.$DARTIFACT=$DVERSION" >> $REPODIR/module.properties
        DEPREPODIR=`getRepoDir $DGROUP $DARTIFACT $DVERSION`

        processArtifact $DGROUP $DARTIFACT $DVERSION
    done
}

function processArtifact() {
    GROUP=$1
    ARTIFACT=$2
    VERSION=$3
    REPODIR=`getRepoDir $GROUP $ARTIFACT $VERSION`

    if [ ! -e $REPODIR ] ; then
        echo "Processing $GROUP:$ARTIFACT:$VERSION"
        #read -p "Press enter to continue: "

        mkdir -p "$ARTIFACT"
        cd "$ARTIFACT"

        downloadFiles $GROUP $ARTIFACT $VERSION
        importJar $GROUP $ARTIFACT $VERSION
        getDeps
        processDeps $REPODIR

        rm -rf "./$ARTIFACT"
    else
        echo "A module for $GROUP:$ARTIFACT:$VERSION has been found. Skipping."
    fi
}

if [ $# -lt 3 ] ; then
    echo "Usage: mvnimport.sh <groupId> <artifactId> <version>"
    exit -1
fi

processArtifact $1 $2 $3

To import a Maven artifact, all you have to do is specify the group ID, artifact, and version:

$ mvnimport.sh com.google.guava guava 14.0.1
Processing com.google.guava:guava:14.0.1
Processing com.google.code.findbugs:jsr305:1.3.9

It will import the artifact, record its immediate dependencies, then recursively process each dependency. If a module for the given coordinates has been found, the script currently skips it.

As I said, this seems to work, but if there are bugs are ways to improve it, I'm all ears. :)