Project build Combat-ant Getting Started Guide (9)

Source: Internet
Author: User
Tags auth closure file upload ming

Project build Combat-ant Getting Started Guide (9)

The previous 8 ant tutorials, which are basically not too complex, are experiencing complex scenarios in the actual project build. Today, in this tutorial, Minghe will combine the use of ant with a real project build process and demonstrate how to handle the looping tasks in Ant. Project Introduction

Project name: KF (kissy-form)
Purpose: Kissy Form component Package
Source Address: Https://github.com/minghe/kissy-form
KF is still in the development phase, the completed components only uploader (asynchronous file upload), will release more form components, interested friends can be concerned about.
Uploader scenarios are complex enough to cover most common uses of ant, please download the KF source to the local, find uploader Build.xml file (form/src/uploader/build.xml).


Build Target

Build (The built package release file) must be aligned with the SRC (source directory) directory structure. Merge src under all JS for render-pkg.js (excluding themes, demo, auth, tests directory of files) will be Render-pkg.js moved to the build directory, and renamed to Render.js Copy a copy of the themes directory to the Build/form/uploader traversal themes under each directory, the subdirectory of all JS merged into index-pkg.js delete all JS files except Index-pkg.js Rename index-pkg.js to all JS files in the index.js compression build directory Copy a copy of uploader.swf file to the build directory

The task is not a lot, maybe you will think that the Ming river is so complicated and egg ache, in fact these tasks with the Ming River actually to achieve the goal of construction, not the focus of this article, not discussed here.
The next step is to see how Minghe achieves these goals.
Because you need to use a third party class library Ant-contrib-1.0b3.jar, download the file to your Ant/lib directory first.
Click here to download all JS for render-pkg.js (excluding themes, demo, auth, tests directory files)

<target name= "Concat" >
    <concat destfile= "render-pkg.js" encoding= "${charset}" outputencoding= "${ CharSet} ">
        <fileset dir=" ${src.dir} "includes=" **/*.js "excludes=" Tests/*.js,demo/**/*.js,auth/*.js, Themes/**/*.js "/>
        <filterchain>
            <deletecharacters chars=" "/>
        </filterchain>
    </concat>
</target>

You can see the introduction to the basic Task label (top)-ant Guide (3) on the use of concat.

move Render-pkg.js to the build directory and rename it to Render.js

<target name= "Move" ><br/> <move file= "render-pkg.js" tofile= "${uploader.build.dir}/render.js"
    / >
</target>

Renaming is not necessarily a rename label, and can be implemented indirectly using the move label. copy a copy of the themes directory to Build/form/uploader and delete the HTML file in the directory

<target name= "Move-theme" >
    <copy todir= "${uploader.build.dir}/themes" overwrite= "true" >
        < Fileset dir= "${src.dir}/themes"/>
    </copy>
    <delete>
        <fileset dir= "${ Uploader.build.dir}/themes "includes=" **/*.html "/>
    </delete>
</target>

This part of the goal is also very simple, using copy and delete can be implemented.

iterate through each directory under Themes, merge all JS under the subdirectory into Index-pkg.js. Delete all JS files except Index-pkg.js. > renamed index-pkg.js as Index.js

Well, into the focus of this article, the goal of this task has a difficulty, is how to handle the loop in ant.

As shown in the figure above, the themes directory under build and SRC must be consistent, and you must traverse each subdirectory to merge the directory's JS, so you need to use a loop. At this point you may think that the for,ant used in programming languages have a for tag. It does affect ant is not a programming language, there is no direct for label, but through the third party class library can implement for loop, we look at the code.
Please make sure that you have ant-contrib-1.0b3.jar files under the Lib directory under Ant on this machine. add a new namespace

<project name= "Uploader.build" default= "Build" basedir=. "xmlns:ac=" Antlib:net.sf.antcontrib ">
</ Project>

Add the AC namespace to your project: mlns:ac= "Antlib:net.sf.antcontrib".

traversal of each directory under Themes

<target name= "Build-theme" depends= "Move-theme" >
    <ac:for param= "file" >
        <path>
            < Fileset dir= "${uploader.build.dir}/themes" includes= "**/index.js"/>
        </path>
        <sequential>
            <ac:var name= "Var.dir" unset= ' true '/> <dirname property= ' var.dir ' file= ' @{file
            } '/>
            < Echo>concat ${var.dir}</echo>
        </sequential>
    </ac:for>
</target>

Use Ac:for to open the loop, param as the parameter name for replacing the file path at the back of the loop.

<path>
   <fileset dir= "${uploader.build.dir}/themes" includes= "**/index.js"/>
</path>

Defines the file path. Take the index.js in each subdirectory.
<sequential>
    <ac:var name= "Var.dir" unset= "true"/> <dirname property= "Var.dir" file= "@{file"
    } "/>
    <echo>concat ${var.dir}</echo>
</sequential>

Sequential label, used with for label, Flag Loop start.
Ac:var defines the Var.dir variable (that is, the subdirectory path) to change, and note that the property of the ant definition cannot be changed.
DirName gets the subdirectory path for the file.
Next, perform the task:
<concat destfile= "${var.dir}/index-pkg.js" encoding= "${charset}" outputencoding= "${charset}" >
    < Fileset dir= "${var.dir}" includes= "*.js"/>
    <filterchain>
        <deletecharacters chars= ""/>
    </filterchain>
</concat>
<delete>
    <fileset dir= "${var.dir}/" includes= "*. JS "excludes=" Index-pkg.js "/>
</delete> <rename src=" ${var.dir}/index-pkg.js "dest=" ${
Var.dir}/index.js "/>
compress all JS files in the build directory

<target name= "minify" depends= "CRLF" >
    <apply executable= "java" verbose= "true" dest= "${" Uploader.build.dir} ' failonerror= ' true ' parallel= ' false ' >
        <fileset dir= ' ${uploader.build.dir} ' includes= "**/*.js"/>
        <arg line= "-jar"/> <arg path=
        "${compiler}"/> <arg line= "--charset UTF8
        "/ >
        <arg value= "--warning_level/> <arg value=" QUIET "/> <arg value=
        "
        --js "/> <srcfile/>
        <arg value= "--js_output_file"/>
        <targetfile/>
        <mapper type= " RegExp "from=" ^ (. *) \.js$ "to=" \1-min.js "/>
    </apply>
</target>

See the ant combination Yui-compressor and Closure-compiler-ant Starter Guide for how to use the ant compression script.
Other simple parts are no longer described.

The complete code is as follows:

<project name= "Uploader.build" default= "Build" basedir=. "xmlns:ac=" Antlib:net.sf.antcontrib "> <  Description>uploader build file</description> <property name= "CharSet" value= "Utf-8"/> <property Name= "Tool.dir" location= ". /.. /.. /tool "/> <property name=" compiler "location=" ${tool.dir}/closure-compiler/compiler.jar "/> <property na Me= "Form.build.dir" location= ". /.. /.. /build/form "/> <property name=" Src.dir "location=". 
    /> <property name= "Uploader.build.dir" location= "${form.build.dir}/uploader"/> <!--merge source files, excluding templates js--> <target name= "Concat" > <concat destfile= "render-pkg.js" encoding= "${charset}" outputencoding= "${char Set} "> <fileset dir=" ${src.dir} "includes=" **/*.js "excludes=" tests/*.js,demo/**/*.js,auth/*.js,themes/* */*.js "/> <filterchain> <deletecharacters chars=" "/> </filterch Ain> </coNcat> </target> <!--move merged source files to build directory--> <target name= "Moves" > <move file= "rend" Er-pkg.js "tofile=" ${uploader.build.dir}/render.js/> </target> <!--delete all files under the build directory--> ET name= "clean-build" > <delete> <fileset dir= "${uploader.build.dir" includes= *.css,**/*.swf "/> </delete> </target> <!--mobile uploader.swf--> <target name=" move -swf "> <copy file=" plugins/ajbridge/uploader.swf "todir=" ${uploader.build.dir}/plugins/ajbridge "/> &L T;/target> <!--The source file of the mobile template to the build directory--> <target name= "Move-theme" > <copy todir= "${uploader".
        Build.dir}/themes "overwrite=" true "> <fileset dir=" ${src.dir}/themes "/> </copy> <delete> <fileset dir= "${uploader.build.dir}/themes" includes= "**/*.html"/> &LT;/DELETE&G
    T
 </target>   <!--build templates, merge the JS file under the template, generate Index-pkg.js, delete the source file, change the index-pkg.js to index.js--> <target name= "Build-theme" depends= " Move-theme "> <ac:for param=" File "> <path> <fileset dir=" ${uploader. Build.dir}/themes "includes=" **/index.js "/> </path> <sequential> &L
                T;ac:var name= "Var.dir" unset= "true"/> <dirname "property=" Var.dir "file=}" @{file <echo>concat ${var.dir}</echo> <concat destfile= "${var.dir}/index-pkg.js" encoding=
                    Arset} "outputencoding=" ${charset} "> <fileset dir=" ${var.dir} "includes=" *.js "/> <filterchain> <deletecharacters chars= ""/> </filtercha in> </concat> <delete> <fileset dir= "${var.dir}/" in Cludes= "*.js" excludes= "inDex-pkg.js "/> </delete> <rename src=" ${var.dir}/index-pkg.js "dest=" ${var.dir}
    /index.js "/> </sequential> </ac:for> </target> <!--remove redundant blank pages before compression--> <target name= "CRLF" > <fixcrlf includes= "*.js" srcdir= "${uploader.build.dir}" encoding= "UTF8" eol= "CRLF" "></fixcrlf> </target> <!--compression script--> <target name=" minify "depends=" CRLF "> &L
            T;apply executable= "java" verbose= "true" dest= "${uploader.build.dir}" failonerror= "true" Parallel= "false" > <fileset dir= "${uploader.build.dir}" includes= "**/*.js"/> <arg line= "-jar"/> <arg p
            Ath= "${compiler}"/> <arg line= "--charset utf8"/> <arg "value=--warning_level"/>  <arg value= "QUIET"/> <arg value= "--js"/> <srcfile/> <arg Value= "--js_output_file"/> <targetfile/> <mapper type=" regexp "from=" ^ (. *) \.js$ "to=" \1-min.js "/>" </apply> </target> <target name= "Build" depends= "concat,clean-build,move,build-theme,minify,move-s WF "> </target> </project>

Turn from: http://www.36ria.com/5003


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.