ABS工具是如何實現?

原理介紹

先說一個普通的ant文件結構,包括project/property/target/task等元素,下面是一個樣例:html

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" all="all" basedir=".">
   <property name="source.dir" location="source" />
   <target name="all" depends="clean,compile,package,deploy" />
   <target name="clean">
     <delete file="${tmp.dir}" />
   </target>
   
   <target name="compile">
   </target>
   
   <target name="package">
   </target>
   
   ...
</project>

其中delete元素就是一個task,在ant內置的task中有一個能夠執行外部構建文件的task叫ant(參考地址:http://ant.apache.org/manual/Tasks/ant.html)咱們能夠這樣去包裝構建文件:
file: test_project/build.xmlapache

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" all="all" basedir=".">
    <property environment="env" />
    <!-- properties configuration -->
    <property name="ci.root" location="${env.JENKINS_CI}" />
    <property name="jenkins.tools.root" location="${env.JENKINS_TOOLS}"
    <property name="jdk.root" location="${env.JAVA_HOME}" />
    <property name="ant.root" location="${env.ANT_HOME}" />

    <property name="abs-tasks.root" location="${jenkins.tools.root}/abs-tasks" />
    <property name="abs-libs.root" location="${jenkins.tools.root}/abs-libs" />
    <property name="ant-contrib.entry.jar" location="${abs-libs.root}/ant-contrib.jar" />

    <!-- taskdef define section -->
    <!--ant-contrib taskdefs.-->
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${ant-contrib.entry.jar}" />
        </classpath>
    </taskdef>

    <!-- abs-task-framework macro -->
    <macrodef name="abs-task">
        <attribute name="task" />
        <element name="properties" optional="true" />
        <sequential>
            <ant antfile="${abs.tasks.root}/@{task}/run.xml" inheritRefs="true">
                <properties />
            </ant>
        </sequential>
    </macrodef>
    <!-- more taskdef ... -->

    <target name="clean">
        <!-- clean tasks... -->
    </target>

    <target name="compile">
        <abs-task name="customtask">
            <properties>
                <property name="executable.file" value="scripts/demo_vs.bat" />
            </properties>
        </abs-task>
    </target>

    <target name="package">

        <path id="artifact.files">
            <fileset dir="${artifact.dir}">
                <include name="**/*.exe" />
                <include name="**/etc/*.ini" />
            </fileset>
        </path>

        <abs-task name="package">
            <properties>
                <propety name="artifact.pathrefid" value="artifact.files" />
            </properties>
        </abs-task>
    </target>

</project>

file: abs-tasks/customtask/run.xmlui

<?xml version="1.0" encoding="UTF-8"?>
<project name="customtask" default="customtask">
    <dirname property="task.basedir" file="${ant.file.customtask}" />
    <import file="${task.basedir}/macro/customtask_macro.xml" optional="true" />
    <target name="customtask">
        <customtask-macro executableFile="${executable.file}" />
    </target>
</project>

file: abs-tasks/package/run.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="package" default="package">
    <dirname property="task.basedir" file="${ant.file.package}" />
    <import file="${task.basedir}/macro/package_macro.xml" optional="true" />
    <target name="package">
        <package-macro artifactPathRefId="${artifact.pathrefid}" />
    </target>
</project>

將最開始的代碼進行拆分而後包裝成abs-tasks擴展task,這樣就能夠實現構建task的重用。spa

<ant antfile="${abs.tasks.root}/@{task}/run.xml" inheritRefs="true">
    <properties />
</ant>

這個原理其實就是ant -f run.xml -Dk1=v1 -Dk2=v2code

重構包裝

將build.xml文件中的proerties/taskdef define抽取出來單獨存放,目錄結構能夠這個orm

- jenkins-tools
    - abs-config
        ci-common-properties.xml
        ci-common-taskdefs.xml
        ci-common-config.xml
        standerdData.xml #用於描述項目目錄結構規範,能夠自定義一些屬性方便構建使用
    - abs-tasks
        - sourcemonitor # 這個有構建和報告展現功能
            - run.xml
            - macro
                - customtask-macro.xml
            - convertor # 後面會講
                - report.xsl
                - ci-summary.xsl
                - ci-module.xsl
        - customtask #這個只有構建功能
            - run.xml
            - macro
                - package-macro.xml
                - package.xml

file: xxx_project/build.xmlxml

<?xml version="1.0" encoding="UTF-8"?>
<project name="xxx_project" default="build" basedir=".">
    <property environment="env" />
    <property name="abs-config.dir" location="${env.JENKINS_TOOLS}/abs-config" />
    <import file="${abs-config.dir}/ci-common-config.xml" />
    <import file="${abs-config.dir}/standardData.xml" />

    <target name="build" depends="build.steps,build.after-steps" />
    <target name="build.steps" depends="compile" />
    <target name="build.after-steps" depends="artifact" />

    <target name="compile" depends="setup.normal">
        <abs-task name="customtask">
            <properties>
                <property name="executable.file" value="xxx" />
            </properties>
        </abs-task>
        <abs-task name="sourcemonitor">
            <properties>
                <property name="xxx" value="xxx" />
            </properties>
        </abs-task>
    </target>

    <target name="package">
        ...
    </target>

</project>

好了就到這裏吧,ant腳本只是一個構建載體,你能夠將借用exec 擴展不少命令,也能夠使用groovy腳本開發擴展任務或者字符串解析等等。htm

下期預告:如何實現構建任務的數據展現?ip

ci

相關文章
相關標籤/搜索