We can install external jar files into a local maven repository by using the following script.
#!/bin/bash
# Duy Dinh
# 19 may 2015
# install external jar in a local maven repository from POM files
# bash listFiles.sh DIR mavenDIR
directory=$1 # input directory containing POM/jar files
mavenDir=$2 #, e.g. /d/IDE/maven-3.3.1/bin/
args=$#
# global settings
# check Java home
if ! (test -d "$JAVA_HOME") then
# using the default setting
JAVA_HOME="/c/Program Files/Java/jre7"
echo "JAVA_HOME not found, so, we use the default one located at $JAVA_HOME"
if ! (test -d "$JAVA_HOME") then
echo "JAVA_HOME is not specified. Please specify it and export it as environment variable PATH"
exit 0
fi
export JAVA_HOME
export PATH=$PATH:"$JAVA_HOME/bin"
fi
echo "JAVA_HOME : $JAVA_HOME"
pattern="-iname *.pom"
BATCH_SIZE=100
usage(){
echo "Usage:"
echo "bash $0 INPUT mavenDir"
echo
echo "Description: "
echo "INPUT: input directory containing JAR/POM files"
echo "mavenDir: maven directory, note that maven must be configured with the proxy settings (if required) and the location of the local maven repository."
echo
echo "Example:"
echo "bash $0 ./ /home/duy/maven3.3.1/bin"
}
# count the total number of jar files having an associated pom
countFiles(){
input=$1
extension=$2 # pom
sum=0
local counter=0
for e in `find $input -iname *.$extension`
do
if [[ $(($counter%$BATCH_SIZE)) -eq 0 ]]; then
echo "[$counter] scanning next $BATCH_SIZE documents ..."
fi
if ! (test -d $e) then
filename=$(basename $e)
#extension=${filename##*.}
name=${filename%.*}
parent=`dirname $e`
# test if jar file exists
if [ -f "$parent/$name.jar" ]; then
sum=`expr $sum + 1`
fi
fi
counter=`expr $counter + 1`
done
# total number of files found
#total=$sum
return $sum # sum is a global variable
}
#--------------------------- MAIN ----------------------------------------------------
# check argument length
if [ ${#args} -lt 1 ]
then
usage
exit 0
fi
echo "Counting the number of jar files having an associated pom ..."
countFiles $directory "pom"
echo "Total number of jar files having a POM: $sum files found."
id=1
echo "Listing files in directory $directory"
echo "find $directory $pattern"
for e in `find $directory $pattern`
do
if !(test -d $e)
then
echo
echo "*** [$id/$sum] Processing file $e"
filename=$(basename $e)
extension=${filename##*.}
name=${filename%.*}
parent=`dirname $e`
# test if jar file exists
if [ -f "$parent/$name.jar" ]; then
#echo "$mavenDir/mvn install:install-file -Dfile=$parent/$name.jar -DpomFile=$e"
$mavenDir/mvn install:install-file -Dfile=$parent/$name.jar -DpomFile=$e
id=`expr $id + 1`
fi
fi
done
echo "Program terminated!"
#--------------------------- END MAIN ----------------------------------------------------