build.xml 2.73 KB
<?xml version="1.0" encoding="UTF-8" ?>
<project name="main" default="build" basedir=".">
	<taskdef resource="net/sf/antcontrib/antlib.xml" />
	<taskdef resource="net/sf/antcontrib/antcontrib.properties" />

	<property file="build.properties" />
	<property name="servers" value="192.168.102.12;192.168.102.13" />
	<property name="user" value="ginozhang" />
	<property name="pwd" value="123456" />
	<property name="war_name" value="yoho-kisjob-web" />

	<property name="tomcat_location" value="/home/ginozhang/apache-tomcat-8.0.36" />

	<target name="deployAndStart" description="初始化处理 停tomcat 删除环境上的war包">
		<for list="${servers}" delimiter=";" param="server">
			<sequential>
				<antcall target="deployAndStartSingle">
					<param name="server_ip" value="@{server}" />
				</antcall>
			</sequential>
		</for>
	</target>

	<target name="deployAndStartSingle" description="初始化处理 停tomcat 删除环境上的war包">
		<property name="server_ip" value="" />

		<antcall target="deploySingle">
			<param name="server_ip" value="${server_ip}" />
		</antcall>

		<echo message="start tomcat" />
		<sshexec host="${server_ip}" username="${user}" password="${pwd}" trust="true" failonerror="false" command="cd ${tomcat_location}/bin; ./startup.sh" />
	</target>

	<target name="deploy" description="将包部署到远程服务器 但并不启动">
		<for list="${servers}" delimiter=";" param="server">
			<sequential>
				<antcall target="deploySingle">
					<param name="server_ip" value="@{server}" />
				</antcall>
			</sequential>
		</for>
	</target>

	<target name="deploySingle" description="将包部署到单个远程服务器 ">
		<property name="server_ip" value="" />

		<antcall target="stopSingle">
			<param name="server_ip" value="${server_ip}" />
		</antcall>

		<echo message="upload war package" />
		<scp file="war/${war_name}.war" todir="${user}@${server_ip}:${tomcat_location}/webapps" password="${pwd}" trust="true" />
	</target>

	<target name="stop" description="停止远程机器上的tomcat">
		<for list="${servers}" delimiter=";" param="server">
			<sequential>
				<antcall target="stopSingle">
					<param name="server_ip" value="@{server}" />
				</antcall>
			</sequential>
		</for>
	</target>

	<target name="stopSingle" description="停止远程机器的tomcat 并删除war包和日志">
		<property name="server_ip" value="" />

		<echo message="clear server ${server_ip}" />
		<echo message="stop tomcat" />
		<sshexec host="${server_ip}" username="${user}" password="${pwd}" trust="true" failonerror="false" command="cd ${tomcat_location};./bin/shutdown.sh;rm -rf ${tomcat_location}/webapps/${war_name}*;rm -rf ${tomcat_location}/logs/2016-*" />
	</target>


	<target name="build" depends="deployAndStart">
	</target>
</project>