使用ant巨集定義任務

c3tc3tc3t發表於2013-11-13

基礎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>

相關文章