`
sunqitang
  • 浏览: 74878 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ant介绍

阅读更多

Ant 是什么?

Ant 是一个构建工具,它可以帮助我们将项目开发过程中需要完成的各种步骤组织起来,通过一个简易的方式来构建整个项目。 Ant 究竟能做什么呢?这取决于 Ant 的任务( Task ), Ant 有哪些任务,就能完成哪些工作!一些典型的任务是:编译 java 源代码、运行 java 程序、拷贝文件或目录、将编译完成的类打包等等。

       当然, Ant 是一个开放的架构,任何人,都可以按照自己的方式,添加自己的任务( Task ),这需要遵守 Ant 的一些规范。

如何开始使用 Ant

1、   确保你的机器已经安装了 JDK ,确保你的 path 环境变量中包含有 java 虚拟机的运行程序,确保你的环境变量中有 JAVA_HOME 变量

2、   下载 ant 并解压

3、   设置 ANT_HOME 环境变量,指向 ant 解压根目录

4、   path 环境变量中,添加 ANT_HOME\bin 目录

5、   打开控制台,运行: ant

6、   如果能够运行 ant (忽略这些异常: Buildfile: build.xml does not exist! ),而不是出现诸如命令无法解释的错误,那么你的 ant 就安装成功了

Ant 的基本概念

Ant 的运行,靠的是一个构建脚本,默认的名称叫 build.xml ,如果你在任何目录下运行 ant 命令, Ant 将自动查找当前目录下有没有 build.xml 文件,如果有这个文件,就读取这个文件,并运行其中的默认 target

解释一下以上话的意思:

构建脚本

Ant 根据构建脚本的描述来执行任务!构建脚本是一个 xml 格式的文件。最简单的构建脚本是:

<?xml version="1.0" encoding="GBK"?>

<project name= " 测试脚本 " default= "copyfile" basedir= "." >

    <target name= "copyfile" >

       <copy file="d:/a.txt" todir="c:/temp" overwrite="true"/>

    </target>

</project>

如果把以上内容放到一个 build.xml 文件中,然后运行 ant 命令, Ant 将执行拷贝文件的任务!

从这个最简单的构建脚本中,我们可以知道脚本的基本编写方法:

1、  脚本的根元素是: <project>

2、  所有的任务,必须通过一个 <target> 标签包围,一个 <target> 标签,可以包含多个任务。

3、  <copy> 标签描述了一个任务,这些任务必须放到一个 <target> 标签的内部, Ant 都可以使用哪些任务呢?请参考 Ant 的相关文档

4、  <project> 中可以包含多个 <target> ,如果不指定运行哪个任务(即直接运行 ant 命令),那么,将使用 <project> 标签中指定的 default 属性的 target

 

ant的文档在解压文件中

 在cmd中把光标提示调整到build.xml所在的目录,然后运行ant。上面的命令即可成功执行。

构建脚本的命名

默认命名是 build.xml ,如果你的构建脚本的名称是 build.xml ,那么你可以直接运行 ant 命令,如果是其它的名称,如: mybuild.xml ,那么,你的命令行应该改为: ant –f  mybuild.xml

运行特定任务?

运行: ant copyfile ,将运行的是被命名为 copyfile target

Ant 的路径模式?

再看下面的 ant 构建脚本:

<?xml version="1.0" encoding="GBK"?>

<project name= " 测试脚本 " default= "copyfile" basedir= "." >

    <target name= "copyfile" >

       <copy todir="c:/temp" overwrite="true">

           <fileset dir="d:">

              <include name="*.txt"/>

           </fileset>

       <copy>

    </target>

</project>

有没有注意到这个文件,跟第一个文件的变化?没错, <copy> 任务的 file 属性变了,现在是 *.txt ,可能你已经意识到,这个的意思是拷贝 d: 盘下所有的以 txt 为扩展名的文件到 c:/temp 目录中。完全正确!但是,究竟包不包含子目录下的 txt 文件呢?这就需要我们了解 ant 中的路径模式。 Ant 中的路径模式非常重要,很多地方都会用到,所以,我们首先来学习的就是 ant 的路径模式!

 

你可以查看 ANT_HOME/docs/manual/dirtasks.html#patterns 下的文档描述!里面有最详细的说明。

 

简单归纳一下:

 

* 符号代表 0 个或若干个字符。

? 符号,代表一个字符。

** 代表一颗目录树(目录和子目录全部包括,任何一级)。

 

Ant 任务

Ant 有很多内置的(即你安装完 ant 之后就可以直接使用的任务)任务,但是也可以添加自己定义的任务。如果你编写了自己的任务,或者你要引入一些不是 ant 自带的任务时,我们需要使用 <taskdef> 标签来定义:

 

比如:

   <path id= "xdoclet.task.classpath" >

         <fileset dir= "${xdoclet.home}/lib" >

         <include name= "**/*.jar" />

         </fileset>

         <fileset dir= "${xdoclet.home}/plugins" >

         <include name= "**/*.jar" />

         </fileset>

   </path>

    <taskdef

       name= "xdoclet"

       classname= "org.xdoclet.ant.XDocletTask"

       classpathref= "xdoclet.task.classpath"

    />

在这段文本 里,包含了很多内容:

1 <path> 元素,是用来定义一个路径的,通常是定义 classpath ,因为 classpath 可以包括众多的 jar 包,也可以包括众多的目录,所以可以使用 <fileset> 标签或 <pathelement> 等标签来定义这些路径。

2 、这些标签总体上来说都是轻易便能够看懂的。无需过多的解释。那个 <include> 标签中的 name 属性,正是一个路径模式,表示包括本目录(即 dir 属性定义的目录。)下的所有子目录中的 jar 文件。

 

定义完任务之后,就可以来使用它了,如:

    <!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} --> <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

<target name= " 生成 Hibernate 配置文件 " >

       <xdoclet>

           <fileset dir= "${src.dir}/com/bjsxt/oa/model" >

              <include name= "**/*.java" />

           </fileset>        

           <component

              classname= "org.xdoclet.plugin.hibernate.HibernateConfigPlugin"

              destdir= "${src.dir}"

              version= "3.0"

              hbm2ddlauto= "update"

              jdbcurl= "jdbc:mysql://127.0.0.1/oa_200706"

              jdbcdriver= "com.mysql.jdbc.Driver"

              jdbcusername= "root"

              jdbcpassword= "mysql"

              dialect= "org.hibernate.dialect.MySQLDialect"

              showsql= "true"

           />

       </xdoclet>

    </target>

具体如何使用,请参考关于这个 task 的相关说明文档。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics