maven auto install

It's cumbersome to repeat the same thing many times. When working on several machines, I need to install Maven on each machine.
I wrote a simple script which can ease this small task.

#! /bin/bash
# This script downloads and installs maven automatically
# Author: Gerald

dstdir=''
defaultrepo="${HOME}/.m2/repository"
repo=${defaultrepo}

function parseParams(){
  if [ $# -lt 1 ]; then
    errorMsg
    return
  fi

  param=''
  while [ $# -ne 0 ]; do
    param=$1
    shift
    if [ ${param} == "-r" ]; then
      if [ $# -eq 0 ]; then
        errorMsg
        return
      else
        param=$1
        repo=${param%/}
        shift
      fi
    else
      dstdir=${param%/}
    fi
  done
}
function checkParams(){
if [ "x${JAVA_HOME}" == "x" ]; then
  echo "You must set environment variable JAVA_HOME first"
  return 1
fi
if !( [ -d ${dstdir} ] && [ -w ${dstdir} ]); then
cat<<-EOF
Make sure your input parameter "${dstdir}" is correct
EOF
return 1
fi

if [ "${repo}" == "${defaultrepo}" ]; then
  mkdir -p "${repo}"
  return 0
elif !( [ -d ${repo} ] && [ -w ${repo} ]); then
cat<<-EOF
Make sure your input parameter "${repo}" is correct
EOF
return 1
else
  cat <<-EOF
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Your local repository is not the default value. You shoulde change
configuration file conf/settings.xml.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
EOF
fi
return 0
}
function errorMsg(){
cat <<-EOF
  You have not provided enough parameters.
  Usage:
    ./maven_install.sh install_dir [-r repository]
EOF
}
bashprofile=''
function getBashProfilePath(){
  if [ -f ~/.bash_profile ]; then
    bashprofile="${HOME}/.bash_profile"
  elif [ -f ~/.profile ]; then
    bashprofile="${HOME}/.profile"
  else
    echo "Cannot find your bash profile"
  fi
}

urlbase='http://apache.tradebit.com/pub/maven/binaries/'
package='apache-maven-2.0.10-bin.tar.gz'
function downloadAndInstallMaven2(){
  getBashProfilePath
  url="${urlbase}${package}"
  mkdir -p /tmp/maven2
  wget "${url}" -O "/tmp/maven2/${package}"
  oldpwd="`pwd`"
  cd /tmp/maven2
  tar zxf ${package}
  mavendirname=${package%-bin.tar.gz}
  cp -r ${mavendirname} "${dstdir}"
  rm ${package}
  cd ${oldpwd}
  cd ${oldpwd}
  echo "" >> ${bashprofile}
  echo "export M2_HOME=${dstdir}/${mavendirname}" >>${bashprofile}
  echo 'export M2=${M2_HOME}/bin' >>${bashprofile}
  echo 'export PATH=${M2}:${PATH}' >>${bashprofile}
  # . ${bashprofile}
}

parseParams $@
checkParams
if [ $? -ne 0 ]; then
  exit 1
fi
downloadAndInstallMaven2
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License