Ant build.xml的一个示例

来源:互联网 发布:淘宝宝贝如何优化 编辑:程序博客网 时间:2024/05/23 01:12

概述

  ant是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。在实际软件开发中,有很多地方可以用到ant。但是目前使用更多一些的是Maven、groovy等。本文仅是作为学习的笔记方便以后查阅。

优点

ant 是Apache软件基金会JAKARTA目录中的一个子项目,它有以下的优点:
1、跨平台性:Ant是纯Java语言编写的,所以具有很好的跨平台性。
2、操作简单:Ant是由一个内置任务和可选任务组成的,用ant任务就像是在dos中写命令行一样。Ant运行时需要一个XML文件(构建文件)。 Ant通过调用target树,就可以执行各种task。每个task实现了特定接口对象。
3、维护简单、可读性好、集成简单:由于Ant构建文件 时XML格式的文件,所以很容易维护和书写,而且结构很清晰。Ant可以集成到开发环境中。由于Ant的跨平台性和操作简单的特点,它很容易集成到一些开发环境中去。

demo

下面是一个配置示例:
build.properties

userweb.root.dir=/opt/demo/userjre.dir=/opt/Java/jdk1.7.0_79web.lib.home=/opt/lib/syldb.dir=/opt/work/user/20160825

build.xml

<?xml version="1.0" encoding="UTF-8"?><project name="user" default="all">    <!-- 配置文件 -->    <property file="build.properties" />    <!-- 时间戳格式 -->    <tstamp>        <format property="date" pattern="yyyyMMdd" />    </tstamp>    <!--基础目录定义 -->    <property name="userweb.dir" value="${userweb.root.dir}" />    <property name="jre.dir" value="${jre.dir}" />    <property name="db.dir" value="${db.dir}" />    <property name="web.lib.dir" value="${web.lib.home}" />    <property name="base.tmp" location="${userweb.dir}/tmp/" />    <property name="base.target" location="${base.tmp}/target/" />    <property name="base.build" location="${base.tmp}/build/" />    <property name="base.db" location="${base.build}/db/" />    <property name="base.user" location="${base.build}/user/" />    <property name="base.web.inf" location="${base.user}/WEB-INF/" />    <property name="base.class" location="${base.user}/WEB-INF/classes" />    <!-- 初始化 -->    <target name="init" description="Build initialization" depends="clean">        <echo message="Begin to init environment ... " />        <mkdir dir="${base.tmp}" />        <mkdir dir="${base.build}" />        <mkdir dir="${base.db}" />        <mkdir dir="${base.user}" />        <mkdir dir="${base.web.inf}" />        <mkdir dir="${base.class}" />        <mkdir dir="${base.target}" />        <echo message="End to init environment ... " />    </target>    <!-- 清理工程 -->    <target name="clean" description="Build initialization">        <echo message="Begin to clean ... " />        <delete dir="${base.tmp}" />        <echo message="End to clean ... " />    </target>    <!-- 编译字节码路径 -->    <path id="user.classpath">        <fileset dir="${jre.dir}" />        <fileset dir="${web.lib.dir}" />    </path>    <!-- 源码路径 -->    <path id="user.sourcepath">        <dirset dir="${userweb.dir}">            <include name="src/com/user/mapping" />            <include name="resources" />        </dirset>    </path>    <!-- 编译 -->    <target name="compile">        <echo message="Begin to compile src ..." />        <javac srcdir="${userweb.dir}/src" destdir="${base.class}/"            includeAntRuntime="false" encoding="UTF-8" debug="true">            <classpath refid="user.classpath" />        </javac>        <copy todir="${base.class}">            <fileset dir="${userweb.dir}/resources">                <type type="file" />            </fileset>        </copy>        <copy todir="${base.class}/com/user/mapping">            <fileset dir="${userweb.dir}/src/com/user/mapping" />        </copy>        <echo message="End to compile src" />    </target>    <!-- 构建工程 -->    <target name="build">        <echo message="Begin to build user ..." />        <!--        <copy todir="${base.web.inf}/lib">            <fileset dir="${web.lib.dir}" />        </copy>        -->        <copy file="${userweb.dir}/WebContent/WEB-INF/web.xml" tofile="${base.web.inf}/web.xml" />        <copy todir="${base.user}">            <fileset dir="${userweb.dir}/WebContent">                <exclude name="${userweb.dir}/WebContent/WEB-INF" />            </fileset>        </copy>        <copy todir="${base.db}">            <fileset dir="${db.dir}" />        </copy>        <echo message="End to build user" />    </target>    <!-- 打包 -->    <target name="zip">        <zip destfile="${base.target}/user-${date}.zip">            <fileset dir="${base.build}" />        </zip>    </target>    <target name="all" depends="init, compile, build, zip" description="build all"/></project>
0 0
原创粉丝点击