通过ant实现Java项目编译和部署

build.xml内容如下:<?xml version="1.0" encoding="GBK"?>
<project name="test" default="start-tomcat" basedir=".">
	<!--初始化 参数设置[项目初始化信息]-->
	<property name="project.name" value="test"/>
	<property name="src.dir" value="${project.name}\\src"/>
	<property name="WebContent" value="${project.name}\\WebContent"/>
	<property name="project.name.lib" value="${WebContent}\\WEB-INF\\lib"/>
	<property name="project.encoding" value="GBK"/>

	<!--初始化 编译之后的临时存放目录-->
	<property name="class.dir" value="dist\\classes"/>
	<property name="dist.dir" value="dist\\wars"/>
	<property name="war.name" value="${project.name}.war"/>
	
	<!--初始化 tomcat信息-->
	<property name="tomcat.root" value="D:\\ant\\test\\tomcat6"/>
	<property name="tomcat.webapps" value="${tomcat.root}\\webapps"/>
	<property name="tomcat.lib" value="${tomcat.root}\\lib"/>

	<!--svn相关信息
	<property name="svn.path" value=""/>-->

	<!--定义classpath-->
	<path id="project.classpath">
		<!--项目lib下所有jar-->
		<fileset dir="${project.name.lib}">
			<include name="**/*.jar"/>
		</fileset>
		<!--tomcat lib下所有jar-->
		<fileset dir="${tomcat.lib}">
			<include name="**/*.jar"/>
		</fileset>
	</path>

	<!--删除之前编译的项目-->
	<target name="clean" description="清除${class.dir}下临时文件和${tomcat.webapps}\\${war.name}">
		<echo message="清除${class.dir}下临时文件和${tomcat.webapps}\\${war.name}"/>
		<delete file="${tomcat.webapps}\\${war.name}"/>
		<delete dir="${class.dir}"/>
		<delete dir="${WebContent}\\WEB-INF\\classes"/>
	</target>

	<!--重新编译项目 依赖于clean-->
	<target name="compile" depends="clean" description="">
		<echo message="编译源文件开始..."/>
		<mkdir dir="${class.dir}"/>
		<echo message="开始复制文件到${class.dir}..."/>
		<copy includeemptydirs="false" todir="${class.dir}" overwrite="true" description="开始复制文件到${class.dir}">
			<!--注意src下有多个目录-->
			<fileset dir="${src.dir}">
				<exclude name="**/*.launch"/>
				<exclude name="**/*.java"/>
			</fileset>
		</copy>
		<javac destdir="${class.dir}" debug="false" verbose="false" encoding="${project.encoding}" includeantruntime="on" deprecation="false" nowarn="true">
			<classpath refid="project.classpath"/>
			<src path="${src.dir}"/>
		</javac>
	</target>

	<!--把项目打包成war文件-->
	<target name="war" depends="compile" description="正在打包项目...">
		<echo message="正在打包项目..."/>
		<copy todir="${WebContent}\\WEB-INF\\classes">
			<fileset dir="${class.dir}"/>
		</copy>
		<mkdir dir="${dist.dir}"/>
		<jar destfile="${dist.dir}\\${war.name}">
			<fileset dir="${WebContent}">
				<include name="**/*" />
			</fileset>
		</jar>
	</target>

	<!-- 停止tomcat服务 -->
	<target name="tomcat.stop">
		<echo message="正在停止tomcate..."/>
		<java jar="${tomcat.root}/bin/bootstrap.jar" fork="true">  
			  <jvmarg value="-Dcatalina.home=tomcat6"/>  
			 		<arg line="stop"/>  
			 </java>  
		 <waitfor maxwait="5" maxwaitunit="second">  
		       <available file="errors.log"/>  
		 </waitfor>  
	</target>

	<!--将打包*.war文件copy到tomcat-->
	<target name="all"  depends="war,tomcat.stop" description="正在复制${warname}到${tomcat.deploy.dir}...">
		<echo message="正在复制${war.name}到${tomcat.webapps}..."/>
		<delete dir="${tomcat.webapps}/${war.name}"/>		
		<copy file="${dist.dir}/${war.name}" todir="${tomcat.webapps}" overwrite="true"/> 
    </target>

	<!--启动tomcat-->
	<target name="start-tomcat" description="正在启动tomcat" depends="all,tomcat.stop">
		<echo message="正在启动tomcat..."/>
	     <exec executable="${tomcat.root}/bin/startup.bat" spawn="true" vmlauncher="false">  
		     <env key="CATALINA_HOME" value="tomcat6" />  
		      <arg line="/c start tomcat.6/bin/startup.bat" />  
	    </exec>          
	 </target>
</project>

你可能感兴趣的:(ant,部署,编译)