一,安裝環境
1.安裝spm
spm工具是基於node(nodejs的服務平台)的,因此我們需要先安裝 node 和 npm 下載地址:http://nodejs.org/#download.下載完成后安裝即可。
node安裝完成后,找到cmd命令文件以管理員的方式打開,輸入以下命令進行安裝:
npm install spm -g
在此過程中,可能需要你很長的時間等待。(偶爾可能連接失敗了,你需要關閉cmd后重新開啟並執行同樣的命令,過程將繼續)
安裝完成后,恭喜你,可以使用了。
2.spm的使用:
使用spm其實就是執行cmd命令,安裝完成后,你就可以使用命令了 (當然有很多命令的),輸入:
spm help
你會看到所有的命令。我們主要用的命令就是
spm build
當然得注意兩點:
(1).需要將執行目錄切換到項目。比如你的項目js目錄在D:/www/spm/js下;則需要先用cmd命令切換到D:/www/spm/js.見下圖
(2).項目的js目錄結構里面必須包含src目錄,即未合並和壓縮的js文件(seajs模塊文件)。目錄結構約定傳送門。
最后,這些只是將了我接觸的時候遇到的問題,具體spm命令及使用細節請見官方文檔
https://github.com/spmjs/spm/
二,批量壓縮類
寫了個類查找所有JS文件,調用DOS命令執行文件的壓縮
package com.tank.test; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import org.apache.log4j.Logger; import org.apache.log4j.spi.LoggerFactory; /** * @author tank * @date:Sep 24, 2012 4:14:58 PM * @description: * @version :1.0 */ public class TestSeaJS { private static final Logger logger = Logger.getLogger(TestSeaJS.class); private static final String SRC_PATH = "F:/MyEclipseforSpring8.6/showcang/WebRoot/js"; private static final String OUT_PATH = "D:/apache-tomcat-6.0.35/webapps/showcang/js"; //private static final String APP_URL = "http://127.0.0.1:8080/js"; private static final String APP_URL = "http://www.showcang.com/js"; // private static final String CMD = "spm build #0# --combine --app_url #1# // --app_path #2# --out_path #3#"; private static StringBuffer sb = new StringBuffer(); public static void main(String[] args) { //String cmdStr = "spm build ${0} --combine --app_url ${1} --app_path ${2} --out_path ${3}"; File file = new File(SRC_PATH); StringBuffer sbcontext = new StringBuffer(); // sbcontext.append("d: \r"); // sbcontext.append("cd D:/Soft/seajs-spm-ab7a728/demo/js \r"); findFile(file, sbcontext); new FileHelper().getWriteTXT("c:/seajs.bat", sbcontext.toString()); logger.info("壓縮完成!"); } public static void findFile(File file, StringBuffer sbcontext) { File[] dir = file.listFiles(); for (File f : dir) { if (f.isFile()) { String filepath = f.getAbsolutePath(); if (filepath != null && filepath.toLowerCase().endsWith("js")) { String dirpath = f.getParentFile().getPath(); String outPath = dirpath.substring(SRC_PATH.length()); sb.delete(0, sb.length()); sb.append("spm build ").append(filepath).append(" --combine --app_url ").append(APP_URL).append(" --app_path ").append(dirpath).append( " --out_path ").append(OUT_PATH).append(outPath); sbcontext.append(sb.toString()); Runtime rt = Runtime.getRuntime(); BufferedReader br = null; try { Process process = rt.exec("cmd /C " + sb.toString()); br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = br.readLine(); while (line != null) { logger.info(line); line = br.readLine(); } } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); } finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } } } sbcontext.append("\r"); } } else if (file.isDirectory()) { findFile(f, sbcontext); } } } }
文件幫助類:
package com.tank.test; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; /** * @author tank * @date:Sep 26, 2011 9:03:29 PM * @description: 純文本文件操作類 .txt * @version : */ public class FileHelper { public String getReadTXT(String path) { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf-8")); String data = null; StringBuffer sbf = new StringBuffer(); while ((data = br.readLine()) != null) { sbf.append(data); } return sbf.toString(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; } public boolean getWriteTXT(String path, String writeContext) { OutputStreamWriter fw = null; try { fw = new OutputStreamWriter(new FileOutputStream(path), "utf-8"); fw.write(writeContext, 0, writeContext.length()); fw.flush(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } finally { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
三,寫批處理調用
java -jar compass.jar
執行bat即可!