科技始终来自于懒惰的人性
相信有用Jboss发布项目的人都会有段痛苦的经历。首先要打包,其次替换掉Jboss里旧的包,然后重启Jboss服务。相信如果一天如果多次这样做的话你会疯掉,我在疯掉多次以后终于写出了一个shell脚本来替换这繁琐重复的工作。
1.shell脚本
pub.sh主脚本及waiting.sh动态显示等待信息脚本
pub.sh如下,主要完成:使用maven把项目打包, 将旧包备份为*bk,将新包拷贝至Jboss下同时重启Jboss。
pub.sh如下,主要完成:使用maven把项目打包, 将旧包备份为*bk,将新包拷贝至Jboss下同时重启Jboss。
#!/bin/bash
#Author: ju7tme
#$1 the project name
#the path of workspace
export WORKSPACE_HOME=/home/ju7tme/workspace/
#the path of jboss
export JBOSS_HOME=/usr/local/jboss/
#waiting time
WAITING_TIME=15
cd $WORKSPACE_HOME/$1
echo -n "[INFO] creat the package"
(waiting $WAITING_TIME)&(mvn package -DskipTests=true | grep SUCCESS);
wait
echo "[INFO] if there is no success info,there is something wrong with the project"
#get the path of package
packagePath=`find -name *.war -print`
if [ -z "$packagePath" ]; then
echo "[ERROR] cann't find the package............................... FAILURE "
exit -1;
fi
#remove '.' for the path
packagePath=`echo $packagePath | sed s/^.//`
#get the name of the package
package=`echo $packagePath | awk -F"/" '{print $(NF-0)}'`
echo "[INFO] the package is: "$package"............................ SUCCESS "
echo "[INFO] ------------------------------------------------------------------------"
cd $JBOSS_HOME/server/default/deploy/
#if there has the backup package,rm it
if [ -f "$package.bk" ]; then
rm $package.bk
echo "[INFO] the backup package has been rm......................... SUCCESS "
fi
#backup the old package
if [ -f "$package" ]; then
mv $package $package.bk
echo "[INFO] the old package has been backup........................ SUCCESS "
fi
#cp the package
cp $WORKSPACE_HOME/$1/$packagePath $package
echo "[INFO] the new package has been cp to the space............... SUCCESS "
echo "[INFO] ------------------------------------------------------------------------"
echo "[INFO] now shutting down JBoss"
sudo killall -9 java
wait
echo "[INFO] the JBoss has been stop................................ SUCCESS "
echo "[INFO] now restarting JBoss"
sudo $JBOSS_HOME/bin/run.sh
waiting.sh如下,主要工作:动态显示等待信息。
#!/bin/bash
i=0
j=0
SLEEP_TIME=0.2
WAIT_TIME=$[$1*5]
while [ 0 ]
do
case $i in
0)
echo -en "-"
;;
1)
echo -en "\b/"
;;
2)
echo -en "\b|"
;;
3)
echo -en "\b\\"
;;
4)
echo -en "\b."
;;
esac
i=$((($i+1)%5))
j=$[$j+1]
sleep 0.2
if [ $WAIT_TIME -le $j ];then
echo ""
exit -1;
fi
done
2.使用方法
如果你想让这段shell跑起来的话,你要做如下工作
- 如果未使用maven请将pub.sh 中12~15行数据注释掉,并且也不需要waiting.sh了
- 将第六行的workspace_home及第八行的Jboss_home更换为自己的信息,同时第十行waiting_time是你使用maven打包成功的大致时间,请酌情更改
- 将上面两段代码分别拷贝至pub.sh及waiting.sh 同时chmod +x 并拷贝至usr/bin路径下。不过建议自己在主目录下建一个bin文件夹并将其路径配至.bashrc中的PATH
- 完成以上步骤后,在终端下输入pub yourProjectName就可以了
3.一般运行状态
![](http://hi.csdn.net/attachment/201111/30/0_13226408583vhu.gif)