zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Ant学习笔记

2023-09-27 14:24:38 时间

前言:这段时间在学习Ant,发现这是一个很强大的构建工具。你可能使用了很长一段时间,才发现Ant能做数不完的事。总之,个人觉得,Ant学习门槛低,入门简单,能大概看懂程序,写一些简单的脚本即可,剩下在遇到的时候边做边学就可以了。另外,博主资历尚浅,有说的不对的地方,还请大家不吝指教。

一、Ant是一个Apache基金会下的跨平台的构建工具,它可以实现项目的自动构建和部署等功能。它具有跨平台、使用简单、语法清晰、功能强大等特点。
 
二、下载和安装:http://ant.apache.org/,添加环境变量 ANT_HOME ,指向根目录 D:\hybirs630\hybris\bin\platform\apache-ant-1.9.1,安装完成后,进入控制台界面,输入 ant -version检查是否安装成功。
 
三、Ant的构建文件是通过XML编写的,默认名称是build.xml。
<?xml version="1.0"?>
<project name="helloWorld">
       <target name="sayHelloWorld">
              <echo message="Hello,Amigo"/>
       </target>
</project>
备注:在文件的存在目录执行ant sayHelloWorld,ant 默认寻找文件 build.xml ,若文件不为 build.xml ,则执行ant –f hello.xml sayHelloWorld、ant –buildfile hello.xml sayHelloWorld 或 ant –file hello.xml sayHelloWorld)
 
四、Ant的关键元素 project、target、property、task
 
project 元素(至少包含一个,可以包含多个target)、 name属性、 default属性(用于指定project默认执行时所执行的target的名称)、basedir属性(用于指定路径的位置。没有指定,使用Ant的构件文件的附目录作为基准目录)
<?xml version="1.0"?>
<project name="projectStudy" default="sayBaseDir" basedir="D:\hybirs630\hybris\bin\platform\apache-ant-1.9.1">
       <target name="sayBaseDir">
              <echo message="The base dir is: ${basedir}"/>
       </target>
</project>
 
target 元素(是Ant的基本执行单元,它可以包含一个或多个具体的任务,多个target可以存在相互依赖关系)、name属性(指定名称)、depends属性(描述target之间的依赖关系,以,间隔,顺序执行,被依赖的target优先执行)、if/unless属性(验证属性是否存在,若存在,则执行/不执行)、description属性(target功能的简短描述与说明)
<?xml version="1.0"?>
<project name="targetStudy">
       <target name="targetA" if="ant.java.version">
              <echo message="Java Version: ${ant.java.version}"/>
       </target>
       <target name="targetB" depends="targetA" unless="amigo">
              <description>
                            a depend example!
              </description>
              <echo message="The base dir is: ${basedir}"/>
       </target>
</project>
 
property 元素(参量或者参数的定义),若要在外部引入某文件,例如 build.properties 文件,可以通过如下内容将其引入:<property file=” build.properties”/>,同时,Ant还提供了一些它自己的内置属性,如下:
  • basedir:project基目录的绝对路径.
  • ant.file:buildfile的绝对路径,如上面的各例子中,ant.file的值为E:"build.xml;
  • ant.version:Ant的版本,在本文中,值为1.7.0;
  • ant.project.name:当前指定的project的名字,即前文说到的project的name属性的值;
  • ant.java.version:Ant检测到的JDK的版本。
ant 中若引用 property 或者它的内置属性,用 ${} 括号起来引用。
<?xml version="1.0"?>
<project name="propertyStudy" default="example">
  <property name="name" value="amigo"/>
  <!--读取属性文件的属性配置-->
  <property file="foo.properties"/>
  <property name="age" value="25"/>
   <target name="example">
        <echo message="name: ${name}, age: ${age}"/>
   </target>
</project>
 
五、Ant的任务介绍
 
1、输出信息
<echo message="xxx"/>
<echo>yyyy</echo>     
 
2、copy 任务
该任务主要用来对文件和目录的复制功能。举例如下:
  • 复制单个文件
<copy file="file.txt" tofile="copy.txt"/>
  • 对文件目录进行复制
   <copy todir="../newdir/dest_dir"> 
            <fileset dir="src_dir"/>
  </copy>
  • 将文件复制到另外的目录
  <copy file="file.txt" todir="../other/dir"/>
  <copyfile src="test.java" dest="subdir/test.java">
  • 拷贝一堆文件到一个目录
<copy todir="../dest/dir">
    <fileset dir="src_dir">
       <exclude name="**/*.java">
   </fileset>
</copy>
  • 拷贝一个目录下的东西到另一个目录下(includes加入,excludes排除,即排除这些之外的东西都要加入)
<copy src="${src}/resources" dest="${dist}" includes="**/*.java" excludes="**/Test.java"/>
 
3、delete 任务
  • 删除某个文件
<delete file="photo/amigo.jpg"/>
  • 删除某个目录:
<delete dir="photo"/> 
  • 删除所有的备份目录或空目录:(也可以使用include和exclude)
        <delete includeEmptyDirs="true">
               <fileset dir="." includes="**/*.bak"/>
        </delete>
 
 4、mkdir任务

创建目录

<mkdir dir="build"/>
 
5、move任务 
  • 移动单个文件
<move file="fromfile" tofile=”tofile”/>
  • 移动单个文件到另一个目录
<move file="fromfile" todir=”movedir”/>
  • 移动某个目录到另一个目录
 <move todir="newdir">
      <fileset dir="olddir"/>
 </move>
6、重命名任务
<rename src="foo.jar" dest="ant-${version}.jar"/>  
7、Touch的使用

如果文件不存在,创建文件,如果存在,更改最后访问时间为系统时间

<touch file="myfile"/>
8、打Jar包
<target name="build" depends="compile">
    <jar destfile="${hello_jar}" basedir="${dest}"/>
</target>
9、压缩zip文件
<zip destfmlile="${dist}/manual.zip"
       basedir="htdocs/manual"
       includes="api/**/*.html"
       excludes="**/todo.html"/>
 
六、利用Ant构建和部署Java工程,可参考我的另一篇博客:http://www.cnblogs.com/jmcui/p/6946777.html

七、一份Ant的API文档