bash 指令碼編程十四 maven自動部署

來源:互聯網
上載者:User

和Java自動部署的思路一樣,通過在/usr/bin下建立mvn連結,指向/etc/alternatives/mvn連結,/etc/alternatives/mvn指向/usr/mvn3連結,/usr/mvn3連結指向/usr/apache-maven-3.0.4目錄。

由於maven運行是要檢查JAVA_HOME環境變數,而JAVA_HOME環境變數的設定可以放在安裝Java的指令碼中執行。

現在看一下maven的安裝指令碼install.sh

#!/bin/bash source check.shsource ../common/tool.shhasMavenr=$?SRC_MAVEN_FILES=./apache-maven-3.0.4DST_MAVEN_FILES=/usr/apache-maven-3.0.4#!/bin/bash source /etc/environmentif [ $r -eq 1 ]then    echo "maven was installed"else    echo "maven was not installed"    copyFolder $SRC_MAVEN_FILES $DST_MAVEN_FILES    createLink $DST_MAVEN_FILES /usr/mvn3    createLink '/usr/mvn3/bin/mvn' '/etc/alternatives/mvn'    createLink /etc/alternatives/mvn /usr/bin/mvnfi

tool.sh指令碼位於common目錄裡面。整個自動化部署的目錄結構如下:

chenshu@sloop2:~/work/189/appengine/deploy$ tree -L 2.├── common│   └── tool.sh├── java│   ├── check.sh│   ├── install.sh│   ├── jdk1.6.0_35│   └── readme├── maven│   ├── apache-maven-3.0.4│   ├── check.sh│   └── install.sh└── readme

以後還會增加其他的子項目,比如mysql, glassfish, mongodb, nginx,或許有天我會將之建立一個開源項目。

tool.sh指令碼如下:

#!/bin/bashfunction copyFolder {    if [ -d "$2" ]    thenecho "$2 folder exists already, stop copying."    elseecho "$2 folder doesn't exists, start copying..."cp -r $1 $2    fi}#create a link($2) to file($1)function createLink {    if [ -f "$2" ]    thenecho "$2 link exists already, removing it..."rm $2    elseecho "$2 link doesn't exists, creating it..."    fi    echo "creating link: $2 to $1"    ln -s $1 $2}# $1 variable name# $2 expected value# put this into /etc/environment if not foundfunction setEnv {    source /etc/environment    if [ "${!1}" = "$2" ]    thenecho "$1 is correct: $2"    elseecho "$1 is wrong: ${!1} != $2"h=`grep "$1=\"$2\"" /etc/environment`if [ -n "$h" ]then    echo "/etc/environment has $1 already"else    echo "Adding $1 into /etc/environment..."    echo "$1=\"$2\"" >> /etc/environmentfisource /etc/environment    fi}

maven目錄下還有一個check.sh指令碼:

#!/bin/bash #check java is available in this sessionfunction hasMaven {    v=`mvn -version 2>&1`    echo 'check maven ...'    pos=`expr match "$v" 'Apache Maven 3.0.4'`    if [ $pos -gt 0 ]    thenreturn 1    elsereturn 0    fi}

到現在為止,用模組化的方式編寫bash,讓後面的工作可以複用前面的指令碼。感覺輕鬆多了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.