基础basic.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <property file="build.properties"/>

<property name="src.dir" location="src"/>
    <property name="test.src.dir" location="test"/>
    <property name="web.dir" location="web"/>
    <property name="sql.dir" location="sql"/>

<property name="web.WEB-INF.dir" location="${web.dir}/WEB-INF"/>
    <property name="lib.dir" location="${web.WEB-INF.dir}/lib"/>
    <property name="run.test.className" value="**/Test*.class"/>

<property name="build.dir" location="build"/>
    <property name="build.src.dir" location="${build.dir}/src"/>
    <property name="build.src.classes.dir" location="${build.dir}/classes"/>
    <property name="build.test.dir" location="${build.dir}/test"/>
    <property name="build.test.src.dir" location="${build.test.dir}/src"/>
    <property name="build.test.classes.dir" location="${build.test.dir}/classes"/>
    <property name="build.test.report" location="${build.test.dir}/report"/>
    <property name="build.jar.dir" location="${build.dir}/dist"/>
    <property name="build.zip.dir" location="${build.dir}/zip"/>
    <property name="build.doc.dir" location="${build.dir}/doc"/>
    <property name="metadata" location="metadata"/>

<property environment="env"></property>

<path id="compile-path">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>

<path id="compile-test-path">
        <path refid="compile-path"/>
        <pathelement location="${build.src.classes.dir}"/>
    </path>

<path id="run-test">
        <path refid="compile-test-path"/>
        <pathelement location="${build.test.classes.dir}"/>
    </path>

<macrodef name="cleanProject">
        <attribute name="cleanDir" default="${build.dir}"/>
        <element name="cleanTask" optional="yes"/>
        <sequential>
            <echo>项目清理</echo>
            <delete dir="@{cleanDir}"/>
            <cleanTask/>
        </sequential>
    </macrodef>

<macrodef name="initDir">
        <attribute name="build.dir" default="${build.dir}"/>
        <attribute name="build.src.dir" default="${build.src.dir}"/>
        <attribute name="build.src.classes.dir" default="${build.src.classes.dir}"/>
        <attribute name="build.test.dir" default="${build.test.dir}"/>
        <attribute name="build.test.src.dir" default="${build.test.src.dir}"/>
        <attribute name="build.test.classes.dir" default="${build.test.classes.dir}"/>
        <attribute name="build.test.report" default="${build.test.report}"/>
        <attribute name="build.jar.dir" default="${build.jar.dir}"/>
        <attribute name="build.zip.dir" default="${build.zip.dir}"/>
        <attribute name="build.doc.dir" default="${build.doc.dir}"/>
        <attribute name="metadata" default="${metadata}"/>

<element name="initTask" optional="yes"/>
        <sequential>
            <echo>项目目录初始化</echo>
            <mkdir dir="@{build.dir}"/>
            <mkdir dir="@{build.src.dir}"/>
            <mkdir dir="@{build.src.classes.dir}"/>
            <mkdir dir="@{build.test.dir}"/>
            <mkdir dir="@{build.test.src.dir}"/>
            <mkdir dir="@{build.test.classes.dir}"/>
            <mkdir dir="@{build.test.report}"/>
            <mkdir dir="@{build.jar.dir}"/>
            <mkdir dir="@{build.zip.dir}"/>
            <mkdir dir="@{build.doc.dir}"/>
            <mkdir dir="@{metadata}"/>
            <initTask/>
        </sequential>
    </macrodef>

<macrodef name="compileSrcFile">
        <attribute name="from.src.dir" default="${src.dir}"/>
        <attribute name="to.dest.dir" default="${build.src.classes.dir}"/>
        <attribute name="class.path.ref" default="compile-path"/>
        <attribute name="failonerror" default="true"/>

<element name="compileTask" optional="yes"/>
        <sequential>
            <echo>编译源文件</echo>
            <javac failonerror="@{failonerror}"
                   includeantruntime="false"
                   srcdir="@{from.src.dir}"
                   destdir="@{to.dest.dir}"
                   classpathref="@{class.path.ref}"/>
            <compileTask/>
        </sequential>
    </macrodef>

<macrodef name="compileTestSrcFile">
        <attribute name="from.test.src.dir" default="${test.src.dir}"/>
        <attribute name="to.test.dest.dir" default="${build.test.classes.dir}"/>
        <attribute name="class.path.ref" default="compile-test-path"/>
        <attribute name="failonerror" default="true"/>
        <element name="compileTestTask" optional="yes"/>
        <sequential>
            <echo>编译测试文件</echo>
            <javac failonerror="@{failonerror}"
                   includeantruntime="false"
                   srcdir="@{from.test.src.dir}"
                   destdir="@{to.test.dest.dir}"
                   classpathref="@{class.path.ref}"/>
            <compileTestTask/>
        </sequential>
    </macrodef>

<macrodef name="runTest">
        <attribute name="printsummary" default="false"/>
        <attribute name="failureproperty" default="junit.fail"/>
        <attribute name="haltonfailure" default="false"/>
        <attribute name="runClasspath" default="run-test"/>
        <attribute name="fork" default="true"/>
        <!-- 命令行输出测试信息 brief主要信息 -->
        <!-- <attribute name="formatter" default="brief" /> -->
        <!--测试类信息格式-->
        <attribute name="formatter" default="xml"/>
        <!--测试类信息使用文件存储-->
        <attribute name="usefile" default="true"/>
        <!--测试类报告输出位置-->
        <attribute name="report.to.dir" default="${build.test.report}"/>
        <!--测试类来自哪个文件夹-->
        <attribute name="from.test.class.dir" default="${build.test.classes.dir}"/>
        <!--测试哪些类-->
        <attribute name="include.run.TestClass" default="${run.test.className}"/>
        <!--从哪个dir中找报告-->
        <attribute name="from.report.dir" default="${build.test.report}"/>
        <!--格式化哪些测试类结果xml文件-->
        <attribute name="report.file" default="TEST-*.xml"/>
        <!--报告采用哪种格式-->
        <attribute name="fortmatType" default="frames"/>
        <!--格式化后报告输出位置-->
        <attribute name="to.dir" default="${build.test.report}/html"/>
        <element name="runTestTask" optional="yes"/>
        <sequential>
            <echo>运行单元测试</echo>
            <junit failureproperty="@{failureproperty}" fork="@{fork}"
                   printsummary="@{printsummary}"
                   haltonfailure="@{haltonfailure}">
                <classpath refid="@{runClasspath}"/>
                <formatter type="@{formatter}" usefile="@{usefile}"/>
                <batchtest todir="@{report.to.dir}">
                    <fileset dir="@{from.test.class.dir}" includes="@{include.run.TestClass}"/>
                </batchtest>
            </junit>
            <echo>生成报告</echo>
            <junitreport todir="@{report.to.dir}">
                <!--从哪个dir中找报告-->
                <fileset dir="@{from.report.dir}" includes="@{report.file}"/>
                <!--格式化好的报告文件输出位置-->
                <report format="@{fortmatType}" todir="@{to.dir}"/>
            </junitreport>
            <fail if="${failureproperty}" message="单元测试失败具体查看${build.test.report}"/>
            <runTestTask/>
        </sequential>
    </macrodef>

<macrodef name="docFiles">
        <attribute name="src.from.dir" default="${src.dir}"/>
        <attribute name="use" default="true"/>
        <attribute name="packagename" default="com.*"/>
        <attribute name="charset" default="UTF-8"/>
        <attribute name="fileEncoding" default="UTF-8"/>
        <attribute name="docEncoding" default="UTF-8"/>
        <attribute name="to.dir" default="${build.doc.dir}"/>
        <attribute name="classpath" default="compile-path"/>
        <element name="docFilesTask" optional="yes"/>

<sequential>
            <echo>生成文档</echo>
            <javadoc sourcepath="@{src.from.dir}"
                     use="@{use}"
                     packagenames="@{packagename}"
                     charset="@{charset}"
                     encoding="@{fileEncoding}"
                     docencoding="@{docEncoding}"
                     destdir="@{to.dir}"
                    >
                <classpath refid="@{classpath}"/>

</javadoc>
            <docFilesTask/>
        </sequential>
    </macrodef>

<macrodef name="jarFile">
        <attribute name="dest" default="${build.jar.dir}/${project.jar.name}"/>
        <attribute name="basedir" default="${build.src.classes.dir}"/>
        <attribute name="buildBy" default="${user.name}"/>
        <attribute name="duplicate" default="preserve"/>
        <attribute name="package" default="null"/>
        <attribute name="Timeformat" default="yyyy-MM-dd HH:mm:ss"/>
        <element name="Task" optional="yes"/>
        <sequential>
            <echo>生成jar文件</echo>
            <tstamp prefix="build.">
                <format property="timeSign" pattern="@{Timeformat}"/>
            </tstamp>
            <jar destfile="@{dest}" duplicate="@{duplicate}">
                <fileset dir="@{basedir}" includes="**/@{package}/*"/>
                <manifest>
                    <attribute name="Build-BY" value="@{buildBy}"/>
                    <attribute name="Build-time" value="${build.timeSign}"/>
                </manifest>

</jar>
            <Task/>
        </sequential>
    </macrodef>

<macrodef name="copyFile">
        <attribute name="to.dir" default="${build.src.dir}"/>
        <attribute name="to.test.src.dir" default="${build.test.src.dir}"/>
        <attribute name="from.dir" default="${src.dir}"/>
        <attribute name="from.test.src.dir" default="${test.src.dir}"/>
        <attribute name="includeFiles" default="**/*.*"/>

<element name="Task" optional="yes"/>
        <sequential>
            <echo>拷贝src文件</echo>
            <copy todir="@{to.dir}">
                <fileset dir="@{from.dir}" includes="@{includeFiles}"/>
            </copy>
            <copy todir="@{to.test.src.dir}">
                <fileset dir="@{from.test.src.dir}" includes="@{includeFiles}"/>
            </copy>
            <Task/>
        </sequential>
    </macrodef>

<macrodef name="zipFile">
        <attribute name="duplicate" default="preserve"/>
        <attribute name="dest" default="${build.zip.dir}/${project.zip.name}"/>

<!--要压缩的来源jar文件-->
        <attribute name="includeJar" default="${build.jar.dir}/*"/>
        <!--压缩文件里的目录前缀-->
        <attribute name="prefixjar" default="${project.prefix}/jar"/>

<attribute name="includesrcdir" default="${build.src.dir}"/>
        <!--要包含的文件-->
        <attribute name="includesrc" default="**/*.*"/>
        <attribute name="prefixsrc" default="${project.prefix}/src"/>

<attribute name="includedocdir" default="${build.doc.dir}"/>
        <!--要包含的文件-->
        <attribute name="includedoc" default="**/*.*"/>
        <attribute name="prefixdoc" default="${project.prefix}/doc"/>

<attribute name="includetestdir" default="${build.test.dir}"/>
        <!--要排除的文件-->
        <attribute name="excludes" default="report/"/>

<attribute name="prefixtest" default="${project.prefix}/test"/>

<element name="Task" optional="yes"/>
        <sequential>
            <echo>压缩zip文件</echo>
            <zip destfile="@{dest}" duplicate="@{duplicate}">
                <zipfileset file="@{includeJar}" prefix="@{prefixjar}"/>
                <zipfileset dir="@{includesrcdir}" includes="@{includesrc}" prefix="@{prefixsrc}"/>
                <zipfileset dir="@{includedocdir}" includes="@{includedoc}" prefix="@{prefixdoc}"/>
                <zipfileset dir="@{includetestdir}" includes="**/" excludes="@{excludes}" prefix="@{prefixtest}"/>
            </zip>
            <Task/>
        </sequential>
    </macrodef>

<macrodef name="warFile">
        <attribute name="type" default="war" />
        <attribute name="destfile" default="${build.jar.dir}/${web.name}.war" />
        <attribute name="filefrom" default="${web.dir}" />
        <attribute name="include" default="**/*" />
        <element name="Task" optional="yes"/>
        <sequential>
            <echo>生成war包</echo>
            <war destfile="@{destfile}">
                <fileset dir="@{filefrom}" includes="@{include}"/>
            </war>
            <Task/>
        </sequential>
    </macrodef>

<macrodef name="deployWar">
        <attribute name="todir" default="${env.CATALINA_BASE}/webapps" />
        <attribute name="warfrom" default="${build.jar.dir}/${web.name}.war" />
        <element name="Task" optional="yes"/>
        <sequential>
            <echo>部署war包</echo>
            <copy  todir="@{todir}" file="@{warfrom}" />
            <Task/>
        </sequential>
    </macrodef>

<macrodef name="createdatabase">
        <attribute name="userid" default="${root.username}" />
        <attribute name="password" default="${root.password}" />
        <attribute name="driver" default="${databaseDriver}" />
        <attribute name="classpath" default="compile-test-path" />
        <attribute name="url" default="${mysql.url}" />
        <attribute name="transaction.src" default="${sql.dir}/create.sql" />
        <element name="Task" optional="yes"/>
        <sequential>
            <echo>创建测试数据库</echo>
                <sql classpathref="@{classpath}" driver="@{driver}" userid="@{userid}" password="@{password}" url="@{url}">
                        <transaction src="@{transaction.src}" />
                </sql>
            <Task/>
        </sequential>
    </macrodef>
</project>

属性文件 build.properties

ftp.username=zl
ftp.password=123456
ftp.server=localhost
ftp.dir=user_manager
project.version=SNAPSHOT_0.1
project.name=usermanager
project.prefix=${project.name}_${project.version}
project.jar.name=${project.prefix}.jar
project.zip.name=${project.prefix}.zip
module.version=SNAPSHOT_0.1
dao.jar.name=user-dao-${module.version}.jar
service.jar.name=user-service-${module.version}.jar
controller.jar.name=user-controller-${module.version}.jar
model.jar.name=user-model-${module.version}.jar
captcha.jar.name=user-captcha-${module.version}.jar
web.name=user
databaseDriver=com.mysql.jdbc.Driver
root.username=root
root.password=root
username=ant_test
password=ant123
mysql.url=jdbc:mysql://localhost:3306/

使用文件build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <import file="basic.xml" />

<target name="clean">
        <cleanProject />
    </target>

<target name="initDir" depends="clean">
        <initDir>
            <inittask>
                <!--编译的次数和时间-->
                <buildnumber/>
                <propertyfile file="${metadata}/build.properties">
                    <entry key="Build-time" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss" />
                    <entry key="Build-number" type="int" value="${build.number}" />
                </propertyfile>

</inittask>
        </initDir>
    </target>

<target name="compile" depends="initDir">
        <compileSrcFile>
            <compiletask>
                <echo>拷贝配置文件</echo>
                <copy todir="${build.src.classes.dir}">
                    <fileset dir="${src.dir}" includes="**/*.xml,**/*.properties" />
                </copy>
            </compiletask>
        </compileSrcFile>
    </target>

<target name="compile-test" depends="compile">
        <compileTestSrcFile />
    </target>

<target name="createDatabase" depends="compile-test">
        <createdatabase/>
    </target>

<target name="run-test" depends="createDatabase">
        <runTest />
    </target>

<target name="docs" depends="run-test">
        <docFiles/>
    </target>

<target name="jarFiles" depends="docs">
        <jarFile dest="${build.jar.dir}/${model.jar.name}"  package="model"/>
        <jarFile dest="${build.jar.dir}/${dao.jar.name}"  package="dao"/>
        <jarFile dest="${build.jar.dir}/${service.jar.name}"  package="service"/>
        <jarFile dest="${build.jar.dir}/${captcha.jar.name}"  package="captcha"/>

</target>

<target name="copyFiles" depends="jarFiles">
        <copyFile/>
    </target>

<target name="war" depends="copyFiles">
        <warFile/>
    </target>
    <target name="zipFiles" depends="war">
        <zipFile/>
    </target>

<target name="DeployWar" depends="zipFiles">

<deployWar/>
    </target>

<target name="end" depends="DeployWar">
        <echo>整个过程结束</echo>
    </target>

</project>

使用ant宏定义任务的更多相关文章

  1. c++宏定义命令

    在程序开始以#开头的命令,他们是预编译命令.有三类预编译命令:宏定义命令.文件包含命令.条件编译命令:今天聊聊宏定义: 宏定义命令将一个标识符定义为一个字符串,源程序中的该标识符均以指定的字符串来代替 ...

  2. dll导入导出宏定义,出现“不允许 dllimport 函数 的定义”的问题分析

    建立dll项目后,在头文件中,定义API宏 #ifndef API_S_H #define API_S_H ...... #ifndef DLL_S_20160424 #define API _dec ...

  3. iOS之常用宏定义

    下面我为大家提供一些常用的宏定义! 将这些宏定义 加入到.pch使用 再也不用 用一次写一次这么长的程序了 //-------------------获取设备大小------------------- ...

  4. linux中offsetof与container_of宏定义

    linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->M ...

  5. Linux Kernel代码艺术——系统调用宏定义

    我们习惯在SI(Source Insight)中阅读Linux内核,SI会建立符号表数据库,能非常方便地跳转到变量.宏.函数等的定义处.但在处理系统调用的函数时,却会遇到一些麻烦:我们知道系统调用函数 ...

  6. 面试问题5:const 与 define 宏定义之间的区别

    问题描述:const 与 define 宏定义之间的区别 (1) 编译器处理方式不同     define宏是在预处理阶段展开:     const常量是编译运行阶段使用: (2) 类型和安全检查不同 ...

  7. 关于Xcode8.1 / iOS10+ 真机测试系统打印或者宏定义打印不显示问题

    前言: 最近做项目时遇到了很多莫名其妙的问题,其中就有这个打印(NSLog).也不多废话了,我们先来回顾一下Xcode8发布以来,我们遇到的一些关于打印的问题,当然也有解决方法: 1.Xcode8打印 ...

  8. JDStatusBarNotification和一些宏定义

    // //  AddTopicViewController.m //  vMeet2 // //  Created by 张源海 on 16/6/30. //  Copyright © 2016年 h ...

  9. #define宏定义形式的"函数"导致的bug

    定义了一个宏定义形式的"函数": #define  SUM8(YY)\ {\ int Y = YY>>2;\ ...\ } 然后使用的时候,传入了一个同名的变量Y: i ...

随机推荐

  1. Linux 判断进程是否已经运行的程序

    bool ServerProcess::isAlreadyRunning() { #ifndef __linux__ WarningLog(<<"can't check if p ...

  2. ViewResolver 视图解析器

    pringMVC 视图解析器 前言 在前一篇博客中讲了 SpringMVC 的Controller 控制器,在这篇博客中将接着介绍一下 SpringMVC 视图解析器.当我们对SpringMVC控制的 ...

  3. Git代码行统计命令集

    统计某人的代码提交量,包括增加,删除: git log --author="$(git config --get user.name)" --pretty=tformat: --n ...

  4. mysql 循环插入

    在mysql添加测试数据,想和mssql一样用循环实现,发现不管怎么样都执行失败 经查询发现mysql不支持匿名块,只能先创建出一个存储过程,执行,然后删除 CREATE PROCEDURE test ...

  5. 反射:获取Class对象的三种方式

    获取Class对象的三种方式 package lianxiApril18; /** * 获取Class对象的三种方式 * 1 Object ——> getClass(); * 2 任何数据类型( ...

  6. WPF 简洁的主界面

    用的是dev的TileLayouotControl控件. <dxwui:PageAdornerControl Header="" Padding="30" ...

  7. WPF成长之路------帧动画(1)

    最近公司的一个项目因为是WPF的,而自己已经很长一段时间没有接触过WPF了,再加上之前没有做过wpf的动画效果,因此在学习的过程中也顺便记录一下,说不定以后还会用上,同时也算是总结一下吧!刚开始写博客 ...

  8. sun.misc.Unsafe.park(Native Method)

    关闭tomcat时或者重启tomcat时 log4j2 报错: sun.misc.Unsafe.park(Native Method) 异常信息: 30-Aug-2018 15:59:34.900 S ...

  9. SDOI2013直径(树的直径)

    题目描述: 点这里 题目大意: 就是在一个树上找其直径的长度是多少,以及有多少条边满足所有的直径都经过该边. 题解: 首先,第一问很好求,两边dfs就行了,第一次从任一点找距它最远的点,再从这个点找距 ...

  10. pageadmin自助建站 网站目录权限的设置方法

    在用pageadmin网页设计的时候遇到各种问题可以参考官网教程网站目录权限的设置方法 网站目录必须设置读取和写入权限,否则后台解压,删除文件,在线上传等功能都无法正常使用,下面讲解本机和服务器配置目 ...