Atea - Hero's Grave

面向对象,开源,框架,敏捷,云计算,NoSQL,商业智能,编程思想。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  40 随笔 :: 0 文章 :: 28 评论 :: 0 Trackbacks
自从年初开始用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即可。

附源码firsttest.rar

posted on 2008-07-17 17:51 Atea 阅读(10006) 评论(1)  编辑  收藏 所属分类: Ant

评论

# re: Ant学习笔记——自己构建Ant编译环境 2008-07-18 08:48 melland
写的不错的build.xml  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: