First download ant and compiler
Address: 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> javascrui 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"/>
<! -------------------
The ant configuration file goes through four steps:
1. initialize target init and create a directory build for saving files;
2. target concat merges two js files and stores them in the build directory;
3. target compress calls Yui Compressor to compress the merged js.
4. Clear target clean to delete the generated build directory.
ANT labels and attributes:
Project default corresponds to the name value of a target, indicating which step is performed by default;
The depends of target corresponds to the name attribute of some other targets, indicating dependency;
$ {Name} can reference the value defined in property.
Create a directory using the mkdir label
Replaceregexp, replace the regular expression, and empty the DEBUG mark. Do not process the DEBUG information in the official environment.
Pay attention to setting the encoding attribute of the file. Otherwise, garbled characters may exist.
For more information about ANT, see the official manual: 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 = "$ {src}/core. js"/>
<Path = "$ {src}/g. js"/>
<Path = "$ {src}/nav. js"/>
</Concat>
<! -------------------
Description http://ant.apache.org/manual/Tasks/replaceregexp.html for replaceregexp
---->
<Replaceregexp match = "DEBUG" replace = "" flags = "g" byline = "true" file = "$ {build}/all. js "encoding =" $ {charset} "/>
</Target>
<! -------------------
YUICompressor parameter http://developer.yahoo.com/yui/compressor/#work
Common parameters:
-H, \-help display help information
\-Type <js | css> specifies the file type of the input file
\-Charset <charset> specifies the encoding used to read the input file.
\-Line-break <column> insert a line-bread symbol after the specified column
\-V, \-verbose displays info and warn Information
-O <file> specifies the output file. The default output is the console.
Special JavaScript parameters:
\-Nomunge only compresses and does not confuse local variables.
\-Preserve-semi keep all semicolons.
\-Disable-optimizations cannot be optimized.
---->
<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>