Use SPM build to bulk package and compress seajs code

Source: Internet
Author: User

1. installation environment

1. Install SPM

The SPM tool is based on node (nodejs Service Platform), so we needInstallNode and NPM: http://nodejs.org/javasdownload.after the download is complete, the installation is now available.

After the node is installed, find the CMD command file and open it as an administrator. Enter the following command to install it:

NPM install SPM-G

In this process, you may have to wait for a long time. (The connection may fail occasionally. You need to disable CMD and restart and execute the same command. The process will continue)

After the installation is complete, congratulations, you can use it.

2. Use of SPM:

Executing SPM is actually executing the CMD command. After the installation is complete, you can use the command (of course there are many commands) and enter:

SPM help

You will see all the commands. The main command we use is

SPM build

Of course, pay attention to two points:

(1). Switch the execution directory to the project. For example, if the JS directory of your project is under D:/www/SPM/JS, you need to use the CMD command to switch to D:/www/SPM/JS. See

(2) the project's Js directory structure must contain the src directory, that is, JS files that are not merged or compressed (seajs module files ). Directory structure Convention portal.

Finally, these problems only occur when I contact you. For details about the SPM commands and usage, see the official documentation.

Https://github.com/spmjs/spm/

Ii. Batch Compression

Write a class to search for all JS files and call the DOS command to compress the files.

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,201 2 4:14:58 * @ Description: * @ version: 1.0 */public class testseajs {Private Static final logger = logger. getlogger (testseajs. class); Private Static F Inal 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 #"; PR Ivate static stringbuffer sb = new stringbuffer (); public static void main (string [] ARGs) {// string character STR = "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 (). getwrit Etxt ("C:/seajs. Bat", sbcontext. tostring (); logger.info ("compressed! ");} 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 = rt.exe C ("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 );}}}}

file help class:

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,201 1 9:03:29 * @ Description: operation class of plain text files. 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 blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {If (BR! = NULL) {try {BR. close ();} catch (ioexception e) {// todo auto-generated catch blocke. 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 blocke. printstacktrace (); Return false;} finally {try {FW. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}

Iii. Write batch processing calls

Java-jar compass. Jar

Execute bat!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.