Shell one-click installation of maven

Preface

The following script has been tested on CentOS 7.9.

Parameter description:
$1: path to decompress the installation package

$2: maven download link

$3: The storage address of the installation package after downloading, optional parameter, defaults to the current folder

source <脚本名称> <$1:安装路径> <$2:下载链接> <$3:下载存放地址,可选参数>

Run the example:

source maven_install.sh ../server/ https://dlcdn.apache.org/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz

Installation script

#!/bin/bash

# 校验参数
if [ ! "$1" ];then
  echo "请输入安装地址"
  return
fi

if [ ! "$2" ];then
  echo "请输入下载地址"
  return
fi

# 文件夹不存在则创建
if [ ! -d $1 ];then
  mkdir -p $1
  echo "======================= 创建文件夹 $1 ======================="
fi

# 下载maven安装包
if [ "$3" == "" ];then
  wget $2
else
  wget $2 -P $3
fi

# 下载maven安装包的名称
package_names=($(ls -t1 $3 | grep maven | grep tar.gz | awk '{print $1}'))
package_name=${package_names[0]}

# 解压安装包
if [ "$3" == "" ];then
  tar -zxvf $package_name -C $1
else
  unzip_path=$(cd $3 && pwd)
  tar -zxvf "$unzip_path/$package_name" -C $1
fi

# 获取解压后的文件夹名称
folder_names=($(ls -t1 $1 | grep maven | awk '{print $1}'))
folder_name=${folder_names[0]}

# 配置环境变量
install_path=$(cd $1/$folder_name && pwd)

echo -e "\n# maven配置" >> /etc/profile.d/my_env.sh
echo "export MAVEN_HOME=$install_path" >> /etc/profile.d/my_env.sh
echo "export PATH=\$MAVEN_HOME/bin:\$PATH" >> /etc/profile.d/my_env.sh

# 刷新环境变量
source /etc/profile

# 验证环境变量是否生效
echo "==============================================="
mvn -version

if [ $? == 0 ];then
  echo "=========== maven install success ==========="
else
  echo "=========== maven install failed ==========="
fi

Finish

The above is the script for installing maven. If you have any questions, please feel free to communicate.

Guess you like

Origin blog.csdn.net/LSW_JAVADP/article/details/130361223