一,安装环境
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即可!