日期:2014-05-16  浏览次数:20389 次

Ant和compiler合并压缩js

首先下载ant和compiler

地址:http://ant.apache.org/

          http://code.google.com/p/closure-compiler/

<?xml version="1.0" encoding="UTF-8"?>
<project name="Javascritp_build" default="clean" basedir="../">
     
    <description>Javascritp build for Ant</description>
     
    <property name="src" location="js"/>
    <property name="build" location="build"/>
    <property name="target" location="result"/>
    <property name="lib" location="lib"/>
    <property name="charset" value="utf-8"/>
     
    <!-- - - - - - - - - - - - - - - - - - 
    这个 ant 配置文件要经过4个流程:
    1、target init 进行初始化处理,创建一个目录build,用于暂存文件;
    2、target concat 合并两个 js 文件,放到 build 目录下;
    3、target compress 调用 Yui Compressor 对合并后的 js 进行压缩
    4、target clean 进行清理动作,删除生成的 build 目录
 
    ANT标签和属性解释:
    project 的 default 对应某个 target 的 name 值,表示默认执行哪个步骤;
    target 的 depends 对应其他某些 target 的 name 属性,表示依赖性;
    ${name} 可以引用 property 中定义的值。
    mkdir 标签创建一个目录
    replaceregexp, 正则表达式替换,将DEBUG标识替换为空,在正式环境不处理DEBUG信息
    注意设置文件的 encoding 属性,否则可能有乱码情况
 
    关于ANT的详细文档,请看官方手册:http://ant.apache.org/manual/        
         - - - - - - - - - - - - - - - - - -->
    <target name="init">
        <mkdir dir="${build}" />
    </target>
     
    <target name="concat" depends="init">
        <concat destfile="${build}/all.js" encoding="${charset}" outputencoding="${charset}">
            <path path="${src}/core.js" />
            <path path="${src}/g.js" />
            <path path="${src}/nav.js" />
        </concat>
         
        <!-- - - - - - - - - - - - - - - - - - 
            replaceregexp的说明    http://ant.apache.org/manual/Tasks/replaceregexp.html
         - - - - - - - - - - - - - - - - - -->
         
        <replaceregexp match="DEBUG" replace="" flags="g" byline="true" file="${build}/all.js" encoding="${charset}" />
    </target>
     
    <!-- - - - - - - - - - - - - - - - - - 
        YUICompressor参数 http://developer.yahoo.com/yui/compressor/#work
         
        通用参数:
            -h, \-\-help                 显示帮助信息
           \-\-type <js|css>            指定输入文件的文件类型
           \-\-charset <charset>        指定读取输入文件使用的编码
           \-\-line-break <column>      在指定的列后插入一个 line-bread 符号
           \-v, \-\-verbose              显示info和warn级别的信息
           -o <file>                  指定输出文件。默认输出是控制台。
         
        JavaScript专用参数:
             \-\-nomunge                  只压缩, 不对局部变量进行混淆。
           \-\-preserve-semi            保留所有的分号。
           \-\-disable-optimizations    禁止优化。
    - - - - - - - - - - - - - - - - - -->
    <target name="compress" depends="concat">
        <echo message="start compress" />
        <java jar="${lib}/yuicompressor-2.4.2.jar" fork="true" failonerror="false">
            <arg line="--type js --charset ${charset} --nomunge ${build}/all.js -o ${target}/core.js" />
        </java>
        <echo message="end compress" />
    </target>
         
    <target name="clean" depends="compress">
        <delete dir="${build}"/>
    </target>
     
</project>