build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<property name="build.dir" location="build"></property>
<property name="src.dir" location="src"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.war" location="${build.dir}/war"></property>
<property name="web.name" value="hello"></property>
<property name="web.root" value="WebRoot"></property>
<property name="web.WEB-INF" location="${web.root}/WEB-INF"></property>
<property name="web.lib" location="${web.WEB-INF}/lib"></property>
<!--environment="env" 用${env.xxx}可以引用相应的系统变量或者环境变量等等。如${env.TOMCAT_HOME}就引用TOMCAT_HOME-->
<property environment="env"></property>
<!--lib所在的路径,及tomcat的lib所在的路径-->
<path id="complie">
<fileset dir="${web.lib}" includes="*.jar"></fileset>
<fileset dir="${tomcat_home}/lib" includes="*.jar"></fileset>
</path>
<target name="init">
<delete dir="${build.dir}"></delete>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.war}"/>
</target>
<!--编译-->
<target name="complie" depends="init" >
<javac destdir="${build.classes}" srcdir="${src.dir}" classpathref="complie"></javac>
</target>
<!--把编译好的文件打成war包-->
<target name="war" depends="complie">
<war destfile="${build.war}/${web.name}.war" basedir="${build.classes}">
<fileset dir="${web.root}" includes="**/*.*"></fileset>
<lib dir="${web.lib}"></lib>
<webinf dir="${web.WEB-INF}"></webinf>
<classes dir="${build.classes}"></classes>
</war>
</target>
<!--把war包拷贝到webapps目录下,启动tomcat就可以访问项目了-->
<target name="deploy" depends="war">
<copy todir="${env.TOMCAT_HOME}/webapps" >
<fileset dir="${build.war}" includes="${web.name}.war"></fileset>
</copy>
</target>
<!--MySql 连接-->
<presetdef name="sql-admin">
<sql driver="com.mysql.jdbc.Driver" password="123" url="jdbc:mysql://localhost:3300/test" userid="123">
<classpath refid="complie"></classpath>
</sql>
</presetdef>
<!--SQLServer 连接-->
<!--
<presetdef name="sql-admin">
<sql driver="com.microsoft.jdbc.sqlserver.SQLServerDriver" password="123" url="jdbc:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=test" userid="123">
<classpath refid="complie"></classpath>
</sql>
</presetdef>
-->
<target name="init-mysql">
<sql-admin>
<transaction src="${build.dir}/mysql/mysql.sql"><!--可以用src引用外部的sql文件,也可以把语句直接写在下面-->
create table ant2(id int);
</transaction>
</sql-admin>
</target>
<!--echo测试-->
<target name="echo_test">
<property environment="env"></property>
<echo>${env.TOMCAT_HOME}</echo>
<echo>${env.OS}</echo>
<echo>${env.JAVA_HOME}</echo>
</target>
</project>