bash
脚本:dep
web.sh
运行环境:linux/UNIX操作
系统,
CVS配置
管理库,ant
工具,JDK
局限性:只对java
代码进行构建
实现
功能:取出全量版本标签(代码
基线标签)代码,进行构建,封装打包(tar ball)
把包发布给部署人员进行部署,部署完毕通知测试人员可以对此版本产品进行测试。
编写此脚本的目的:简化基于java代码
项目的编译,完成一个通用java代码的构建脚本,通过对配置
文件的
设置来达到对多个
项目进行构建。
运行前可以通过执行
depweb.sh -h 来查看脚本的帮助(简明使用方法)
运行前需要设置配置文件,可以通过执行
depweb.sh -i
project
生成project-build.properties配置文件,配置文件内容填写实例如下:
复制内容到剪贴板
代码:
# Must set the "CVSROOT" environment variable
CVSROOT=:pserver:user:passwd@cvs-server:/cvs/repository/root
# Must set the module name of project in CVS Repository
MODULE_NAME=projectsample
# Must set the "$module" source code path relative to \$CVSROOT
SRC_PATH=projectsample/codepath/
# Must set the java files path relative to $SRC_HOME
javadir=src"
# Must set the class files path relative to $DST_HOME
classdir=WEB-INF/classes
# Must set the jars path relative to \$SRC_PATH
srclib=WEB-INF/lib
# The absolute common lib path
svrlib=/userhome/soft/tomcat/common/lib
以下内容为脚本内容,此内容包含整个构建打包过程以及一个build.xml的模板
####################################################
#prog:depweb.sh #
#func:Web Application build create and tar package #
#vern:1.6 #
#auth:liurs #
#comp:FORLINK #
####################################################
#!/bin/sh
usage()
{
clear
echo "USAGE: $PROG [-a|--add] [-h|--help] [-i|--initial] [-c|--complete]"
echo
echo "WHERE: -h --help = Display help infomation"
echo "Usage: $PROG -h|--help"
echo
echo " -i --initial = 设置配置文件,初始化构建环境"
echo "Usage: $PROG -i <模块名>"
echo
echo " -a --add = 直接增量部署,用如下格式:"
echo "Usage: $PROG -a <增量标签> <模块名>"
echo
echo " -c --complete = 初始全量部署,用如下格式:"
echo "Usage: $PROG -c <全量标签> <模块名>"
echo
echo "正常全量部署,用如下格式:"
echo "Usage: $PROG <全量标签> <模块名>"
echo
return 0
}
setsample()
{
clear
echo "PREREQUISITES:"
echo
echo "* The $HOME/build/\$MODULE-build.properties must be set"
echo "* such as: "
echo
echo "# Must set the "CVSROOT" environment variable"
echo "CVSROOT=:pserver:user:passwd@cvs-server:/cvs/repository/root"
echo
echo "# Must set the module name of project in CVS Repository"
echo "MODULE_NAME=projectsample"
echo
echo "# Must set the "$module" source code path relative to \$CVSROOT"
echo "SRC_PATH=projectsample/codepath/"
echo
echo "# Must set the java files path relative to $SRC_HOME"
echo "javadir=src"
echo
echo "# Must set the class files path relative to $DST_HOME"
echo "classdir=WEB-INF/classes"
echo
echo "# Must set the jars path relative to \$SRC_PATH"
echo "srclib=WEB-INF/lib"
echo
echo "# The absolute common lib path"
echo "svrlib=/userhome/soft/tomcat/common/lib"
echo
return 0
}
echoxml()
{
echo '<?xml version="1.0" encoding="GB2312"?>
<project name="proname" default="all" basedir=".">
<property environment="sysenv"/>
<property file="${sysenv.MODULE}-build.properties"/>
<property name="proname" value="${sysenv.MODULE}"/>
<property name="srcdir" value="${sysenv.SRC_HOME}"/>
<property name="dstdir" value="${sysenv.DST_HOME}"/>
<property name="depdir" value="${sysenv.DEP_HOME}"/>
<property name="wwwdir" value="${sysenv.WWW_HOME}"/>
<property name="date" value="${sysenv.DATE}"/>
<property name="cvshome" value="${sysenv.CVSROOT}"/>
<property name="tagname" value="${sysenv.CVSTAG}"/>
<property name="buidir" value="${srcdir}"/>
<property name="buidir.java" value="${buidir}/${javadir}"/>
<property name="dstdir.java" value="${dstdir}/${javadir}"/>
<property name="appdir" value="${dstdir}/${classdir}"/>
<property name="comlib" value="${svrlib}"/>
<property name="devlib" value="${sysenv.srclib}"/>
<path id="compile.classpath">
<fileset dir="${devlib}">
<include name="*.jar"/>
</fileset>
<fileset dir="${comlib}">
<include name="*.jar"/>
</fileset>
<pathelement path="${wwwdir}/${classdir}"/>
</path>
<target name="all" depends="clean,init,copy,compile,deploy"/>
<target name="init" depends="">
<mkdir dir="${dstdir}"/>
<mkdir dir="${depdir}"/>
<mkdir dir="${appdir}"/>
</target>
<target name="compile" depends="init" description="Compile main source tree java files">
<javac srcdir="${buidir.java}" destdir="${appdir}" debug="true" deprecation="false" optimize="false" failonerror="true" encoding="gb2312">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="clean" depends="clndst,clndep" description="Clean output directories"/>
<target name="clnclass" depends="" description="Clean out class files">
<delete>
<fileset dir="${srcdir}">
<include name="**/*.class"/>
</fileset>
</delete>
</target>
<target name="clnjava" depends="" description="Clean out source code file">
<delete>
<fileset dir="${dstdir}/${javadir}">
<include name="**/*.java"/>
</fileset>
</delete>
</target>
<target name="clndst" depends="" description="Clean out dest directorie">
<delete dir="${dstdir}"/>
</target>
<target name="clndep" depends="" description="Clean out deploy directories">
<delete>
<fileset dir="${depdir}">
<include name="*.tar"/>
</fileset>
</delete>
</target>
<target name="copy" depends="init" description="Copy web files">
<copydir src="${buidir}" dest="${dstdir}"/>
<copy todir="${appdir}">
<fileset dir="${buidir.java}">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>
</copy>
<!--delete dir="${dstdir.java}"/-->
</target>
<target name="deploy" depends="init" description="Create web tar file">
<tar destfile="${depdir}/${proname}-${tagname}.tar" basedir="${dstdir}"/>
</target>
<target name="cvsexp" depends="" description="cvs export scode from CVS lib to srcdir">
<mkdir dir="${srcdir}"/>
<!--<cvspass cvsroot="${svshome}"/>-->
<cvs command="export" tag="${tagname}" failOnError="true" cvsroot="${cvshome}" package="web/impl" dest="${srcdir}"/>
</target>
</project>'
return 0
}
echocfg()
{
echo '#The config file of depweb.sh and build.xml
# Must set the value of "CVSROOT" environment variable
CVSROOT=
# Must set the module name of project in CVS Repository
MODULE_NAME=
# Must set the "$module" source code path relative to \$CVSROOT
SRC_PATH=
# Must set the java files path relative to \$SRC_HOME
javadir=
# Must set the class files path relative to \$DST_HOME
classdir=
# Must set the jars path relative to \$SRC_PATH
srclib=
# Set the absolute common lib path
svrlib=
'
return 0
}
cmpdir()
{
echo '
#!/bin/sh
cmp_dir()
{
RET=0
cd $HOME_DIR
DIFF_NAME=$HOME_DIR/diff.err
EXIST_NAME=$HOME_DIR/$3.exist.err
>$EXIST_NAME
>$DIFF_NAME
if [ ! -d $1 ]
then
echo directory $1 does not exit!
exit 1
fi
if [ ! -d $2 ]
then
echo directory $2 does not exit!
exit 2
fi
cd $1
for i in `find . -type f ! -path "*\ *"`
do
if [ -f $2/$i ]
then
cmp $1/$i $2/$i >/dev/null 2>/dev/null
if [ $? -ne 0 ]
then
#echo $1/$i
RET=1
echo $i >>$DIFF_NAME
fi
else
#echo $HOME_DIR/$2/$i >>$EXIST_NAME
#echo $i
RET=1
echo $i >>$EXIST_NAME
fi
done
cd $HOME_DIR
return $RET
}
HOME_DIR=`pwd`
if [ $# -ne 2 ]
then
echo "Usage: `basename $0` 目录1 目录2"
exit -1
fi
DIR_1=$1
DIR_2=$2
if [ `expr substr $1 1 1` != "/" ]
then
DIR_1="$HOME_DIR""/""$1"
fi
if [ `expr substr $2 1 1` != "/" ]
then
DIR_2="$HOME_DIR""/""$2"
fi
#echo 第一个目录
cmp_dir $DIR_1 $DIR_2 1
RET1=$?
#echo 第二个目录
cmp_dir $DIR_2 $DIR_1 2
RET2=$?
RET=0
cd $HOME_DIR
if [ $RET1 -ne 0 -o $RET2 -ne 0 ]
then
echo "不同"
ls -l 1.exist.err 2.exist.err diff.err
RET=1
fi
exit $RET'
return 0
}
#check environment
if [ -z $JAVA_HOME ]
then
echo "Environment is wrong,please check the environment!"
exit 1
fi
if [ -z $CVSROOT ]
then
echo "The CVSROOT environment variable isn't setted"
#exit 1
fi
if [ ! `which cvs` ]
then
echo "The cvs command is not setted in \$PATH or cvs isn't installed!"
echo "Please check the environment or installed"
exit 1
fi
if [ ! `which ant` ]
then
echo "The ant isn't installed"
echo "Please install the ant"
exit 1
fi
if [ ! `which cmp_dir` ]
then
if [ ! -d $HOME/bin ]
then
mkdir -p $HOME/bin
fi
cmpdir >$HOME/bin/cmp_dir
chmod +x $HOME/bin/cmp_dir
export PATH=$HOME/bin:.:$PATH
fi
PROG=`basename $0`
ARGV="$@"
ARGC="$#"
export DATE=`date +%Y%m%d`
if [ `uname` = Linux ]
then
export LANG=zh_CN.GB2312
elif [ `uname` = HP-UX ]
then
export LANG=zh_CN.hp15CN
else
LANG=`echo $LANG`
fi
if [ $ARGC -lt 1 -o $ARGC -gt 3 ]
then
usage;exit 1;
fi
if [ `uname` = Linux ]
then
OPTS=`getopt -n 'depweb.sh' -o haic -l help,add,initial,complete -- "$@"`
else
OPTS=`getopt haic: $*`
fi
if [ $? -ne 0 ]
then
echo "Please use $PROG -h"
exit 1
fi
#eval set -- "$OPTS"
for args in $OPTS
do
case "$args" in
-h|--help)
if [ $ARGC -ne 1 ]
then
usage "HELP";exit 1; # Help requested
fi
usage;exit 0;;
-a|--add)
if [ $ARGC -ne 3 ]
then
usage;exit 1;
fi
export CVSTAG=$2
export MODULE=$3
BUD_CFG="$HOME/build/"$MODULE"-build.properties"
if [ ! -f "$BUD_CFG" ]
then
echo "Please initial the $MODULE"
echo "Please use: "depweb.sh -i <$MODULE>""
exit 1
fi
FLAG="add";
break;;
#shift;; #add files deploy
-i|--initial)
if [ $ARGC -ne 2 ]
then
usage;exit 1;
fi
if [ ! `which vi` ]
then
echo "Not install vim"
exit 1
fi
export MODULE="$2"
BUD_CFG="$HOME/build/"$MODULE"-build.properties"
if [ ! -f "$BUD_CFG" ]
then
if [ ! -d $HOME/build ]
then
mkdir -p $HOME/build
fi
echocfg>"$BUD_CFG"
fi
setsample;
echo "********************************************"
echo "Please set the properties file"
sleep 3
vi "$BUD_CFG"
exit 0 ;;
#break;;
#shift;; #increase files deploy
-c|--complete)
if [ $ARGC -ne 3 ]
then
usage;exit 1;
fi
export CVSTAG=$2
export MODULE=$3
BUD_CFG="$HOME/build/"$MODULE"-build.properties"
if [ ! -f "$BUD_CFG" ]
then
echo "Please initial the $MODULE"
echo "Please use: "depweb.sh -i <$MODULE>""
exit 1
fi
FLAG="complete";
break;;
#shift;; # complete files deploy
--)
if [ $ARGC -ne 2 ]
then
usage
exit 1
fi
export CVSTAG=$1
export MODULE=$2
FLAG="cmpold"
break;;
#shift; break;;
*)
usage; exit 1 ;;
esac
done
#init part
rm -rf $HOME/build/src
if [ ! -d $HOME/bin/tmpdir ]
then
mkdir -p $HOME/bin/tmpdir
fi
if [ ! -d $HOME/build ]
then
mkdir -p $HOME/build
fi
if [ ! -d $HOME/build/src ]
then
mkdir -p $HOME/build/src
fi
if [ ! -d $HOME/build/dest ]
then
mkdir -p $HOME/build/dest
fi
if [ ! -d $HOME/build/deploy ]
then
mkdir -p $HOME/build/deploy
fi
if [ ! -d $HOME/deploy ]
then
mkdir -p $HOME/deploy
fi
export SRC_HOME=$HOME/build/src
export DST_HOME=$HOME/build/dest
export DEP_HOME=$HOME/build/deploy
export WWW_HOME=$HOME/build/deploy/$MODULE
BUD_XML="$HOME/build/build.xml"
if [ ! -f "$BUD_XML" ]
then
touch $BUD_XML
echoxml >$BUD_XML
fi
BUD_CFG="$HOME/build/"$MODULE"-build.properties"
if [ ! -f "$BUD_CFG" ]
then
echo "The $MODULE has not been initialed!"
echo "Please use: "depweb.sh -i \<$MODULE\>" to initial the \<$MODULE\> module"
exit 1
fi
export CVSROOT=`grep "CVSROOT=" "$BUD_CFG"|awk -F = '{print $2}'`
SRCPATH=`grep "SRC_PATH=" "$BUD_CFG"|awk -F = '{print $2}'`
if [ ! "$SRCPATH" ]
then
echo "Not appoint the path of source code in the $DEP_CFG"
setsample
exit 1
fi
CONFLST=`cat $HOME/build/config.lst`
DEPFILE=""$MODULE"-"$CVSTAG".tar"
JAVADIR=`grep -w javadir "$BUD_CFG"|awk -F = '{print $2}'`
SRCLIB=`grep -w srclib "$BUD_CFG"|awk -F = '{print $2}'`
#========================build part========================#
echo "cvs login"
cvs login
echo "cd $HOME/build"
cd $HOME/build
echo "cvs -q export -r "$CVSTAG" -d src "$SRCPATH""
cvs -q export -r "$CVSTAG" -d src "$SRCPATH"
if [ $? -ne 0 ]
then
echo "cvs export scode failed!"
exit 1
fi
ant clean
if [ $? -ne 0 ]
then
echo "ant clean target have error!"
exit 1
fi
ant clnclass
ant copy
if [ $? -ne 0 ]
then
echo "ant copy target have error!"
exit 1
fi
ant clnjava
if [ $FLAG = "cmpold" ]
then
export srclib=$SRC_HOME/$SRCLIB
TARNAME="$MODULE"-"$CVSTAG"-add.tar
if [ -d $SRC_HOME/$JAVADIR ]
then
ant compile
if [ $? -ne 0 ]
then
echo "ant compile target have error"
exit 1
fi
fi
cd $DST_HOME
for i in $CONFLST
do
find . -type f -name "$i" -exec rm -f {} \;
done
tar cf "$DEPFILE" *
mv "$DEPFILE" "$DEP_HOME"
#========================package part========================#
cd $DEP_HOME
rm -rf "$MODULE"-dep
mkdir -p "$MODULE"-dep
cp "$DEPFILE" $DEP_HOME/"$MODULE"-dep
cd "$MODULE"-dep
tar xf "$DEPFILE"
rm -f "$DEPFILE"
cd $DEP_HOME
cmp_dir "$MODULE" "$MODULE"-dep
if [ $? -ne 1 ]
then
echo "#########################################"
echo "This version tag is the same to last tag!"
echo "Please check whether this tag is the same to the last one!"
exit 1
fi
cd "$MODULE"-dep
tar cf "$TARNAME" `cat ../2.exist.err`
if [ -s $TARNAME ]
then
tar uf "$TARNAME" `cat ../diff.err`
else
tar cf "$TARNAME" `cat ../diff.err`
if [ $? -ne 0 ]
then
echo "$TARNAME is not created !"
exit 1
fi
fi
rm -rf $HOME/deploy/*
mv "$TARNAME" $HOME/deploy
cd $WWW_HOME
cp $HOME/deploy/"$TARNAME" $WWW_HOME
tar xf "$TARNAME"
rm -f "$TARNAME"
echo "The add tar is created successfully!"
ls -ltr $HOME/deploy/"$TARNAME"
elif [ $FLAG = "complete" ]
then
export srclib=$SRC_HOME/$SRCLIB
TARNAME="$MODULE"-"$CVSTAG".tar
if [ -d $SRC_HOME/$JAVADIR ]
then
ant compile
if [ $? -ne 0 ]
then
echo "ant compile target have error"
exit 1
fi
fi
cd $DST_HOME
tar cf "$TARNAME" *
mv "$TARNAME" "$DEP_HOME"
rm -rf $HOME/deploy/*
cp $DEP_HOME/"$TARNAME" $HOME/deploy
cd $HOME/build/deploy
rm -rf $MODULE
mkdir -p $MODULE
mv "$TARNAME" $MODULE
cd $MODULE
tar xf "$TARNAME"
rm -f "$TARNAME"
echo "The whole tar is created successfully!"
ls -ltr $HOME/deploy/"$TARNAME"
elif [ $FLAG = "add" ]
then
export srclib=$WWW_HOME/$SRCLIB
TARNAME="$MODULE"-"$CVSTAG"-add.tar
if [ -d $SRC_HOME/$JAVADIR ]
then
ant compile
if [ $? -ne 0 ]
then
echo "ant compile target have error"
exit 1
fi
fi
cd $DST_HOME
tar cf "$TARNAME" *
mv "$TARNAME" "$DEP_HOME"
cp "$DEP_HOME"/"$TARNAME" "$DEP_HOME"/"$MODULE"
rm -rf $HOME/deploy/*
cp "$DEP_HOME"/"$TARNAME" $HOME/deploy
cd "$DEP_HOME"/"$MODULE"
tar xf "$TARNAME"
rm -f "$DEP_HOME"/"$MODULE"/"$TARNAME"
echo "The add tar is created successfully!"
ls -ltr $HOME/deploy/"$TARNAME"
else
usage;exit 1;
fi