使用ANT腳本編譯、打包、部署到tomcat,啓動、停止tomcat

<?xml version="1.0" encoding="UTF-8"?>
<project name="lms" default="deploy" basedir="D:/workspace/lms">
	<property environment="env" />
	<property name="webapp.name" value="lms" />
	<property name="tomcat.home" value="D:\developer\apache-tomcat-6.0.32" />
	<property name="dist.dir" value="${basedir}/dist" />
	<property name="ant.dir" value="D:/developer/apache-ant-1.8.3" />
	<property name="webRoot.dir" value="${basedir}/webapp" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="config.dir" value="${basedir}/resources" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
	<property name="build.dir" value="${basedir}/build" />
	<property name="log.file" value="${tomcat.home}/webapps/log.info"/>
	
	<!-- 使用eclipse jdt進行編譯,而不使用JDK編譯  -->
	<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />

	<tstamp>
		<format property="build.time" pattern="yyyy-MM-dd HH:mm:ss" />
	</tstamp>
	
	<echo>${build.time}</echo>
	
	<!-- 初始化classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<!-- 添加tomcat類路徑 -->
		<fileset dir="${tomcat.home}/lib">
			<include name="*.jar" />
		</fileset>
		<!-- ant lib包  -->
		<fileset dir="${ant.dir}">
			<include name="**/*.jar" />
		</fileset>
		<pathelement location="${build.dir}/classes"/>
	</path>

	<!-- ************************** 下面3個方法都是顯示classpath ******************************************* -->
	<!-- 這個運行失敗。抽空研究研究怎麼回事!!
	<macrodef name="echo-path">
		<attribute name="pathref" />
		<sequential>
			<echo>echoing path=@{pathref}</echo>
			<for param="fromfile">
				<path refid="@{pathref}"  />
				<sequential>
					<echo>@{fromfile}</echo>
				</sequential>
			</for>
		</sequential>
	</macrodef>
	<target name="print_classpath1">
		<echo-path pathref="project.classpath" />
	</target>
	-->
	
	<target name="print_classpath2">
		<property name="myclasspath" refid="project.classpath"/>
		<!-- 顯示所有properties值
		 <echoproperties></echoproperties> -->
		<echo message="Classpath = ${myclasspath}"/>
	</target>
	
	<!-- get the source compile classpath in a printable form -->
	<pathconvert pathsep="${line.separator}|   |-- "
             property="echo.path.compile"
             refid="project.classpath">
	</pathconvert>
	<target name="print_classpath3">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>
	</target>
	<!-- **************************OVER:顯示classpath ******************************************* -->		
	
	<!-- 刪除之前的目錄結構 -->
	<target name="clear" description="清理舊文件">
		<echo>刪除之前的目錄結構</echo>
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${tomcat.home}/webapps/${webapp.name}.war" />
		<delete dir="${tomcat.home}/webapps/${webapp.name}" />
	</target>

	<!-- 創建目錄結構 -->
	<target name="init"  description="創建初始化目錄結構">
		<echo>創建初始化目錄結構</echo>
		<mkdir dir="${build.dir}/classes" />
		<mkdir dir="${dist.dir}" />
		<touch file="${log.file}" />
	</target>

	<!-- 編譯java -->
	<target name="compile" depends="init" description="編譯java文件">
		<echo>編譯java文件</echo>
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
			includeantruntime="false" nowarn="on" 
			source="1.6" target="1.6" deprecation="true" debug="true" 
			encoding="UTF-8" classpathref="project.classpath" 
			>
			<compilerarg line="-Xlint:unchecked" />
			<!-- <classpath refid="project.classpath" /> -->
		</javac>
		<copy todir="${build.dir}" overwrite="true">
			<fileset dir="${src.dir}" excludes="**/*.java">
				<!--
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
				-->
			</fileset>
			<fileset dir="${config.dir}" />
		</copy>
	</target>

	<!-- 將class文件打成 jar包 -->
	<!--  
	    <target name="pack" depends="compile"> 
	        <jar jarfile="${build.dir}/${webapp.name}.jar"> 
	            <fileset dir="${build.dir}/classes"> 
	                <include name="**/*.class"/> 
	            </fileset> 
	        </jar> 
	    </target> 
	-->

	<!-- 打成war包, 名稱默認爲 項目名 -->
	<target name="war" depends="compile" description="將工程打成war包">
		<echo>將工程打成war包</echo>
		<war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}" 
			webxml="${webRoot.dir}/WEB-INF/web.xml">
			<lib dir="${lib.dir}" />
			<classes dir="${build.dir}/classes" />
			<fileset dir="${webRoot.dir}">
				<include name="***.*" />
			</fileset>
		</war>
	</target>

	<!-- copy war包 tomcat的deploy目錄 -->
	<target name="deploy" depends="war" description="部署項目">
		<echo>${build.time}部署項目</echo>
		<delete file="${tomcat.home}/webapps/${webapp.name}.war" />
		<delete dir="${tomcat.home}/webapps/${webapp.name}" />
		<copy file="${dist.dir}/${webapp.name}.war" todir="${tomcat.home}/webapps" />
	</target>
	
	<!--********************** 啓停tomcat的兩種方法  ***********************************-->
	<!-- 在命令行界面顯示tomcat控制檯 -->
	<target name="stop_tomcat">
		<echo>停止tomcat</echo>
		<exec executable="cmd" dir="${tomcat.home}/bin" failοnerrοr="false" 
					output="${log.file}" append="true" >
			<!-- <arg value="/c" /> -->   
			<env key="CATALINA_HOME" path="${tomcat.home}"/>
			<arg value="/c shutdown.bat" />   
		</exec>
	</target>

	<target name="start_tomcat">
		<echo>啓動tomcat</echo>
		<exec executable="cmd" dir="${tomcat.home}/bin"  failοnerrοr="false" 
					output="${log.file}" append="true" >
			 <!-- <arg value="/c" /> -->  
			 <env key="CATALINA_HOME" path="${tomcat.home}"/>
			 <arg value="/c startup.bat" />   
		</exec>
	</target>
	
	<!-- 在eclipse控制檯即Console裏顯示tomcat控制檯 -->
	<target name="tomcat.start">
		<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" >
			<jvmarg value="-Dcatalina.home=${tomcat.home}" />
		</java>
	</target>

	<target name="tomcat.stop">
		<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
			<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
			<jvmarg value="encoding UTF-8"></jvmarg>
			<arg line="stop"/>
		</java>
	</target>

	<target name="tomcat.debug">
		<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
			<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
			<jvmarg value="-Xdebug"/>
			<jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/>
		</java>
	</target>
	<!--********************** OVER:啓停tomcat  ***********************************-->
</project>


 

這個是稍微有點變化的build.xml腳本。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章