Skip to content

Instantly share code, notes, and snippets.

@lefou
Created February 8, 2018 09:21
Show Gist options
  • Save lefou/0a95895da5c9e7eeede16b4238c80a88 to your computer and use it in GitHub Desktop.
Save lefou/0a95895da5c9e7eeede16b4238c80a88 to your computer and use it in GitHub Desktop.
Run Maven from the project root directory with the current project pre-selected
#!/usr/bin/env bash
basedir=
dirname=
found=0
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
if [ -z "$1" ]
then
echo "Path not specified to find_maven_basedir"
return 1
fi
initdir="$1"
basedir="$1"
wdir="$1"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
found=1
break
fi
if [ -z "$dirname" ]; then
dirname="`basename ${wdir}`"
else
dirname="`basename ${wdir}`/${dirname}"
fi
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
if [ -d "${wdir}" ]; then
wdir=`cd "$wdir/.."; pwd`
fi
# end of workaround
done
}
find_maven_basedir "$(pwd)"
#echo "Basedir: $basedir"
#echo "project dir: $dirname"
#echo "Found parent: $found"
if [ "$found" = "1" ]; then
echo "Executing Maven from base dir [$basedir] with project list [$dirname]"
(cd "$basedir" && mvn -pl "$dirname" "$@")
unset basedir dirname found
else
unset basedir dirname found
exec mvn "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment