同样是因为代码太久远,部分代码可以用maven构建,部分需要ant构建,而ant构建需要配置build文件,而开发又因为要写业务代码,没办法抽出时间来写build文件,而我自己在尝试将eclips导出来的build文件直接用ant编译后没办法成功,所以这里还是只能用jenkins发布开发已经打好的rar包,具体流程如下: 将jenkins工目录配置成samba服务的共享目录,配置samba服务器这边就不再详解了; 2、让开发使用自己的ide将代码导出rar包,然后发给测试或者自己上传到jenkins工作目录; 3、使用参数化构建分别将tomcat目录、项目名称、远程主机地址等配置成变量: 4、在jenkins里面编写shell脚本将rar包发布到远程主机,具体脚本如下:

				NUM=`ssh $host ps -wauxw|grep $tomcat_name|grep -v grep|awk '{print $2}'|wc -l`
path=/opt/backup/${BUILD_NUMBER} #创建每次要备份的目录

update(){
    unrar x -y $project.rar
	sys_port=0
	newhgc_port=0
	newhgcweb_port=0
    db_host=0
    db_user=0
    db_pass=0
    if [ $host == "10.1.1.81"  -o  $host == "10.1.6.93" ];then
         db_host='10.1.6.82'
         db_user=zhcg
         db_pass="12345"
         sys_port=9083
         supplier_port=7180
         club_port=9085
         cas_port=8443
    elif [ $host == "10.1.1.230" ];then
         db_host=10.1.1.230
         db_user=pgsql
         db_pass=hczd365
         sys_port=19083
         supplier_port=17180
         club_port=19085
         cas_port=18443
    fi
    sed -i "s#jdbc.url=.*#jdbc.url=jdbc:postgresql://$db_host:5432/zhcg_db#g" $project/WEB-INF/classes/db.properties
    sed -i "s#jdbc.username=.*#jdbc.username=$db_user#g" $project/WEB-INF/classes/db.properties
    sed -i "s#jdbc.password=.*#jdbc.password=$db_pass#g" $project/WEB-INF/classes/db.properties
    sed -i "s#dubbo.*##g" $project/WEB-INF/classes/db.properties
    sed -i "s#cas.service.url=.*#cas.service.url=http://devtest.example.com:$supplier_port/hczd-hgc-supplier/sys/login.do#g" $project/WEB-INF/classes/base.properties
    sed -i "s#hgc.index=.*#hgc.index=http://devtest.example.com:$club_port/#g" $project/WEB-INF/classes/base.properties
    sed -i "s#cas.service.prefix=.*#cas.service.prefix=https://devtest.example.com:$cas_port#g" $project/WEB-INF/classes/base.properties
    sed -i "s#hczd.sys=.*#hczd.sys=http://$host:$sys_port/hczd-sys/#g" $project/WEB-INF/classes/base.properties
    sed -i "s#hgc.login=.*#hgc.login=http\://devtest.example.com\:$club_port/client/authority/login.htm?returnUrl\=http\://devtest.example.com\:9085/mall#g" $project/WEB-INF/classes/base.properties
    sed -i "s#zookeeper://\${dubbo.registry}:2191#zookeeper://127.0.0.1:2181#g" $project/WEB-INF/classes/spring/spring-context-dubbo-provider.xml
    sed -i "s#host="\${dubbo.host}"##g" $project/WEB-INF/classes/spring/spring-context-dubbo-provider.xml
    sed -i "s#port="\${dubbo.port}"#port=\"20886\"#g" $project/WEB-INF/classes/spring/spring-context-dubbo-provider.xml
    rar a $project.rar $project
    scp $project.rar $host:/home/$tomcat_name/webapps/
    ssh $host "[ $NUM -gt 0 ] && ps -wauxw|grep $tomcat_name|grep -v grep|awk '{print \$2}'|xargs kill -9"||echo "PID is not exsit!!"
    ssh $host "cd /home/$tomcat_name/webapps/;unrar x -y $project.rar;rm -rf $project.rar"
    ssh $host sh /home/$tomcat_name/bin/startup.sh
  }

case $Status  in
  Deploy)
    echo "Status:$Status"           
    ssh $host "[ -d $path ] && echo "The files is already  exists " ||mkdir -p  $path"
    ssh $host cp -rf /home/$tomcat_name/webapps/$project $path/$project   #将打包好的rar包备份到相应目录,覆盖已存在的目标
    update
    ;;
  Rollback)
      echo "Status:$Status"
      echo "Version:$Version"
      ssh $host rm -rf /home/$tomcat_name/webapps/$project
      ssh $host "cp -rf /opt/backup/$Version/$project /home/$tomcat_name/webapps/"
      ssh $host "[ $NUM -gt 0 ] && ps -wauxw|grep $tomcat_name|grep -v grep|awk '{print \$2}'|xargs kill -9"||echo "PID is not exsit!!"
      ssh $host sh /home/$tomcat_name/bin/startup.sh
      ;;
  *)
  exit
      ;;
esac