和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.sh source ../common/tool.sh hasMaven r=$? SRC_MAVEN_FILES=./apache-maven-3.0.4 DST_MAVEN_FILES=/usr/apache-maven-3.0.4 #!/bin/bash source /etc/environment if [ $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/mvn fi
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/bash function copyFolder { if [ -d "$2" ] then echo "$2 folder exists already, stop copying." else echo "$2 folder doesn't exists, start copying..." cp -r $1 $2 fi } #create a link($2) to file($1) function createLink { if [ -f "$2" ] then echo "$2 link exists already, removing it..." rm $2 else echo "$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 found function setEnv { source /etc/environment if [ "${!1}" = "$2" ] then echo "$1 is correct: $2" else echo "$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/environment fi source /etc/environment fi }
maven目录下还有一个check.sh脚本: