Win7, Ant mates with Yuicompressor to merge, compress, and copy JS and CSS

Source: Internet
Author: User
Tags echo message glob

This article is based on the Windows7 system, which may be simpler on Mac. This article explains the article http://www.cnblogs.com/catprayer/archive/2011/08/03/2126719.html, which has no ink to the finishing touches.

It took some time, it was a successful trial, demo address: http://pan.baidu.com/s/1c0dGm1i

Ant can go to the official website to download, address is: http://ant.apache.org/, yuicompressor can also go to the official website next https://github.com/yui/yuicompressor

The ant version of the demo that is used is the apache-ant-1.8.2,yuicompressor version of yuicompressor-2.4.6.

In addition, because ant is in need of Java operating environment (in the beginning did not look at Ant's instructions, this wasted some time), 1.8 of the version requires at least 1.4 Java JDK, can be viewed from here: http://ant.apache.org/faq.html


Java environment can be downloaded to the official website, address: http://www.oracle.com/technetwork/java/javase/downloads/index.html, please install the Java environment, and then configure the Java environment variables,

After configuration, you can enter Java under Cmd-dos to see if the installation was successful. Then configure the ant environment variable, configure it, and then test whether the installation succeeds,cmd->dos-> input ant, which appears similar to the following content representation

Installation succeeded:


To this environment before it is configured successfully. See the demo structure below


I've built a little project in VS, just to write the build XML file, and again, just to write XML, you can do it yourself manually in the folder.

The core of which is the Build.xml file, which is structured as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><project default= "Compress" basedir= "F:\js\cc\deploymentTes\web" Name= "core" > <!--Project Web path--<property name= "path" value= "F:\js\cc\deploymentTes\web"/> <!--deployed Output path--<property name= "TargetDir" value= "F:\js\cc\deploymentTes\build_output\asset"/> <!--source file path (SRC)- -<property name= "code.src" value= "src"/> <!--!!! YUI Compressor Path!!! --<property name= "Yuicompressor" value= "F:\js\cc\deploymentTes\build\yuicompressor-2.4.6\build\                 Yuicompressor-2.4.6.jar "/> <!--================================= Target:concat Stitching (merging) file =================================--<target name= "concat" depends= "" description= "Concat Code" > <echo Message= "Concat Code Files Begin!!!"/> <!--JS-<concat destfile= "${targetdir}/js/all.js" encoding= " Utf-8 "fixlastline=" on "> <fileset dir=" ${code.src}/js "includes=" jquery-1.10.2.js "/> <fileset dir=" ${code.src}/js "includes=" Jquery-ui.js "/> </concat> <!--CSS-- <concat destfile= "${targetdir}/css/all.css" encoding= "Utf-8" > <fileset dir= "${code.src}/css" includes= "JQ Uery-ui.css "/> <fileset dir=" ${code.src}/css "includes=" Jquery-ui.theme.css "/> </concat> <e Cho message= "Concat Code Files finished!!!"/> </target> <!--================================= Ta Rget:copy Copy File =================================--<target name= "Copy_asset" depends= "Concat" Descripti on= "Copy the asset file" > <echo message= "copy asset Begin"/> <!--main.html--<copy todir= "$ {targetdir}/"overwrite=" true "> <fileset dir=" ${path}/"includes=" *.html "/> </copy> <!--img *.png--<copy todir= "${targetdir}/img" overwrite= "true" > <fileset dir= "${path}/asset/img" includes= " *.png "/> </copy> <echo message= "Copy Asset finished"/> </target> <!--================================= target:compress compression js && css =================================--<target name= "compress" dep ends= "Copy_asset" description= "Compress Code" > <echo message= "Compress code Begin"/> <apply executable= "Java" parallel= "false" failonerror= "true" dest= "${targetdir}/js" > <fileset dir= "${targetdir}/js" includes= "*. JS "/> <arg line="-jar "/> <arg path=" ${yuicompressor} "/> <arg line="--charset utf-8 "/&gt      ; <arg line= "-o"/> <targetfile/> <mapper type= "Glob" from= "*.js" to= "*.min.js"/> </apply > <apply executable= "java" parallel= "false" failonerror= "true" dest= "${targetdir}/css" > <fileset dir= " ${targetdir}/css "includes=" *.css "/> <arg line="-jar "/> <arg path=" ${yuicompressor} "/> < Arg line= "--charseT utf-8 "/> <arg line="-o "/> <targetfile/> <mapper type=" Glob "from=" *.css "to=" *.min.cs S "/> </apply> <echo message=" Compress Code finished "/> <echo message=" clean Begin "/> & lt;!    --<delete verbose= "false" failonerror= "true" > <fileset dir= "${path}" includes= "${targetdir}/*-o.js"/> </delete>--> <echo message= "clean finished"/> </target></project>
The BUILD.bat batch file is intended to call ant, and its contents are:

./apache-ant-1.8.2/bin/ant-f./build.xml
is to run the bat file, as for the Ant and yuicompressor XML parsing instructions, you can read the document, very detailed, and you download the demo contains detailed instructions for use:

The following excerpt is Yuicompressor's readme:

==============================================================================yui Compressor=================== ===========================================================name YUI compressor-the Yahoo! JavaScript and CSS                Compressorsynopsis Usage:java-jar Yuicompressor-x.y.z.jar [options] [input file] Global options-h,--help Displays this information--type <js|css> specifies the type of the input file--charset < charset> Read The input file using <charset>--line-break <column> Insert a line break after t                 He specified column number-v,--verbose Display informational messages and Warnings-o <file>                              Place the output into <file> or a file pattern.  Defaults to stdout. JavaScript Options--nomunge minify only, don't obfuscate--preserve-semi preserve all SE Micolons--disable-optimizations Disable allMicro Optimizationsdescription The YUI Compressor is a JavaScript Compressor which, in addition to removing comments and White-spaces, obfuscates local variables using the smallest possible variable name.  This obfuscation are safe, even when using constructs such as ' eval ' or ' with ' (although the compression are not optimal are  Those cases) Compared to jsmin, the average savings is around 20%. The YUI Compressor is also able to safely compress CSS files. The decision on which compressor is being used are made on the file extension (JS or CSS) GLOBAL options-h,--help P  Rints help with the use of the YUI Compressor--line-break Some Source Control tools don ' t like files containing lines Longer than, say 8000 characters. The linebreak option is used-in, case-to-split long lines after a specific column. It can also is used to make the code more readable, easier to debug (especially with the MS Script Debugger) Spe Cify 0 To get a line break after each SEMi-colon in JavaScript, and after each of the rule in CSS.  --type js|css the type of compressor (JavaScript or CSS) is chosen based on the extension of the input file name (. js or. css) This option was required if no input file has been specified.  Otherwise, this option was only required if the input file extension is neither ' JS ' nor ' CSS '. --charset Character-set If A supported character set is specified, the YUI Compressor would use it to read the Put file. Otherwise, it would assume that the platform's default character set is being used.  The output file is encoded using the same character set. -o outfile place output in file outfile.      If not specified, the YUI Compressor would default to the standard output, which you can redirect to a file.  Supports a filter syntax for expressing the output pattern when there is multiple input files. Ex:java-jar yuicompressor.jar-o '. Css$:-min.css ' *.css ... would minify aLL. css files and save them as-min.css-v,--verbose Display informational messages and warnings. JAVASCRIPT only OPTIONS--nomunge minify only.  Do not obfuscate local symbols. --preserve-semi Preserve unnecessary semicolons (such as right before a '} ') this option was useful when compress  Ed code have to be run through JSLint (which are the case of YUI for example)--disable-optimizations Disable all The built-in micro optimizations.  NOTES + If No input file is specified, it defaults to stdin.  + Supports wildcards for specifying multiple input files.  + The YUI Compressor requires Java version >= 1.4. + It is possible to prevent a local variable, nested function or function argument from being obfuscated by using "hint S ". A hint is a string this is located at the very beginning of a function body like so:function fn (arg1, arg2, Arg3)        {"Arg2:nomunge, Localvar:nomunge, Nestedfn:nomunge";        ... var LocalVar;    ...    function Nestedfn () {...}    ...    }  The hint itself disappears from the compressed file. + C-style comments starting with/*! is preserved. This was useful with comments containing copyright/license information.     For example:/*!     * TERMS of use-easing equations * Open source under the BSD License.     * Copyright 2001 Robert Penner All rights reserved.     */becomes:/* * TERMS of use-easing equations * Open source under the BSD License.     * Copyright 2001 Robert Penner All rights reserved. */modified Rhino FILES YUI Compressor uses a MODIFIED version of the Rhino Library (http://www.mozilla.org/rhino/) the C Hanges were made to support JScript conditional comments, preserved comments, unescaped slash characters in regular expr Essions, and to allow for the optimization of escaped quotes in string literals.  Copyright and LICENSE Copyright (c) Yahoo! Inc.  All rights reserved. The copyrights embodied in THe content of this file is licensed by Yahoo! Inc. under the BSD (revised) Open source license. 

PS, on the internet also found an online, the address is: Http://ganquan.info/yui/?hl=zh-CN

Also attached to the relevant articles for friends to learn:

Http://www.cnblogs.com/likehua/archive/2011/08/16/2141332.html using Ant and Yui compression js

http://blog.csdn.net/kunlong0909/article/details/7584802 Ant and yuicompressor compression css, JS scheme

Win7, Ant mates with Yuicompressor to merge, compress, and copy JS and CSS

Related Article

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.