原文:https://blog.csdn.net/mevicky/article/details/72828554

前言
国内关于build.xml的配置资料太零散了,实在是受不了,故而将自己的笔记整理成博文,方便大家查阅和理解。

build.xml配置参数
构建文件默认叫build.xml,其有很多配置参数。

project
每个构建文件都有一个project标签,有以下属性:
- default:表示默认的运行目标,这个属性是必须的。
- basedir:表示项目的基准目录。
- name:表示项目名。
- description:表示项目的描述。

如下:

每个项目对应一个构建文件,但是如果项目比较复杂,业务线比较多,则有可能对应很多个构建文件,比如:

这时我们需要注意,每个构建文件都需要以project标签包含起来。

property

类似于常量,可以供给build.xml中的其他标签使用。有两个特点:
    - 大小写敏感
    - 不可改变,谁先设定,之后的都不能改变。

该标签可以与多个属性配合使用。
    - name和value:

<property name="module_name" value="admin"/> 

后面直接使用即可:

<echo message="begin nej-build ${module_name}..."/> 

- name和refid:

<property name="srcpath" refid="dao.compile.classpath"/> 

其中的dao.compile.classpath在别的地方进行了定义。当然,也可以通过直接引用的方式:

<property name="baseline.dir" value="${ob_baseline.dir}"/> 

- name和location:

<property name="srcdir" location="src"/> 

将srcdir的值设置为当前文件路径/src。
- file:

<property file="./omad/build.properties"/> 

导入相对文件中的所有变量,这里的build.properties专门用来存放各种变量,示例如下:

url:

<property url="http://www.mysite.com/bla/props/foo.properties"/> 

导入对应文件的属性

environment:

<property environment="env"/> 

设置系统的环境变量前缀为env。比如

<property name="tomcat.home" value="${env.CATALINA_HOME}"/> 

将系统的tomcat安装目录设置到tomcat.home属性中。

import
引入别的xml文件,提高复用性:

<import file="./env-judge.xml"/>
<import file="./tasks.xml"/>

甚至可以批量匹配:

<copy todir="${basedir}/src/html/${html.dir}" overwrite="true" includeEmptyDirs="true">
<fileset dir="${basedir}/lib">
<include name="module-*/**" />
</fileset>
</copy>

target
任务,一个project标签下有一个或多个target标签,代表任务,任务间可以存在依赖关系。有如下属性:
- name:用于标识,这个是必须的
- depends:用来指定所依赖的任务。

<!-- 初始化任务 -->
<target name="init">
<echo message=" init ${init} ..."/>
</target> <!-- 编译 -->
<target name="compile" depends="init">
<delete dir="${classes.dir}" />
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}">
<classpath refid="master-classpath" />
</javac>
</target>
1 if:当属性设置时才执行该任务。
<target name="sync_module_k12_teach" if="${is_k12_teach}">
<antcall target="sync_module_item">
<param name="html.dir" value="org"/>
</antcall>
</target> <target name="sync_module_backend" if="${is_backend}">
<antcall target="sync_module_item">
<param name="html.dir" value="admin"/>
</antcall>
</target> <target name="sync_module_k12_backend" if="${is_k12_backend}">
<antcall target="sync_module_item">
<param name="html.dir" value="admin"/>
</antcall>
</target>

通过判断变量是否存在,执行不同的任务。
- unless:当属性未设置时才执行。
- description:任务描述。

echo
控制台显示

<echo message="begin clean res/module-xx、component-xx、res-base..."/>

delete
删除文件或文件目录,有如下属性
- file:删除文件
- dir:删除目录
- includeEmptyDirs:值得是否删除空目录,默认是true
- failonerror:报错是否停止,默认是true
- verbose:是否列出删除的文件,默认是false

示例如下:

<!--clean other dir-->
<target name="clean_other_dir">
<echo message="begin clean_other_dir..."/>
<delete dir="${basedir}/${compress.dir}"/>
<delete dir="${basedir}/pub"/>
<echo message="begin clean html module-xx..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/src/html" >
<include name="**/module-*/**"/>
</fileset>
</delete>
<echo message="begin clean res/module-xx、component-xx、res-base..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/res" >
<include name="module-*/**"/>
<include name="component-*/**"/>
<include name="res-base/**"/>
</fileset>
</delete>
</target>

mkdir
创建一个目录

<mkdir dir=”${class.root}”/>

copy
拷贝文件或文件目录,属性如下:
- file:表示源文件。
- tofile:表示目标文件。
- todir:表示目标目录。
- overwrite:是否覆盖目标文件,默认为false。
- includeEmptyDirs:是否拷贝空目录,默认为true。
- failonerror:如目标没有发现是否自动停止,默认值true。
- verbose:是否显示详细信息,默认值false。

示例:

<target name="cp">
<copy todir="${compress.dir}" overwrite="true">
<fileset dir="${ob_baseline.dir}">
<include name="pub/" />
<include name="res/" />
<include name="mail_template/" />
</fileset>
</copy>
</target>

fileset
文件集标签,通常与任务结合来使用,例如上面的copy的demo中,通过将fileset定义的文件路径下的文件,拷贝到todir指定的路径中。
也可以用于批量删除:

<delete includeemptydirs="true">
<fileset dir="${basedir}/src/html" >
<include name="**/module-*/**"/>
</fileset>
</delete>
<echo message="begin clean res/module-xx、component-xx、res-base..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/res" >
<include name="module-*/**"/>
<include name="component-*/**"/>
<include name="res-base/**"/>
</fileset>
</delete>

也就是说,但凡遇到文件集操作,都需要用到fileset标签。

exec
用来执行系统命令,或者指定环境的命令。
比如:

<target name="test">
<exec executable="cmd.exe">
<arg line="/c dir"/>
</exec>
</target>

打开命名行,并转到c盘执行dir命令。

能够执行系统命令,就相当于可以执行各种环境比如node、gulp、bower等等:

<!--build style-->
<target name="build_style">
<echo message="begin build_style..."/>
<exec dir="." executable="gulp" failonerror="true">
<arg line="scss"/>
</exec>
</target> <!--bower cache clean if必须是${]才是判断true,false, 否则只要有设定值即可执行-->
<target name="bower_cache_clean" if="${is_bower_cache_clean}">
<echo message="begin bower_cache_clean ..."/>
<exec dir="." executable="bower" failonerror="true">
<arg line="cache clean" />
</exec>
</target>

antcall
执行某个定义的任务。

<target name="sync_module_teach" if="${is_teach}">
<antcall target="sync_module_item">
<param name="html.dir" value="org"/>
</antcall>
</target>

执行sync_module_item任务,并设置参数html.dir的值为org。
该任务定义如下:

<target name="sync_module_item">
<echo message="begin sync_module ${html.dir}..."/>
<copy todir="${basedir}/src/html/${html.dir}" overwrite="true" includeEmptyDirs="true">
<fileset dir="${basedir}/lib">
<include name="module-*/**" />
</fileset>
</copy>
</target>

或者更为简单的表达:

<target name="deploy">
<echo message="begin auto deploy......"/>
<antcall target="clean"/>
<antcall target="bower_install"/>
<antcall target="cnpm_install"/>
<antcall target="sync_module"/>
<antcall target="build_style"/>
<antcall target="nej_build" />
<antcall target="cp"/>
</target>

parallel
并行执行多个子任务。

<parallel failonany="true">
<antcall target="sync_module_corp"/>
<antcall target="sync_module_main"/>
<antcall target="sync_module_teach"/>
<antcall target="sync_module_backend"/>
<antcall target="sync_module_passport"/>
<antcall target="sync_module_business"/>
<antcall target="sync_module_k12_teach"/>
<antcall target="sync_module_k12_backend"/> <antcall target="build_style"/>
</parallel>

通过failonany控制如果一个失败,则不执行。通过并行执行,来提升性能,降低构建花费的时间。

regexp
用于正则的定义的使用,可以与matches结合使用。
比如,定义正则:

<regexp id="regexp_env_test" pattern="^${root_dir}/(${test_dir}|${test_k12_dir})/.+"/>
<regexp id="regexp_env_pre" pattern="^${root_dir}/(${pre_dir}|${pre_k12_dir})/.+"/>

通过pattern指定正则内容,通过id标识。
在需要匹配的时候,使用之:

<condition property="is_test">
<matches string="${basedir}">
<regexp refid="regexp_env_test"/>
</matches>
</condition>

condition
用来判断,如果包含的内容符合条件,则将property指定的属性设置为true,否则为false。
比如上面的例子中,就是将basedir变量的值和regexp_env_test对应的正则匹配,如果正确,就将is_test设置为true,然后后面的流程再去判断。
与之配合的标签有很多,下面一一介绍:
- istrue,isfalse:断言

<condition property="is_test_backend">
<and>
<istrue value="${is_test}"/>
<istrue value="${is_backend}"/>
</and>
</condition>

只有is_test和is_backend变量的值均为true,is_test_backend的值才为true。
- and:逻辑与,需要都满足条件才行,如上例所述。
- not:逻辑非,反过来的结果。
- or,xor:逻辑或和逻辑异或。
- isset:指定属性是否存在:

<condition property="scondition">
<!--如果属性name不存在则返回false-->
<isset property="name"/>
</condition>

equils:指定属性是否相等:

<condition property="scondition">
<!--如果arg1的值与arg2的值相等返回true,否则为false-->
<equals arg1="${name}" arg2="this is name"/>
</condition>

filesmatch:指定文件是否相等:

<condition property="scondition">
<!--如果file1所代表的文件与file2所代表的文件相等返回true,否则为false-->
<filesmatch file1="testfile1.txt" file2="testfile2.txt"/>
</condition>

Ant之build.xml配置详解【转】的更多相关文章

  1. ant+jmeter中build.xml配置详解

  2. java web.xml配置详解(转)

    源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...

  3. web.xml配置详解之listener

    web.xml配置详解之listener 定义 <listener> <listener-class>nc.xyzq.listener.WebServicePublishLis ...

  4. Spring 入门 web.xml配置详解

    Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...

  5. tomcat中server.xml配置详解(转载)(一)

    转载自:https://www.cnblogs.com/starhu/p/5599773.html tomcat中server.xml配置详解 Tomcat Server的结构图如下:(该文件描述了如 ...

  6. Web.xml配置详解(转)

    Web.xml配置详解 Posted on 2010-09-02 14:09 chinaifne 阅读(295105) 评论(16) 编辑 收藏 1 定义头和根元素 部署描述符文件就像所有XML文件一 ...

  7. SpringBoot—整合log4j2入门和log4j2.xml配置详解

    关注微信公众号:CodingTechWork,一起学习进步. 引言   对于一个线上程序或者服务而言,重要的是要有日志输出,这样才能方便运维.而日志的输出需要有一定的规划,如日志命名.日志大小,日志分 ...

  8. Tomcat中的Server.xml配置详解

    Tomcat中的Server.xml配置详解 Tomcat Server的结构图如下: 该文件描述了如何启动Tomcat Server <Server> <Listener /> ...

  9. Log4j2详解——XML配置详解

    Log4j2详解--XML配置详解 找到了个很详细的文章链接 https://www.jianshu.com/p/bfc182ee33db

随机推荐

  1. Eclipse 无输出,但不报错

    解决方法: 若界面中都没有console选项,则 工具栏 Window - Show View - Console Window - Preferences - Run/Debug - Console ...

  2. 20165235 2018-3 《Java程序设计》第5周学习总结

    20165235 2018-3 <Java程序设计>第5周学习总结 教材学习内容总结 第六章 内部类与异常类 (一)内部类:1.java支持在一个类中定义另一个类,这个类叫内部类.2.内部 ...

  3. html-伪类

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Dataset:利用Python将已有mnist数据集通过移动像素上下左右的方法来扩大数据集为初始数据集的5倍—Jason niu

    from __future__ import print_function import cPickle import gzip import os.path import random import ...

  5. 【python】函数式编程

    No1: 函数式编程:即函数可以作为参数传递,也可以作为返回值  No2: map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的 ...

  6. SQL语句中单引号、双引号和反引号的区分

    反引号 反引号:反引号一般在Esc键的下方,为了区分MySQL的保留字与普通字符而引入的符号. 一般我们建表时都会将表名,库名都加上反引号来保证语句的执行度. 例如: SELECT * FROM `u ...

  7. Gym 100342J Triatrip (求三元环的数量) (bitset优化)

    <题目链接> 题目大意:用用邻接矩阵表示一个有向图,现在让你求其中三元环的数量. 解题分析:先预处理得到所有能够直接到达每个点的集合$arrive[N]$和所有能够由当前点到达的集合$to ...

  8. HDU3339 In Action 【最短路】+【01背包】

    <题目链接> 题目大意: 给出一个0-n组成的图,1-n的点上分布着值为pow的电站,给出图的m条边以及距离,从0出发到n个点中的x个点的行走距离和最小(因为是每炸一个点派出一辆坦克),且 ...

  9. type__字符串

  10. 按字典序依次打印只由1~n组成的n位数

    //我的dfs入门.将1~n一次填入数组然后打印. #include<stdio.h> #include<string.h> ]; ]; void dfs(int,int); ...