苦力活:
1. 自动下载Maven
2. 设置环境变量
3. 配置mirror
安装:下载 maven.rar 按照 readme 执行即可
mirror配置:
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>central</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> <mirror> <id>ibiblio</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> </mirror> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>central</mirrorOf> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror> <mirror> <id>maven.net.cn</id> <name>oneof the central mirrors in china</name> <url>http://maven.net.cn/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
############################# 结束符 ##########################
贴上shell 文件:
#!/bin/sh #关闭进程 killpids(){ pids=($(ps -ef | grep $1 | awk '{print $2}')) for pid in ${pids[@]}; do echo "关闭进程: pid ${pid}" kill -9 ${pid} done } killpids maven #删除旧版maven echo "删除旧版maven" rm -rf /usr/local/apache-maven-* #install maven srcPath=$(find /usr/maven -maxdepth 1 -type f|grep '.*maven.*.tar.gz$') if [[ $srcPath =~ "maven" ]] then echo "安装包:$srcPath" rpm -ivh $srcPath else #下载maven yum -y install wget echo -e "Download Maven" wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz -P /usr/maven/ srcPath=$(find /usr/maven -maxdepth 1 -type f|grep '.*maven.*.tar.gz$') fi #解压 echo "解压..." tar -xf $srcPath -C /usr/local path=$(find /usr/local -maxdepth 1 -type d|grep 'apache-maven-.*') dir_name=${path##*/} echo $path echo $dir_name #赋予权限 chmod -R 755 $path #设置环境变量 sed -i '/M2_HOME/d' /etc/profile echo "设置环境变量" echo "export M2_HOME=$path" >> /etc/profile echo 'export PATH=$PATH:$M2_HOME/bin' >> /etc/profile source /etc/profile echo "建立软链接 $path -> apache-maven" ln -s $path apache-maven echo "配置 mirrors" IFSBAK=$IFS #备份原来的值 IFS=# mirror=" # <mirror> # <id>alimaven</id> # <name>aliyun maven</name> # <url>http://maven.aliyun.com/nexus/content/groups/public/</url> # <mirrorOf>central</mirrorOf> # </mirror> # <mirror> # <id>central</id> # <name>Maven Repository Switchboard</name> # <url>http://repo1.maven.org/maven2/</url> # <mirrorOf>central</mirrorOf> # </mirror> # <mirror> # <id>repo2</id> # <mirrorOf>central</mirrorOf> # <name>Human Readable Name for this Mirror.</name> # <url>http://repo2.maven.org/maven2/</url> # </mirror> # <mirror> # <id>ibiblio</id> # <mirrorOf>central</mirrorOf> # <name>Human Readable Name for this Mirror.</name> # <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> # </mirror> # <mirror> # <id>jboss-public-repository-group</id> # <mirrorOf>central</mirrorOf> # <name>JBoss Public Repository Group</name> # <url>http://repository.jboss.org/nexus/content/groups/public</url> # </mirror> # <mirror> # <id>maven.net.cn</id> # <name>oneof the central mirrors in china</name> # <url>http://maven.net.cn/content/groups/public/</url> # <mirrorOf>central</mirrorOf> # </mirror> # ' " #循环写入文件 for line in $mirror; do sed -i "/<\/mirrors>/i\ $line" $path/conf/settings.xml done IFS=$IFSBAK #还原 echo "mvn -v" mvn -v echo ""