Skip to content

Instantly share code, notes, and snippets.

@lefou
Last active November 6, 2020 09:32
Show Gist options
  • Save lefou/a7646179feb9a7bdbcafc2c53f5fb034 to your computer and use it in GitHub Desktop.
Save lefou/a7646179feb9a7bdbcafc2c53f5fb034 to your computer and use it in GitHub Desktop.
mvn wrapper that runs maven from the project top directory with `-pl {cwd}`
#!/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