ant实例

<?xml version="1.0" encoding="UTF-8"?>
<project name="Nightingale" default="" basedir=".">
	<target name="init">
		<echo message="初始化打 war包需要的路径变量"/>
		<!--获取系统时间-->
		<tstamp>
			<format property="sys.time" pattern="yyyy-MM-dd_HH-mm-ss"></format>
		</tstamp>
		<!--读取build.properies文件-->
		<property file="${basedir}/build.properties"/>
		<!--配置java编译环境 -->
		<path id="Nightingale.compile.classpath">
			<fileset dir="${weblib.dir}">
				<include name="**/*.jar" />
			</fileset>
			<fileset dir="${builder.basedir.j2ee}">
				<include name="**/*.jar" />
			</fileset>
		</path>
		<!--判断conf文件夹下面是否有文件-->
		<available file="${publish.conf}" property="keyfile.exist" />
	</target>
	
	<!--删除publish文件夹,并删除war包 -->
	<target name="clean" depends= "init">
		<echo message="清空源文件"/>
		<delete dir="${publish.dir}" />
		<delete><fileset dir="${builder.basedir}" includes="**/*.war"/></delete>
	</target>
	
	<!--创建publish文件夹 -->
	<target name="prepare" depends= "clean">
		<echo message="创建文件夹"/>
		<mkdir dir="${publish.dir}"/>
	</target>
	
	<!-- 拷贝功能目录下的webRoot文件到publish中,并编译java文件 -->
	<target name="build-copy" depends="prepare">
		<echo message="拷贝 webRoot文件到编译后的路径下"/>
		<copy todir="${publish.dir}">
			<fileset dir="${webapp.dir}"/>
		</copy>
		<echo message="编译 java 文件,拷贝 属性配置文件到编译后的路径下"/>
		<javac destdir="${publish.classes}">
			<src path="${java.src.dir}" />
			<classpath refid="Nightingale.compile.classpath" />
		</javac>
	</target>
	
	<!-- 拷贝conf目录下的文件到publish中-->
	<target name="conf.exist" if="${keyfile.exist}">
		<echo message="拷贝conf文件目录下的文件到工程conf目录下--${publish.conf}"/>
		<copy todir="${publish.web.conf}" overwrite = "true" verbose="true">
			<fileset dir="${publish.conf}"/>
		</copy>
	</target>
	<target name="conf.unexist" unless="${keyfile.exist}">
		<echo message="${publish.conf}文件夹下不存在文件"/>
	</target>
	
	<!--把publish中的文件打包成war文件 -->
	<target name="publish-war" depends="build-copy,conf.exist,conf.unexist">
		<echo message="打 war 包,不将 java 文件打入包内"/>
		<war warfile="${war.file}" webxml="${publish.web}">
			<fileset dir="${publish.dir}" />
		</war>
	</target>
	
	<!-- 把war包,拷贝到服务器上 -->
	<target name="publish-remote">
		<antcall target="publish-war" />
		<echo>发送文件 ${war.file} 到服务器上...</echo>
		<scp file="${war.file}" todir="${publish.remote.username}:${publish.remote.password}@${publish.remote.host}:${publish.remote.apphome}"  passphrase="" verbose="true" trust="true"/>
	</target>
</project>



java.src.dir = ${basedir}/src
webapp.dir = ${basedir}/WebRoot
weblib.dir = ${webapp.dir}/WEB-INF/lib/
builder.basedir = ${basedir}/build
builder.basedir.j2ee = ${builder.basedir}/lib/

<!-- Directory structure of the project -->
publish.dir = ${builder.basedir}/publish
publish.web = ${publish.dir}/WEB-INF/web.xml
publish.classes = ${publish.dir}/WEB-INF/classes
publish.web.conf = ${publish.classes}/conf
publish.conf = ${builder.basedir}/conf
war.file = ${builder.basedir}/Nightingale_${sys.time}.war

猜你喜欢

转载自zhihchen.iteye.com/blog/1966207
ANT