Gradle how to turn a flat directory of massive amount of jar files to the .m2/repository structure?

Viewed 51

I wonder how can I quickly take a directory in my computer that contains 1K jars, and let Gradle structure it to the Maven .m2/repository structure. I've read of the Gradle publishing feature, but it seems pretty impossible to use when the amount of jars is that big because they are not taking the information from the META-INF, but want the user to specify the group ID and other details.

1 Answers

I made you wait a little, however I come up with a solution at this point. Have a look at this bash script:

#!/usr/bin/env sh
PWD=$( pwd )
JARS=$( find $PWD -name '*.jar' )
for JARFILE in $JARS
do
    echo "found JAR: $JARFILE";
    mkdir -p $PWD/extracted/$JARFILE/;
    unzip $JARFILE -d $PWD/extracted/ &> /dev/null;
    if [ -d "$PWD/extracted/META-INF/maven/" ]; then
        POMPROPS=$( find $PWD/extracted/META-INF/maven -name 'pom.properties' );
        echo "found POMPROPS: $POMPROPS";
        POMXML=$( find $PWD/extracted/META-INF/maven -name 'pom.xml' );
        echo "found POMXML: $POMXML";
        ARTIFACTID=$( grep 'artifactId' $POMPROPS | cut -d'=' -f2 | tr -d '\r' );
        GROUPID=$( grep 'groupId' $POMPROPS | cut -d'=' -f2 | tr -d '\r' );
        VERSION=$( grep 'version' $POMPROPS | cut -d'=' -f2 | tr -d '\r' );
        if [[ -f $POMPROPS && -f $POMXML ]]; then
            GROUPDIR=$( sed 's/\./\//g' <<<$GROUPID);
            echo "Group dir: "$GROUPDIR;
            mkdir -p ~/.m2test/repository/$GROUPDIR/$ARTIFACTID/$VERSION/
            cp $POMXML ~/.m2test/repository/$GROUPDIR/$ARTIFACTID/$VERSION/$ARTIFACTID-$VERSION.pom
            cp $JARFILE ~/.m2test/repository/$GROUPDIR/$ARTIFACTID/$VERSION/$ARTIFACTID-$VERSION.jar
        else
            echo "did not find pom properties and/or xml "
            echo "$JARFILE: did not find pom properties and/or xml" >> log.txt
        fi
    else
        echo "$JARFILE: no maven directory found" >> log.txt
    fi
    rm -rf $PWD/extracted/;
done

First have a look at it and understand it, please. You should not run any script, without understanding it. Of course I don't want to screw you, but always be cautious . You must have sed, grep and unzip installed. However most distributions should have that on board.

Just run that script, when in a terminal inside of the directory. It should organize your JARs inside of ~/.m2test. If some files were not found, it will be logged inside log.txt. Eventually, you can copy-paste or move the files from ~/.m2test to ~/.m2 as soon, as you are optimistic, it worked out.

And of course: Always do make backups (of your ~/.m2 in this case) !

This - of course - only works, if your JARs have that maven directory in META-INF. If it doesn't, I don't have a better guess, how to organize it into the local repository.

Related