Basis-ANT

作者: afewnotes | 来源:发表于2018-06-23 16:32 被阅读0次

安装

JDK 要求 1.4 及以上版本,推荐 1.7 及以上版本
确保 JAVA_HOME 已配置

  • 下载二进制版压缩包、解压
  • 配置 ANT_HOME 为 Ant 主目录;同时将 bin 目录添加到 PATH
  • 检查安装,命令行输入 ant,输出如下:
Buildfile: build.xml does not exist!
Build failed

使用

语法

Ant 的构建文件以 XML 文件编写。构建文件包含包含一个 project 和至少一个(默认的)target。targets 包含 task 元素,每个 task 元素包含 id 属性以便后续使用。task 元素即是一些执行动作,整个构建则由一系列的执行动作以及必要的属性配置构成。全局属性通过 property 元素配置。

  • property 也是一个 task,用来设置 键值对 形式的数据,便于重用设置
    <property name="NAME" location="VALUE"/>
    

1.6 之后所有 task 都可放到任何 target 外部声明;
当 task 处于 target 外部时,会在任何 target 执行之前计算

  • target 基本语法
    <target name="A"/>
    <target name="B" depends="A"/>
    <target name="C" depends="B"/>
    <target name="D" depends="C,B,A"/>

    执行顺序 A -> B -> C ->D
  • task 基本语法:
<taskName attributeName="value" ... />
<!--
    构建时,可指定需要构建的 target,
    如果未指定,则使用 default 配置的 target ,此处为 'dist'
 -->
<project name="MyProject" default="dist" basedir=".">
  <!-- 描述 -->
  <description>
    simple example build file
  </description>

  <!--
     本构建的全局属性
    - 名称值大小写敏感
    - 可在 task 中通过 '${' 和 '}' 使用这些属性,例如 ${src}
     -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>

  <!--
    一个 project 可包含多个 target。
    target : 需要执行的 task 的集合
   -->
  <target name="init">
    <!--
    task 就是一些可执行的实际动作,比如这里的 创建时间戳 <tstamp> ,创建文件夹 <mkdir> 等等
    -->
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <!-
    一个 target 可依赖其他的 target,比如此处的 compile 依赖于 init
    -->
  <target name="compile" depends="init"
        description="compile the source">
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution">
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.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>

以上示例包含几个 target 任务,默认任务为 dist

  • init 初始化动作,创建了一个 build 目录用于编译
  • compile 编译动作,将 src 目录的源码编译到 build 目录中,依赖于 init 任务
  • dist 打包发布,创建 dist/lib 目录,生成 jar 包,依赖于 compile 任务
  • clean 清理使用的目录

路径处理/ Path

编译 Java 代码时,通常会指定依赖的 jar 包,ANT 中主要有两种方式配置

  • 通过全局设置 <path> 指定路径,然后在 <javac> 任务中引用该配置,可在多个任务中共享
 <path id="project.class.path">
    <pathelement location="lib/"/>
    <pathelement path="${java.class.path}/"/>
    <pathelement path="${additional.path}"/>
  </path>

  <target ... >
    <javac ...>
      <classpath refid="project.class.path"/>
    </javac>
  </target>
  • 直接在 <javac> 任务中指定路径
<target ... >
    <javac ...>
      <classpath>
        <pathelement location="lib/"/>
        <pathelement path="${java.class.path}/"/>
        <pathelement path="${additional.path}"/>
      </classpath>
    </javac>
  </target>

<classpath> 只在 javac 等其他任务中作为参数,并不是一个 task

资源 Resource

在 Ant 中,文件/文件夹被抽象为 [资源](http://ant.apache.org/manual/Types/
resources.html),便于文件访问。

内置的资源类型:

  • resource 基础资源类型,其他资源类型都基于此类型
  • file 代表可通过本地文件系统访问的文件
  • javaresource 可通过 Java 类加载器加载的资源
  • javaconstant 获取 Java 常量值,该常量签名必须为 public static
  • zipentry 代表 ZIP 压缩文件中的某个文件
  • tarentry 代表 TAR 压缩文件中的某个文件
  • gzipresource 非标准资源,为其他资源提供即时压缩的一层封装
  • bzip2resource 非标准资源,为其他资源提供即时压缩的封一层封装
  • url 代表 URL
  • string 代表 Java 字符串
  • propertyresource 代表 Ant 属性

资源集合 Resource Collections

包含资源的资源组

分类:

内置的资源集合

有点类似 SQL 查询操作

  • resources 普通资源集合
  • files 与 fileset 类似的文件集合
  • restrict 根据指定条件限制包含的资源
  • sort 根据自然排序或指定排序器,将资源排序
  • first 获取第一个资源,可与 sort 结合使用,排序查找资源
  • last 获取最后一个资源
  • allbutfirst 除第一个以外的所有资源
  • allbutlast 出最后一个以外的所有资源
  • tokens 包含从资源中的 string 标记,可用来实现 Unix 函数(如 sort, grep 等)
  • 设置 Set operations
    • union 取指定资源集合的并集
    • intersect 取指定资源集合的交集
    • difference 取指定资源集合的差集
  • mappedresources 封装另一个资源集合并使用 mapper 映射内部资源
  • archives 接受任意数量的资源并假定它们是 ZIP 或 TAR 压缩文件。返回资源的内容
  • resourcelist 接受任意数量的资源,读取这些资源并返回一个行读取的资源

文件过滤

通过对资源、资源集合的操作,对构建的文件过滤、筛选;
以 fileset 、dirset 为例

<classpath>
  <pathelement path="${classpath}"/>
  <!-- 此处只包含 lib 目录下的 jar 文件 -->
  <fileset dir="lib">
    <include name="**/*.jar"/>
  </fileset>
  <pathelement location="classes"/>
  <!-- 此处包含 build 目录下的 classes 目录,同时不包含名称中含有 Test 的文件 -->
  <dirset dir="${build.dir}">
    <include name="apps/**/classes"/>
    <exclude name="apps/**/*Test*"/>
  </dirset>
  <filelist refid="third-party_jars"/>
</classpath>

项目实例

  • 背景
    Java Web 项目,通过 Jenkins 配置自动构建,同时调用 ant 脚本进行项目构建打包
  • 目的
    • 设置依赖库
    • 创建构建目录
    • 拷贝静态文件(页面、图片等不需编译文件,包括依赖库)到构建目录
    • 替换自定义配置的 jdbc 文件
    • 编译源代码
    • 生成 war 包
<?xml version="1.0" encoding="UTF-8" ?>
<project name="MyProject" default="dist" basedir=".">
        <description>
                simple example build file
        </description>

        <property environment="SystemVariable" />

        <!-- set global properties for this build -->
        <property name="app" location="appFolder"></property>
        <property name="webapp.name" value="appName"></property>
        <property name="src" location="${app}/src"></property>
        <property name="build" location="build"></property>
        <property name="dist" location="dist"></property>
        <property name="libs" location="${app}/WebRoot/WEB-INF/lib"></property>

        <property name="tomcat.home" value="/opt/tomcat7"></property>
        <property name="java.home" value="${SystemVariable.JAVA_HOME}"></property>

        <path id="classpath">

                <fileset dir="${tomcat.home}/lib" >
                        <include name="*.jar"/>
                </fileset>
                <fileset dir="${tomcat.home}/bin" >
                        <include name="*.jar"/>
                </fileset>
                <fileset dir="${libs}">
                        <include name="*.jar"/>
                </fileset>
        </path>

        <target name="clean" description="clean up">
                <!-- delete the ${build} and ${dist} directory trees -->
                <delete dir="${build}"/>
        </target>

        <target name="init" depends="clean">
                <!-- create the time stamp -->
                <tstamp></tstamp>
                <!-- create the build director structure used by compile -->
                <mkdir dir="${build}" />
            <mkdir dir="${build}\WEB-INF"/>
                <mkdir dir="${build}\WEB-INF\classes"/>
                <mkdir dir="${build}\WEB-INF\lib"/>

                <copy todir="${build}">
                        <fileset dir="${app}\WebRoot">
                                <include name="WEB-INF/**" />
                                <include name="**" />
                        </fileset>
                </copy>

                <copy todir="${build}\WEB-INF\classes">
                        <fileset dir="${src}">
                                <include name="**/*.xml"/>
                                <include name="**/*.properties"/>
                        </fileset>
                </copy>

                <copy file="../jdbc.properties" tofile="${build}\WEB-INF\classes\jdbc.properties" overwrite="true">
                </copy>
        </target>

        <target name="compile" depends="init" description="compile the source">
                <!-- compile the java code from ${src} into ${build} -->
                <!-- <compilerarg line="-encoding UTF-8 "/>    -->
                <javac srcdir="${src}" destdir="${build}/WEB-INF/classes"  includeAntRuntime="false" debug="true"
                        classpathref="classpath" encoding="UTF-8"  />
        </target>

        <target name="dist" depends="compile" description="generate the distribution">
                <!-- create the distribution directory -->
                <mkdir dir="${dist}" />

                <war destfile="${dist}/${webapp.name}.war" webxml="${build}/WEB-INF/web.xml" >
                        <!-- <lib dir="${build}/WEB-INF/lib" >
                                <include name="**"/>
                        </lib>
                        <classes dir="${build}/WEB-INF/classes"/> -->
                        <fileset dir="${build}"/>
                </war>
        </target>
</project>

运行

Ant 提供两种方式运行:

  • 命令行
ant [options] [target [target2 [target3] ...]]

详细参数可使用 ant -h 查看
示例:
- ant 默认当前文件包含 build.xml,运行默认任务
- ant -buildfile anotherName.xml ,指定配置文件,运行默认任务
- ant -buildfile anotherName.xml dist,指定配置文件,运行指定任务

  • Java 调用
// 传统调用入口
java -Dant.home=c:\ant org.apache.tools.ant.Main [options] [target]
// 1.6 后引入
java -Dant.home=c:\ant org.apache.tools.ant.launch.Launcher [options] [target]

Ant 也可以通过 <java> 命令启动

```
<java
        classname="org.apache.tools.ant.launch.Launcher"
        fork="true"
        failonerror="true"
        dir="${sub.builddir}"
        timeout="4000000"
        taskname="startAnt">
    <classpath>
        <pathelement location="${ant.home}/lib/ant-launcher.jar"/>
    </classpath>
    <arg value="-buildfile"/>
    <arg file="${sub.buildfile}"/>
    <arg value="-Dthis=this"/>
    <arg value="-Dthat=that"/>
    <arg value="-Dbasedir=${sub.builddir}"/>
    <arg value="-Dthe.other=the.other"/>
    <arg value="${sub.target}"/>
</java>
```

对比两种方式,命令行更加方便,且可与其他工具配合使用。

相关文章

  • Basis-ANT

    安装 JDK 要求 1.4 及以上版本,推荐 1.7 及以上版本确保 JAVA_HOME 已配置 下载二进制版压缩...

网友评论

    本文标题:Basis-ANT

    本文链接:https://www.haomeiwen.com/subject/syfdyftx.html