DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ant 脚本

07.24.2008
| 2060 views |
  • submit to reddit
        ant 构架部署web应用脚本

build.properties:
=====================================
appserver.home=d:/tomcat
deploy.path=${appserver.home}/webapps

tomcat.manager.url=http://localhost:8080/manager

tomcat.manager.username=spring
tomcat.manager.password=spring
=====================================

<?xml version="1.0" encoding="gb2312"?>

<project name="springapp" basedir="." default="usage">

	<property file="build.properties"/>	
	<property name="src.dir" value="src"/>
	<property name="extlib.dir" value="lib"/>	
	<property name="web.dir" value="war"/>
	<property name="config.dir" value="config"/>	
	<property name="class.dir" value="${web.dir}/WEB-INF/classes"/>
	<property name="lib.dir" value="${web.dir}/WEB-INF/lib"/>	
	<property name="name" value="springapp"/>

	<path id="master-classpath">
		<fileset dir="${lib.dir}">
			<include name="*.jar"/>
		</fileset>

		<fileset dir="${appserver.name}/common/lib">
			<include name="servlet*.jar"/>
		</fileset>

		<pathelement path="${class.dir}"/>
	</path>

	<target name="usage" description="脚本包含的可执行命令">
		<echo message="【帮助信息】${name}工程可用命令:"/>		
		<echo message="使用方法:在ant 命令后加下列命令"/>
		<echo message=""/>
		<echo message="build:-->(编译工程)build the application"/>
		<echo message="deploy:-->(发布应用)deploy application as directory"/>
		<echo message="deploywar:-->(打包成war文件)deploy application as a war file"/>
		<echo message="install:-->(将应用安装到tomcat)Install application in tomcat"/>
		<echo message="reload:-->(重新装入应用)reload application in tomcat"/>
		<echo message="start:-->(开始tomcat应用)start tomcat application"/>
		<echo message="stop:-->(停止tomcat应用)stop tomcat application"/>
		<echo message="list:-->(列表tomcat的应用)list tomcat application"/>
	</target>

	<!--[初始化]
		1.建立目录
		2.拷贝外部引用包web-inf/lib
		3.拷贝配置文件(web.xml等)至web-inf	
	-->
	<target name="init">
		<mkdir dir="${class.dir}" />
		<mkdir dir="${lib.dir}" />
		
		<copy todir="${lib.dir}">
			<fileset dir="${extlib.dir}">
				<include name="*.jar"/>
			</fileset>			
		</copy>

		<copy todir="${web.dir}/WEB-INF">
			<fileset dir="${config.dir}">
				<include name="web.xml"/>
			</fileset>			
		</copy>	
	</target>

	<!-- [编译程序] -->
	<target name="build" depends="init" description="build the application">
		<javac srcdir="${src.dir}" destdir="${class.dir}" debug="true" deprecation="true">
			<classpath refid="master-classpath" />
		</javac>

		<copy todir="${class.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.gif" /> 
				<include name="**/*.jpg" /> 
				<include name="**/*.png" /> 
				<include name="**/*.wav" /> 
				<include name="**/*.dtd" /> 
				<include name="**/*.properties" /> 
			</fileset>
		</copy>		
	</target>
	
	<!-- [发布目录到tomcat]
		1.建立tomcat目录
		2.拷贝war目录下所有内容到tomcat目录
	-->
	<target name="deploy" description="Deploy application to servlet container">		 
		 <mkdir dir="${deploy.path}/${name}"/>
		 <copy todir="${deploy.path}/${name}">
			<fileset dir="${web.dir}"/>
		 </copy>
	</target>
	
	<!--[发布应用为war包]-->
	<target name="deploywar">
		<war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
			<fileset dir="${web.dir}"/>
		</war>
	</target>

	<!--[删除tomcat应用]-->
	<target name="clean">
		<delete dir="${deploy.path}/${name}"/>
	</target>

	<target name="start">
		<java jar="${appserver.home}/bin/bootstrap.jar" fork="true">
			<jvmarg value="-Dcatalina.home=${appserver.home}"/>
			<arg line="start"/>
		</java>
	</target>

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