• 使用ant宏定义任务


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

  • 相关阅读:
    表的锁机制
    ABAP函数:VIEW_MAINTENANCE_CALL(维护表视图等)
    数据字典中新建表时出现黄色警告的处理
    smartforms长文本处理方式
    打印报表页码
    可以根据柜子内表取出所有的柜子信息的BAPI函数
    方法的重载、类的继承
    构造方法
    函数方法
    方法
  • 原文地址:https://www.cnblogs.com/or2-/p/3421809.html
Copyright © 2020-2023  润新知