Skip to content

Instantly share code, notes, and snippets.

@marcastel
Last active April 7, 2023 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcastel/9065fb6abbcd1932400e3e28bb6a978b to your computer and use it in GitHub Desktop.
Save marcastel/9065fb6abbcd1932400e3e28bb6a978b to your computer and use it in GitHub Desktop.
Maven `mvn` script
#! /bin/ksh
#! @brief Apache Maven Startup Script
#
# NOTE. If you are not using the AIT framework, replace all calls to `lq`(1) by `print -u2`, for example:
# lq +error "This is an error"
# becomes
# print -u2 "error: this is an error"
function mvn_getdir {
typeset dir=$(mvn_getarg "$@") d=$dir
while [[ $d != / ]]; do [[ -d $d/.mvn ]] && { dir=$d; break; }; d=$(cd $d/..; pwd); done
print -v dir
}
function mvn_getarg {
typeset dir=$(pwd); integer -s found=0
for arg in "$@"; do
[[ $arg == -@(f|-file) ]] && { found=1; continue; }
(( found == 1 )) || continue
if [[ -d $arg ]]; then dir=$(cd $arg && pwd -P)
elif [[ -f $arg ]]; then dir=$(cd $(dirname $arg) && pwd -P)
[[ -d $dir ]] || { lq +error "$dir: directory not found [$arg]!", exit 1; }
else lq +error "$arg: PM file not found!"; exit 1; fi
break
done; print -v dir
}
# Make sure we aren't running on Windows
[[ $(uname) == @(CYGWIN|MINGW)* ]] && { lq +error "Cygwin and MingW are not currently supported!"; return 1; }
# Determine this program's name and location resolving all symbolic links
[[ -L $0 ]] && a=$(readlink -f $0) || a=$0; [[ $a == */* ]] && b=$(cd ${a%/*}; pwd -P) a=${a##*/} || b=$(pwd -P)
typeset -m progname=a progpath=b
# Determine directories of interest
typeset homedir=${MAVEN_HOME:-$(cd $progpath/..; pwd -P)}
typeset repodir=${MAVEN_BASEDIR:-$(mvn_getdir "$@")}
# Initialise default settings
integer -s skiprc=${MAVEN_SKIP_RC:+1} # Flag to disable loading of mavenrc files
typeset -m args=MAVEN_ARGS # Arguments passed to Maven before CLI arguments.
typeset -m bugs=MAVEN_DEBUG_OPTS # Undocumented
typeset -m conf=MAVEN_CONFIG # Undocumented
typeset -m opts=MAVEN_OPTS # Java runtime options used when Maven is executed.
# Source all available runtime command files
typeset rcfiles=( /usr/local/etc/mavenrc /etc/mavenrc $HOME/.mavenrc )
typeset rc; (( skiprc == 1 )) || for rc in ${rcfiles[@]}; do [[ -f $rc ]] && source $rc; done; unset rc
# JAVA_HOME must point at your Java Development Kit installation
typeset javabin; [[ -n $JAVA_HOME ]] && javabin=$JAVA_HOME/bin/java || javabin=$(whence java)
[[ -z javabin || ! -x $javabin ]] && { lq +error "JAVA_HOME environment variable not declared!"; exit 1; }
typeset jars="$(ls -1 $homedir/boot/plexus-classworlds-*.jar)"
typeset exec='org.codehaus.plexus.classworlds.launcher.Launcher'
typeset jcfg; [[ -f $repodir/.mvn/jvm.config ]] && jcfg="$(cat $repodir/.mvn/jvm.config | tr -s '\r\n' ' ')"
export MAVEN_PROJECTBASEDIR=$repodir
export MAVEN_CMD_LINE_ARGS="${conf:+$conf }$@"
exec "$javabin" $jcfg $opts $bugs -classpath "${jars}" "-Dclassworlds.conf=$homedir/bin/m2.conf" "-Dmaven.home=$homedir" \
"-Dlibrary.jansi.path=$homedir/lib/jansi-native" "-Dmaven.multiModuleProjectDirectory=$repodir" $exec $args "$@"
#! @revision 2023-04-07 (Fri) 06:58:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment