转载:Ant学习笔记——自己构建Ant编译环境

转载:http://www.blogjava.net/atealxt/archive/2008/07/17/ant_study_note.html
自从年初开始用NetBeans6.0,才接触到Ant。
这是今年6月份的一篇Ant学习笔记。

安装
1.下载并构建环境。
去官网下载src包和bin包。解压缩它们到同一目录,运行build.bat,bootstrap.bat。
2.设置系统环境变量
新建ANT_HOME,值为解压缩的目录。如C:\ant\apache-ant-1.7.0
在PATH末尾追加%ANT_HOME%\bin;
3.测试安装环境
命令行下输入ant -version,成功看到Ant版本信息。

运行
1.建一个项目文件夹firsttest
2.把项目开发完成的文件和文件夹放到firsttest下
例:java文件的文件夹src,web文件的文件夹web
3.firsttest下新建文件build.xml,编辑如下:

<?xml version="1.0" encoding="UTF-8"?>                                    

<project name="ant_firsttest" default="dist" basedir=".">                                    
  <description>ant firsttest!</description>                                    

  <!-- set global properties for this build -->        
  <!--设定变量,之后用。location为文件夹路径-->                            
  <property name="src" location="src"/>                                    
  <property name="build" location="build"/>                                    
  <property name="dist"  location="dist"/>                                    
  <property name="web"  location="web"/>                                    

  <!--设置properties文件位置.这里没用到。-->                                       
  <!--<property file="nbproject/project.properties"/>-->                                    

  <!--初始化命令-->                                    
  <target name="init">                                    
    <!-- Create the time stamp -->                                    
    <tstamp/>                                    

    <!--mkdir是建立文件夹,${build}即刚才设定的变量。这几行都在干这事。-->    
    <!-- Create the build directory structure used by compile -->                                    
    <mkdir dir="${build}/WEB-INF/lib"/>                                    
    <mkdir dir="${build}/WEB-INF/classes"/>                                    

    <mkdir dir="${build}/WEB-INF/classes/javafile/package1"/>                                          
    <mkdir dir="${build}/WEB-INF/classes/javafile/package2"/>                                               

  </target>                                    

  <!--编译-->                                    
  <target name="compile" depends="init"                                    
        description="compile the source " >                                    

    <!-- Compile the java code from ${src} into ${build} -->                                    
    <!--javac标签用来设置编译程序的参数,srcdir为java文件路径,destdir为编译后class文件的保存路径。-->
    <javac srcdir="${src}/javafile/package1" destdir="${build}/WEB-INF/classes/javafile/package1"/>                                    
    <javac srcdir="${src}/javafile/package2" destdir="${build}/WEB-INF/classes/javafile/package2"/>                                    
    <!--如果路径下还有别的文件需要一起打包,用copy 命令。-->    
    <copy file="${src}/hello_ant.xml" tofile="${build}/WEB-INF/classes/hello_ant.xml" />                                                                             

  </target>                                    

  <!--编译后就要打包了。-->                                    
  <target name="dist" depends="compile"                                    
        description="generate the distribution" >                                    
    <!-- Create the distribution directory -->                                    
    <mkdir dir="${dist}"/>                                    

       <!--像jsp,jar这些直接用不用编译的文件,直接用copy命令。-->                                
    <copy file="${web}/image/a.gif" tofile="${build}/image/a.gif" />                                    
    <copy file="${web}/WEB-INF/web.xml" tofile="${build}/WEB-INF/web.xml" />                                     
    <copy file="${web}/WEB-INF/lib/a.jar" tofile="${build}/WEB-INF/lib/a.jar" />                                          
    <copy file="${web}/index.jsp" tofile="${build}/index.jsp" />                                     

    <!--最后用jar命令打成jar/war文件,文件名和后缀随便起。basedir为欲打包的原文件路经-->                                    
    <jar jarfile="${dist}/ant_firsttest.jar" basedir="${build}"/>                                    
  </target>                                    

  <!--删除-->
  <target name="clean"                                    
        description="clean up" >                                    
    <!--设定删除命令要删的路径。-->    
    <!-- Delete the ${build} and ${dist} directory trees -->                                    
    <delete dir="${build}"/>                                    
    <delete dir="${dist}"/>                                    
  </target>                                    
</project>                                        
4.控制台在firsttest目录,输入ant后回车就打包完成了!
  注:输入ant回车自动执行init,compile,dist命令。要想执行clean命令,输入ant clean即可。

参考:
http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值