根据配置xml(节点顺序)或调用方法进行处理,可以:
合并js,css的src
合并js,css文件内容
删除文件
替换文件中内容
执行cmd命令
调用bat文件
使用gcc,yui压缩js,css
动态版本号

配置文件
<?xml version="1.0"?>
<project name="refund" default="build">
<property name="charset" value="utf-8"/>
    <property name="tool.dir" location="http://www.cnblogs.com/../tool"/>
    <property name="compiler" location="${tool.dir}/closure-compiler/compiler.jar"/>
    <property name="form.build.dir" location="http://www.cnblogs.com/../build/form"/>
    <property name="src.dir" location="."/>
    <property name="uploader.build.dir" location="${form.build.dir}/uploader"/>
    <property name="ant_test_ahead" location="ant/ant" />
    <property name="ant_test_back" location="http://www.cnblogs.com/../ant_test"/>
    <property name="ant_test_back1" location=""/>
    <property name="gcc" location="compiler.jar"/>
    <property name="yui" location="yuicompressor.jar"/>
    <property name="ver" dynamic="true" type="datetime" format="yyyyMMddHHmmss" />
    <target name="build">
        <echo>Hello world!</echo>
        <route destfile="a_b.css" encoding="${charset}" outputencoding="${charset}">
            <path path="a.css"/>
            <path path="b.css"/>
            <fileset dir="${ant_test_back1}" includes="index.html"/>
        </route>
        <note>执行单个命令</note>
        <command workdir=""> 
            <arg value="dir"/>
        </command>
        <echo>批处理bat文件</echo>
        <batch>
            <path path="mybattest.bat"/>
        </batch>
        <note>替换内容</note>
        <replace encoding="${charset}" outputencoding="${charset}">
            <fileset dir="${ant_test_back1}" includes="rep.txt" excludes="a.js,b.js"/>
            <marknote start="&lt;start&gt;" end="&lt;end&gt;"  value="替换的内容" path="toRep.txt" />        
            <mark key="@rep" value="替换的内容@rep不能死循环哦。。。" path="toRep.txt"/>
            <mark key="@rep" value="" path=""/>
            <mark key="" value="" path=""/>
        </replace>        
        <compress  executable="java" verbose="true" dest="js" failonerror="true" parallel="false">
            <fileset dir="${ant_test_back1}" includes="*.js" excludes="a.js,b.js"/>
            <arg line="-jar"/>
            <arg path="${gcc}"/>
            <arg line="--charset utf8"/>
            <arg value="--warning_level"/>
            <arg value="QUIET"/>
            <arg value="--js"/>
            <srcfile value="a.js"/>
            <arg value="--js_output_file"/>
            <mapper type="regexp" from="^(.*)\.js$" to="\1_${ver}.min.js"/>
            <targetfile value="a.min.js"/>
        </compress>
        <route destfile="a_b.js?t=${ver}" encoding="${charset}" outputencoding="${charset}">
            <path path="a.js"/>
            <path path="b.js"/>
            <fileset dir="${ant_test_back1}" includes="*.html,*.aspx" excludes="index_1.aspx,xx.aspx"/>
        </route>
         <concat destfile="a_c.js">
            <path path="a.js"/>
            <path path="c.js"/>
        </concat>
         <delete>
            <fileset dir="${ant_test_back1}" includes="xx.aspx"/>
        </delete>
         <concat destfile="b_c.js">
            <path path="b.js"/>
            <path path="c.js"/>
        </concat>
    </target>
    <target name="build">
        <echo>Hello world!</echo>
        <compress  executable="java" verbose="true" dest="css" failonerror="true" parallel="false">
            <fileset dir="${ant_test_back1}" includes="*.css" excludes=""/>
            <arg line="-jar"/>
            <arg path="${yui}"/>
            <arg line="--charset utf8"/>           
            <srcfile value="a.css"/>
            <arg value="-o"/>
            <mapper type="regexp" from="^(.*)\.css$" to="\1.min.css"/>
            <targetfile value="a.min.css"/>
        </compress>
        <route destfile="a_b.js?t=${ver}" encoding="${charset}" outputencoding="${charset}">
            <path path="a.js"/>
            <path path="b.js"/>
            <fileset dir="${ant_test_back1}" includes="*.html,*.aspx" excludes="index_1.aspx,xx.aspx"/>
        </route>
         <concat destfile="a_c.js" encoding="${charset}" outputencoding="${charset}">
            <path path="a.js"/>
            <path path="c.js"/>
        </concat>
         <delete>
            <fileset dir="${ant_test_back1}" includes="xx.aspx"/>
        </delete>
         <concat destfile="b_c.js">
            <path path="b.js"/>
            <path path="c.js"/>
        </concat>
    </target>
</project>
命令行调用方法
* 命令不区分大小写
            * exit 退出
            * build_project_bat -命令名 参数1 参数2 ... 参数N
            * [根据xml执行所有]
            * lckeyant -exec xmlpath rootpath  
            * [合并当前页面中的srcs路径引用(js,css)为target路径]:
            * lckeyant -route_page_srcs2target currentPage srcs target rootPath encoding outputencoding 
            * [合并当前页面中的多种srcs路径引用(js,css)为对应的多个target路径]:
            * lckeyant -route_page_srcs2targets currentPage combineFiles encoding outputencoding 
            * [多页面对多(js,css)=>多target个的(js,css)]:
            * lckeyant -route_pages_srcs2targets htmlPageFiles combineFiles encoding outputencoding 
            * [传入阐述batArgs到fileName的bat文件执行批处理任务]:
            * lckeyant -bat fileName batArgs
            * [在工作目录workDir内执行cmdStr命令]:
            * lckeyant -cmd workDir cmdStr 
            * [传入参数args执行可执行命令如ie.exe]:
            * lckeyant -run execName args 
            * [拼接多个文件内容,读取编码,输出编码格式]:
            * lckeyant -concat_all combineFiles encoding outputencoding 
            * [将源目录文件内容追加到目标文件中]:
            * lckeyant -concat sourceFilePath targetFilePath  encoding  outputEncoding  
            * [删除arg1-N文件,可以相对于工作根目录的路径]:
            * lckeyant -delete arg1 arg2 ...  argN  

            //提示信息
            * ------参数example--------
            * ------webPages与htmlPageFiles同 如 E:\xx\xx\root\index.html,E:\xx\default.html
            * ------srcs 如: a.js,b.js,c.js
            * ------target 如: a_b_c.js
            * ------combineFiles 如: C:\xxx\root::a.js,b.js,c.js=>a_b_c.js
            * ------htmlPageFiles 如: E:\xx\xx\root\index.html,E:\xx\default.html

试验 ant_test.rar

详见 git

www.LcKey.com 提供支持 

posted on 2012-06-10 01:09  LcKey  阅读(420)  评论(0编辑  收藏  举报