#!/bin/sh
# -----------------------------------------------------------------------------
# Maven functions
#
# @author Jason van Zyl
# @author Emmanuel Venisse
# -----------------------------------------------------------------------------
POM=pom.xml
downloadMBootDependencies()
{
findAndSetMavenRepoLocal
for i in `cat ${MBOOT_HOME}/deps`
do
echo $i | grep SNAPSHOT > /dev/null 2>&1
snapshot=$?
if [ "$snapshot" = "0" ] || [ ! -f $repoLocal/$i ]
then
mkdir -p `dirname $repoLocal/$i` > /dev/null 2>&1
wget -q -O $repoLocal/$i http://www.ibiblio.org/maven/$i
fi
done
}
createMBootClasspath()
{
findAndSetMavenRepoLocal
if [ "$cygwin" = "true" ]
then
MBOOT_CP=`cygpath -pu "$MBOOT_CP"`
fi
for i in `cat ${MBOOT_HOME}/deps`
do
MBOOT_CP=${MBOOT_CP}:$repoLocal/$i
done
}
findAndSetMavenRepoLocal()
{
if [ -f $HOME/maven.properties ]
then
repoLocal=`cat $HOME/maven.properties | egrep "^maven.repo.local" | sed 's/^.*= *//'`
else
repoLocal=$HOME/.maven/repository
fi
if [ "$cygwin" = "true" ]
then
repoLocal=`cygpath -pu "$repoLocal"`
fi
}
isCommandSuccessful()
{
# $1 == $?
# $2 == Error message on failure
ret=$1
if [ $ret != 0 ]; then
echo $2
exit $ret
fi
}
runJava()
{
# $1 == classpath
# $2 == Main class
# $3 == Main args
if $cygwin; then
CP=`cygpath -pw "$1"`
else
CP=$1
fi
"${JAVACMD}" -classpath "$CP" $2 "$3"
}
runTests()
{
# $1 == classpath
# $2 == home args
# $3 == repo local
# $4 == dependencies file
# $5 == includes file
# $6 == excludes file
if $cygwin; then
CP=`cygpath -pw "$1"`
else
CP=$1
fi
if $cygwin; then
repo=`cygpath -pw "$3"`
else
repo=$3
fi
if [ -f $5 ]
then
"${JAVACMD}" -classpath "$CP" org.codehaus.surefire.SurefireBooter "$2" "$repo" $4 $5 $6
fi
}
compile()
{
# $1 == classpath
# $2 == destination for compiled classes
# $3 == source directory
# $4 == generated source directory
if [ -d $3 ] || [ -d $4 ]
then
if [ ! -d $2 ]
then
mkdir -p $2
fi
if [ ! -z $3 ] && [ -d $3 ]
then
sources=`find $3 -name '*.java'`
fi
if [ ! -z $4 ] && [ -d $4 ]
then
generatedSources=`find $4 -name '*.java'`
fi
if $cygwin; then
CP=`cygpath -pw "$1"`
else
CP=$1
fi
"${JAVAC}" -classpath "$CP" -d $2 ${sources} ${generatedSources}
fi
}
buildJar()
{
# $1 == directory to JAR
# $2 == JAR path relative to the cwd
(
dir=`pwd`
cd $1
if $cygwin; then
JARFILE=`cygpath -pw ${dir}/$2`
else
JARFILE=${dir}/$2
fi
${JAVA_HOME}/bin/jar -cf $JARFILE *
)
}
buildMavenProject()
{
# 1. Parse the model
# 2. Download any required dependencies
# 3. Compile the sources
# 4. Move required resources into location
# 5. Create JAR.
# $1 == directory where project lives
# $2 == jar name
# $3 == flag to install
# $4 == flag to install pom
# $5 == flag to leave mboot files
(
home=`pwd`
cd $1
# If the user only want to install the POM skip the rest
if [ "$3" = "0" ] && [ "$4" = "1" ]
then
installPom
return
fi
# Look for source directory in pom.xml
sourceDirectory=`grep sourceDirectory ${POM} | sed -e 's/^*//;s///;s/<\/sourceDirectory>//;s/\${basedir}\///'`
[ -z $sourceDirectory ] && sourceDirectory=src/main/java
buildDir=target
buildDest=target/classes
# Look for unit test source directory in pom.xml
unitTestSourceDirectory=`grep unitTestSourceDirectory ${POM} | sed -e 's/^*//;s///;s/<\/unitTestSourceDirectory>//;s/\${basedir}\///'`
[ -z $unitTestSourceDirectory ] && unitTestSourceDirectory=src/test/java
buildTestDest=target/test-classes
##[ -d $buildDir ] && rm -rf $buildDir
echo "Building project in `pwd`"
if $cygwin = true; then
home=`cygpath -pw $home`
fi
runJava ${MBOOT_HOME}/classes Bootstrapper ${home}
isCommandSuccessful $? "Failed running project parser!"
bootstrapClasspath=`cat bootstrap.classpath`
if $cygwin = true; then
bootstrapClasspath=`cygpath -pu "$bootstrapClasspath"`
fi
projectDependencyClassPath=$bootstrapClasspath:.
generatedSourceDirectory=target/generated-sources
compile "$projectDependencyClassPath" $buildDest $sourceDirectory $generatedSourceDirectory
isCommandSuccessful $? "Failed compiling classes!"
echo "Building tests in `pwd`"
repoLocal=`cat bootstrap.repo`
if $cygwin = true; then
repoLocal=`cygpath -pu "$repoLocal"`
fi
createMBootClasspath
if [ ! -z $unitTestSourceDirectory ] && [ -d $unitTestSourceDirectory ]
then
compile "$buildDest:$projectDependencyClassPath:${MBOOT_CP}" $buildTestDest $unitTestSourceDirectory
isCommandSuccessful $? "Failed compiling test classes!"
fi
copyResources bootstrap.resources target/classes
copyResources bootstrap.tests.resources target/test-classes
echo "Running tests in `pwd`"
# We only need the booter jar in the classpath, it will load everything else.
runTests ".:${MBOOT_HOME}/classes:$repoLocal/surefire/jars/surefire-booter-1.1.jar" "$1" "$repoLocal" bootstrap.libs bootstrap.tests.includes bootstrap.tests.excludes
isCommandSuccessful $? "Failed running project tests!"
if [ "$2" = "default" ]
then
jarName=`getJarName ${POM}`
else
jarName=$2
fi
echo "Building jars (${jarName}) in `pwd`/target"
buildJar $buildDest target/${jarName}
if [ "$3" = "1" ]
then
findAndSetMavenRepoLocal
groupId=`getGroupId`
echo "Installing ${jarName} in ${repoLocal}/${groupId}/jars"
mkdir -p ${repoLocal}/${groupId}/jars > /dev/null 2>&1
cp target/${jarName} ${repoLocal}/${groupId}/jars
installPom
fi
if [ "$5" = "0" ]
then
rm -f bootstrap.classpath > /dev/null 2>&1
rm -f bootstrap.libs > /dev/null 2>&1
rm -f bootstrap.deps > /dev/null 2>&1
rm -f bootstrap.resources > /dev/null 2>&1
rm -f bootstrap.repo > /dev/null 2>&1
rm -f bootstrap.tests.includes > /dev/null 2>&1
rm -f bootstrap.tests.excludes > /dev/null 2>&1
rm -f bootstrap.tests.resources > /dev/null 2>&1
fi
)
}
installPom()
{
findAndSetMavenRepoLocal
groupId=`getGroupId`
pomName=`getPomName ${POM}`
dir=${repoLocal}/${groupId}/poms
echo "Installing POM in ${dir}/${pomName}"
mkdir -p ${dir}
cp ${POM} ${dir}/${pomName}
}
getJarName()
{
# $1 == pom.xml
version=`cat $1 | tr '\n' ' ' | sed 's#.*##' | sed 's#.*##' | grep '' | sed -e 's#^.*##;s#.*$##'`
if [ -z $version ]
then
version=`grep currentVersion $1 | sed -e 's/^ *//;s///;s/<\/currentVersion>//'`
fi
artifactId=`cat $1 | tr '\n' ' ' | sed 's#.*##' | grep '' | sed -e 's#^.*##;s#.*$##'`
if [ -z $artifactId ]
then
artifactId=`cat $1 | tr '\n' ' ' | sed 's#.*##' | sed 's#.*##' | sed 's#.*##' | grep '' | sed -e 's#^.*##;s#.*$##'`
fi
jarName="${artifactId}-${version}.jar"
echo $jarName
}
# returns the name under which the pom should be installed
getPomName()
{
version=`cat $1 | tr '\n' ' ' | sed 's#.*##' | sed 's#.*##' | grep '' | sed -e 's#^.*##;s#.*$##'`
if [ -z $version ]
then
version=`grep currentVersion $1 | sed -e 's/^ *//;s///;s/<\/currentVersion>//'`
fi
artifactId=`cat $1 | tr '\n' ' ' | sed 's#.*##' | grep '' | sed -e 's#^.*##;s#.*$##'`
if [ -z $artifactId ]
then
artifactId=`cat $1 | tr '\n' ' ' | sed 's#.*##' | sed 's#.*##' | sed 's#.*##' | grep '' | sed -e 's#^.*##;s#.*$##'`
fi
pomName="${artifactId}-${version}.pom"
echo $pomName
}
getGroupId()
{
groupId=`cat ${POM} | tr '\n' ' ' | sed 's#.*##' | grep '' | sed -e 's#^.*##;s#.*$##'`
echo $groupId
}
copyResources()
{
# $1 == resourcesfile
# $2 == target directory
if [ -f $1 ]
then
resources=`cat $1`
for i in $resources
do
directory=`echo $i | awk 'BEGIN { FS = "@" } { print $1 }' | awk 'BEGIN { FS = "," } { print $1 }'`
targetPath=`echo $i | awk 'BEGIN { FS = "@" } { print $1 }' | awk 'BEGIN { FS = "," } { print $2 }'`
includes=`echo $i | awk 'BEGIN { FS = "@" } { print $2 }' | awk 'BEGIN { FS = "," } { for ( j = 1; j <= NF; j++ ) { printf( "%s ", $j ) } }'`
for include in $includes
do
if [ ! -d "$directory" ]
then
continue
fi
files=`eval "find $directory -name $include"`
for file in $files
do
# Replace the "/" with "@" to prevent shell expansion of *.properties
# to *.properties in the CWD.
tmpDirectory=`echo $directory | sed "s/\//@/g"`
tmpFile=`echo $file | sed "s/\//@/g"`
# Now grab the path excluding the original directory so we can translate that
# path into the target directory.
path=`echo $tmpFile | sed "s/$tmpDirectory//;s/\@/\//g;s/^\///"`
targetDirectory=$2
[ ! -z $MBOOT_DEBUG ] && echo "path = $path"
[ ! -z $path ] && translatedPath=`dirname $path`
[ ! -z $MBOOT_DEBUG ] && echo "translatedPath = $translatedPath"
if [ ! -z $targetPath ]
then
[ ! -z $MBOOT_DEBUG ] && echo "targetPath = $targetPath"
targetDirectory="${targetDirectory}/${targetPath}/${translatedPath}"
else
targetDirectory="${targetDirectory}/${translatedPath}"
fi
[ ! -z $MBOOT_DEBUG ] && echo "targetDirectory = $targetDirectory"
[ ! -d $targetDirectory ] && mkdir -p $targetDirectory
cp $file $targetDirectory > /dev/null 2>&1
done
done
done
find $2 -name 'CVS*' -exec rm -rf {} > /dev/null 2>&1 \;
fi
}
# OS specific support. $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname`" in
CYGWIN*) cygwin=true ;;
Darwin*) darwin=true
if [ -z "$JAVA_HOME" ] ; then
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
fi
;;
esac
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# You will need to specify JAVA_HOME if compiling with 1.2 or later.
if [ -n "$JAVA_HOME" ] ; then
if [ -f "$JAVA_HOME/lib/tools.jar" ] ; then
CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar
fi
if [ -f "$JAVA_HOME/lib/classes.zip" ] ; then
CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/classes.zip
fi
else
echo "Warning: JAVA_HOME environment variable not set."
echo " If build fails because sun.* classes could not be found"
echo " you will need to set the JAVA_HOME environment variable"
echo " to the installation directory of java."
fi
# IBM's JDK on AIX uses strange locations for the executables:
# JAVA_HOME/jre/sh for java and rmid
# JAVA_HOME/sh for javac and rmic
if [ -z "$JAVAC" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/sh/javac" ] ; then
JAVAC=${JAVA_HOME}/sh/javac;
else
JAVAC=${JAVA_HOME}/bin/javac;
fi
else
JAVAC=javac
fi
fi
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
else
JAVACMD=java
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly."
echo " We cannot execute $JAVACMD"
exit 1
fi
# For Cygwin, switch to Windows format before running java
if $cygwin; then
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
fi
export CLASSPATH