1.在myeclipse中创建javaWeb项目AntDemo
2.将build.xml放到AntDemo根目录下
3.修改build.xml中的Project name,工程目录名,工程名,还有tomcat路径
4.右键->properties->builders->新建一个build,指向你刚刚创建的build.xml
5.选中build.xml->run as Ant Builder就ok了
Ant脚本build.xml
<?xml version="1.0" encoding="gbk"?> <project name="AntDemo" default="build" basedir="."> <!-- 工程根目录 --> <property name="AntDemo.home" value="."/> <!-- 工程根名 --> <property name="AntDemo.name" value="AntDemo"/> <!-- tomcat路径 --> <!-- <property name="tomcat.home" value="D:/Program Files/apache-tomcat-7.0.54/apache-tomcat-7.0.54"/> --> <property name="tomcat.home" value="D:/Program Files/apache-tomcat-7.0.54/apache-tomcat-7.0.54"/> <!-- tomcat部署路径 --> <property name="webapps.home" value="${tomcat.home}/webapps"/> <!-- JAVA源文件路径 --> <property name="src.home" value="${AntDemo.home}/src"/> <!-- class文件存放位置 --> <property name="classes.home" value="${AntDemo.home}/classes"/> <!-- 发布文件夹 --> <property name="deploy.home" value="${webapps.home}"/> <!-- <property name="deploy.home" value="${webapps.home}/deploy"/> --> <!-- webRoot路径 --> <property name="web.home" value="${AntDemo.home}/WebRoot"/> <!-- 创建工程路径 --> <property name="build.home" value="${AntDemo.home}/build"/> <!-- /WEB-INF/classes --> <property name="build.classes" value="${AntDemo.home}/WEB-INF/classes"/> <!-- /WEB-INF/lib --> <property name="build.lib" value="${AntDemo.home}/WEB-INF/lib"/> <!-- 工程引用包 --> <property name="lib.home" value="${web.home}/WEB-INF/lib"/> <!-- 编译工程所需的JAR包 --> <path id="compile.classpath"> <pathelement location="${classes.home}"/> <fileset dir="${lib.home}"> <include name="*.jar"/> <include name="*.zip"/> </fileset> <fileset dir="${tomcat.home}/lib"> <include name="*.jar"/> </fileset> <pathelement location="${tomcat.home}/common/classes"/> </path> <!-- 测试ANT --> <target name="help"> <echo message="Please specify a target![usage:ant‘targetname‘]"/> <echo message="Here is a list of possible targets:"/> <echo message=" clean-all....Delete build dir,all .class and war files"/> <echo message=" perpare....Creates directories if requied"/> <echo message=" compile....Compiles source files"/> <echo message=" build......Build war file from.class and other files"/> <echo message=" deploy......Copy war file to the webapps directory"/> </target> <!-- 清除 --> <target name="clean-all"> <delete dir="${build.home}"/> <delete dir="${classes.home}"/> <delete dir="${deploy.home}"/> <delete dir="${webapps.home}/${AntDemo.home}" failonerror="false"/> <delete dir="${webapps.home}/work/" /> <delete dir="${webapps.home}/${AntDemo.name}.war"/> </target> <!-- 准备工作 --> <target name="prepare" depends="clean-all"> <echo message="Tomcat Home=${tomcat.home}"/> <echo message="Webapps Home=${webapps.home}"/> <echo message="classes Home=${classes.home}"/> <echo message="war Home=${deploy.home}"/> <echo message="Java src Home=${src.home}"/> <mkdir dir="${classes.home}"/> <mkdir dir="${deploy.home}"/> <mkdir dir="${build.home}"/> <mkdir dir="${build.home}/WEB-INF"/> <mkdir dir="${build.home}/WEB-INF/classes"/> <mkdir dir="${build.home}/WEB-INF/lib"/> </target> <!-- 编译 --> <target name="compile" depends="prepare"> <javac srcdir="${src.home}" destdir="${classes.home}" includeantruntime="on" debug="true"> <compilerarg line="-encoding UTF-8"/> <classpath refid="compile.classpath"/> </javac> </target> <!-- 构建临时工程 --> <target name="build" depends="compile"> <copy todir="${build.home}"> <fileset dir="${web.home}"/> </copy> <copy todir="${build.home}/WEB-INF/classes"> <fileset dir="${classes.home}"/> </copy> <copy todir="${build.home}/WEB-INF/classes"> <fileset dir="${src.home}"> <include name="**/*.properties"/> <include name="**/*.prop"/> </fileset> </copy> <jar jarfile="${deploy.home}/${AntDemo.name}.war" basedir="${build.home}"/> </target> <!-- 发布 --> <target name="deploy" depends="build"> <copy todir="${webapps.home}" file="${deploy.home}/${AntDemo.name}.war"/> </target> </project>
时间: 2024-10-29 17:36:42