首先下載ant和compiler
地址:http://ant.apache.org/
http://code.google.com/p/closure-compiler/
[html] view plaincopy www.2cto.com
<?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, Regex替換,將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>