如何使用ant

本文详细介绍如何使用Apache Ant进行项目的自动化构建过程,包括安装配置、编写build.xml文件,并演示了普通Java项目及Web项目的构建步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:从http://ant.apache.org 下载 apache-ant-1.7.0-bin.zip,解压到你自己的目录,我的:E:\apache-ant-1.7.0 

第二步:配置 JAVA_HOME 和 ANT_HOME 
配置完成后,打开dos窗口,输入 ant 回车,如果提示: 
Buildfile: build.xml does not exist! 
Build failed 

则说明配置完成 

第三步:创建自己的工程 
我的工程:test 
目录结构: 
D:\work\code\test_core\src 
D:\work\code\test_core\lib 


第四步:编写build.xml 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <project default="jar" name="test" basedir=".">  
  4.     
  5.   <property name="defaulttargetdir" value="./target"></property>   
  6.   <property name="classesdir" value="./target/classes"></property>  
  7.   <property name="logsdir" value="./logs"></property>  
  8.   <property name="defaulttargetdir" value="./target"></property>  
  9.   <property name="final.name" value="test"></property>  
  10.     
  11.   
  12.   <target name="compile" description="o Compile the code" >  
  13.     <mkdir dir="${defaulttargetdir}"></mkdir>  
  14.     <mkdir dir="${classesdir}"></mkdir>  
  15.     <mkdir dir="${logsdir}"></mkdir>  
  16.   
  17.     <javac destdir="${classesdir}" deprecation="true" debug="true" optimize="false" excludes="**/package.html">  
  18.       <src>  
  19.         <pathelement location="./src"></pathelement>  
  20.       </src>  
  21.       <classpath>  
  22.         <fileset dir="./lib">  
  23.           <include name="*.jar"></include>  
  24.         </fileset>  
  25.       </classpath>  
  26.     </javac>  
  27.       
  28.   </target>  
  29.       
  30.   <target name="jar" description="o Create the jar" depends="compile">  
  31.         <copy todir="${classesdir}">  
  32.             <fileset dir="./src">  
  33.                 <include name="**/*.properties" />  
  34.             </fileset>  
  35.         </copy>      
  36.     <jar jarfile="./lib/${final.name}.jar" excludes="**/package.html" basedir="${classesdir}"></jar>  
  37.   
  38.   </target>  
  39.     
  40.   <target name="clean" description="o Clean up the generated directories">  
  41.     <delete dir="${classesdir}"></delete>  
  42.     <delete file="${lib.dir}/${webapp.name}.jar" />  
  43.   </target>  
  44.   
  45.   
  46.   
  47. </project>  
  48.       


打开dos窗口,进入D:\work\code\test_core\目录,输入ant jar 回车 
则test.jar 文件将会被输出到 lib目录下 
如果仅仅是编译java源文件,你可以使用 ant compile命令,classes文件将会被输出到 target/classes目录下,如果你在src下有以properties结尾的配置文件,则配置文件将也会被拷贝到 target/classes 目录下,起作用的是:
<copy todir="${classesdir}"> 
<fileset dir="./src"> 
<include name="**/*.properties" /> 
</fileset> 
</copy> 

web工程: 
D:\work\code\test_web\src 
D:\work\code\test_web\lib 
D:\work\code\test_web\web 

build.xml如下: 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="testweb" basedir="." default="">  
  3.     <property name="webapp.name" value="testweb" />  
  4.     <property name="webapp.version" value="1.0" />  
  5.     <property name="lib.dir" value="${basedir}/lib" />  
  6.     <property name="dist.dir" value="${basedir}/dist" />  
  7.     <!-- The target directory for building the unpacked web application -->  
  8.     <property name="build.dir" value="${basedir}/build" />  
  9.     <property name="classes.dir" value="${basedir}/build/classes" />  
  10.     <!-- The target directory for building the packed web application -->  
  11.     <property name="webapp.dist" value="${dist.dir}" />  
  12.     <!-- The target directory for building the unpacked web application -->  
  13.     <property name="webapp.target" value="${build.dir}/web" />  
  14.     <property environment="env" />  
  15.     <property name="tomcat.home" value="D:/tomcat-5.5.23" />  
  16.   
  17.     <target name="init" description="defines custom tasks">  
  18.     </target>  
  19.       <path id="tomcat.classpath">  
  20.         <pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/>  
  21.       </path>  
  22.     <!-- Check timestamp on files -->  
  23.     <target name="prepare" depends="init" description="create target directories">  
  24.         <tstamp>  
  25.             <format property="copyright.year" pattern="yyyy" />  
  26.         </tstamp>  
  27.         <echo message="Preparing target directory '${webapp.target}'" />  
  28.         <mkdir dir="${lib.dir}" />  
  29.         <mkdir dir="${dist.dir}" />  
  30.         <mkdir dir="${build.dir}" />  
  31.         <mkdir dir="${classes.dir}" />  
  32.         <mkdir dir="${webapp.target}" />  
  33.         <mkdir dir="${webapp.target}/WEB-INF" />  
  34.         <mkdir dir="${webapp.target}/WEB-INF/lib" />  
  35.         <mkdir dir="${webapp.dist}" />  
  36.   
  37.     </target>  
  38.   
  39.     <!-- List of variables in .properties files that will be replaced at  
  40.          build time -->  
  41.     <filterset id="variables.to.replace">  
  42.         <filter token="APPNAME" value="${webapp.name}" />  
  43.     </filterset>  
  44.   
  45.     <!-- Copy any resource or configuration files -->  
  46.     <target name="compile" depends="prepare" description="compile source directory">  
  47.         <echo message="compileing ${webapp.name} ..." />  
  48.    
  49.         <copy todir="${classes.dir}">  
  50.             <fileset dir="${basedir}/src">  
  51.                 <include name="**/*.properties" />  
  52.    
  53.               
  54.             </fileset>  
  55.         </copy>  
  56.           
  57.           
  58.         <javac srcdir="${basedir}/src" destdir="${classes.dir}">  
  59.             <include name="**/*.java" />  
  60.             <classpath>  
  61.                 <fileset dir="${basedir}/web/WEB-INF/lib">  
  62.                     <include name="*.jar">  
  63.                     </include>  
  64.                 </fileset>  
  65.             </classpath>  
  66.             <classpath refid="tomcat.classpath"/>  
  67.         </javac>  
  68.     </target>  
  69.     <!-- Copy any resource or configuration files -->  
  70.     <target name="jar" depends="compile" description="jar ">  
  71.         <echo message="jar ${webapp.name}.jar  ..." />  
  72.         <jar jarfile="${lib.dir}/${webapp.name}.jar">  
  73.             <fileset dir="${classes.dir}" includes="**" />  
  74.         </jar>      
  75.     </target>  
  76.     <!-- Simple alias to package-web -->  
  77.     <target name="war" depends="jar" description="alias for package-web">  
  78.         <echo message="war {webapp.name} ..." />  
  79.         <copy todir="${webapp.target}/WEB-INF">  
  80.             <fileset dir="${basedir}/web/WEB-INF">  
  81.                 <include name="*.xml" />  
  82.             </fileset>  
  83.         </copy>  
  84.         <copy todir="${webapp.target}/WEB-INF/lib">  
  85.             <fileset dir="${lib.dir}">  
  86.                 <include name="**/*.jar" />  
  87.             </fileset>  
  88.             <fileset dir="${basedir}/web/WEB-INF/lib">  
  89.                 <include name="**/*.jar" />  
  90.             </fileset>  
  91.         </copy>  
  92.   
  93.         <copy todir="${webapp.target}">  
  94.             <fileset dir="${basedir}/web">  
  95.                 <include name="**/*.html" />  
  96.                 <include name="**/*.htm" />  
  97.                 <include name="**/*.vm" />  
  98.                 <include name="**/*.jsp" />  
  99.                 <include name="**/*.txt" />  
  100.                 <include name="**/*.zip" />  
  101.                 <include name="**/*.gif" />  
  102.                 <include name="**/*.jpg" />  
  103.                 <include name="**/*.png" />  
  104.                 <include name="**/*.js" />  
  105.                 <include name="**/*.css" />  
  106.                 <include name="**/*.htc" />  
  107.                 <include name="**/*.dwt" />  
  108.                 <include name="**/*.cert" />  
  109.                 <include name="**/*.jnlp" />  
  110.                 <include name="**/*.xml" />  
  111.                 <include name="**/*.tld" />  
  112.                 <!--include name="**/*.properties" /-->  
  113.             </fileset>  
  114.         </copy>  
  115.         <echo message="Packaging ${webapp.name}'s web archive file ..." />  
  116.         <delete file="${webapp.dist}/${webapp.name}.war" />  
  117.         <jar jarfile="${webapp.dist}/${webapp.name}.war">  
  118.             <fileset dir="${webapp.target}" includes="**" />  
  119.         </jar>  
  120.   
  121.     </target>  
  122.   
  123.     <target name="deploy" depends="compile,war" if="tomcat.home" description="unwar into the servlet container's deployment directory">  
  124.   
  125.         <unwar src="${webapp.dist}/${webapp.name}.war" dest="${tomcat.home}/webapps/${webapp.name}" />  
  126.     </target>  
  127.     <target name="undeploy" if="tomcat.home" description="undeploy war file to servlet container's deployment dir">  
  128.         <echo message="Undeploying webapp from Tomcat" />  
  129.         <delete file="${tomcat.home}/webapps/${webapp.war}" />  
  130.         <delete dir="${tomcat.home}/webapps/${webapp.name}" />  
  131.     </target>  
  132.     <target name="clean">  
  133.         <echo message="cleaning ${build.dir} and ${dist.dir} ... " />  
  134.         <delete dir="${build.dir}" />  
  135.         <delete dir="${dist.dir}" />  
  136.         <delete file="${lib.dir}/${webapp.name}.jar" />  
  137.     </target>  
  138. </project>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值