ant使用教程--基础篇

来源:互联网 发布:一年级学拼音软件 编辑:程序博客网 时间:2024/05/24 04:51

centos下ant的安装:http://blog.csdn.net/u014803081/article/details/72866666
ant的进阶:http://blog.csdn.net/u014803081/article/details/72869410
ant的svn:http://blog.csdn.net/u014803081/article/details/72884895

先看下ant的使用方法
[root@localhost ~]# ant -help
这里写图片描述
用到比较多的命令:

-buildfile<file>,-file<file>,-f<file> :指定执行构件文件的位置和名称。-find<file>,-s<file> :查找构件文件,并执行找到的构件文件。-help,-p :显示ant的命令帮助信息。-version :显示ant 的版本信息。-diagnostics :显示ant项目的运行环境、依赖库等信息,为错误诊断和报告提供一些有用的帮助信息。 -quiet,-q :隐藏ant执行的提示输出信息。命令行将不会输出具体的target的执行信息。-verbose,-v : 显示ant执行的详细信息,包括操作系统和Java环境等信息。-debug,-d :显示ant执行调试的信息、详细的log信息。-lib<path> :指定一个类库所在的位置(jar文件位置),让ant工具可以使用这些类文件。path类型指定类库文件。-logfile<file>,-l<file> :指定ant执行时日志输出文件,让日志输出到指定文件而不在命令行输出。-D<property>=<value> :用于设定一些变量,这些变量可以在target中直接引用,或覆盖已设定的属性值。

1、先建一个ant测试操作目录
[root@localhost src]# mkdir -p /data/ant
ant执行是基于xml简本文件的,首先我们建一个xml文件
[root@localhost ant]# vi test-build.xml
格式必须是这样的,name值得是ant的名称,default默认执行的target的name,basedir是默认操作的空间,如果不写就是xml文件所在的目录。

<?xml version="1.0"?><project name="MyBuild" default="test" basedir="/data/ant/build">     <target name="test">        <echo message="test echo messsage, basedir=${basedir}" />    </target></project>

执行,先说明下默认参数
基础命令:ant -f test-build.xml test
默认target是project中的default执行
默认xml的名字是build.xml
所以在这里 ant = ant -f build.xml test
下面我们开始一点一点说明:
[root@localhost build]# ant -f test-build.xml
这里写图片描述

2、加入第二个target

<?xml version="1.0"?><project name="MyBuild" default="test" basedir="/data/ant/build">    <target name="test">        <echo message="test echo messsage, basedir=${basedir}" />    </target>     <target name="test2">        <echo message="test2 echo messsage, basedir=${basedir}" />    </target></project>

分别执行两个target,默认的是不需要加name的
[root@localhost build]# ant -f test-build.xml
[root@localhost build]# ant -f test-build.xml test2
这里写图片描述

3、property功能,可以在project中也可以在target中,这个是全局和局部的区别。

<property name="addr" value="zhejiang hangzhou binjiangqu" /><target name="test-property">    <property name="name" value="zhangsan" />    <property name="age" value="25" />    <echo message="this is target test2." />    <echo message="java version: ${ant.java.version}" />    <echo message="project name: ${ant.project.name}" />    <echo message="ant file: ${ant.file}" />    <echo message="name = ${name}, age = ${age}, addr = ${addr}" /></target>

[root@localhost build]# ant -f test-build.xml test-property
这里写图片描述

4、拷贝文件
新建一个文件夹:[root@localhost build]# mkdir file
新建一个文件:[root@localhost file]# vi test.txt
源文件夹和文件必须存在,目标文件夹可以没有,会自动创建

    <target name="test-copyfile">          <copy file="file/test.txt" tofile="filebak/test.txt" />      </target>

[root@localhost build]# ant -f test-build.xml test-copyfile
这里写图片描述

5、拷贝文件夹
创建目标文件夹:[root@localhost build]# mkdir filebak2
源文件夹和文件必须存在,目标文件夹可以没有,会自动创建

<target name="test-copydir">      <copy todir="filebak2">          <fileset dir="file" />      </copy>  </target>

[root@localhost build]# ant -f test-build.xml test-copydir
这里写图片描述

6、创建文件夹

<target name="test-mkdir">      <mkdir dir="test" />  </target>

[root@localhost build]# ant -f test-build.xml test-mkdir
这里写图片描述

7、删除文件

<target name="test-delfile">      <delete file="filebak2/test.txt" />  </target> 

[root@localhost build]# ant -f test-build.xml test-delfile
这里写图片描述

8、删除文件夹

<target name="test-deldir">      <delete dir="filebak2" />  </target> 

[root@localhost build]# ant -f test-build.xml test-deldir
这里写图片描述

9、依赖
depends=”” 依赖的名字

<target name="echo1" >      <echo message="service is run ......" /> </target>  <target name="echo" depends="echofirst">      <echo message="service is running ......" /> </target>

[root@localhost build]# ant -f test-build.xml echo2
这里写图片描述

完整代码

<?xml version="1.0"?><project name="MyBuild" default="test" basedir="/data/ant/build">    <target name="test">        <echo message="test echo messsage, basedir=${basedir}" />    </target>     <target name="test2">        <echo message="test2 echo messsage, basedir=${basedir}" />    </target>    <property name="addr" value="zhejiang hangzhou binjiangqu" />    <target name="test-property">        <property name="name" value="zhangsan" />        <property name="age" value="25" />        <echo message="this is target test2." />        <echo message="java version: ${ant.java.version}" />        <echo message="project name: ${ant.project.name}" />        <echo message="ant file: ${ant.file}" />        <echo message="name = ${name}, age = ${age}, addr = ${addr}" />    </target>    <target name="test-copyfile">        <copy file="file/test.txt" tofile="filebak/test.txt" />    </target>    <target name="test-copydir">        <copy todir="filebak2">            <fileset dir="file" />        </copy>    </target>    <target name="test-mkdir">        <mkdir dir="test" />    </target>    <target name="test-delfile">        <delete file="filebak2/test.txt" />    </target>    <target name="test-deldir">        <delete dir="filebak2" />    </target>    <target name="echo1" >        <echo message="service is run ......" />    </target>    <target name="echo2" depends="echo1">            <echo message="service is running ......" />    </target></project>

以上这些是最基础的部分,下一篇说明一些高级操作

原创粉丝点击