Chapter 16. Concatenation and Baking

If you’ve properly set up your JavaScript files to contain one object
per file, then it’s likely you have dozens of JavaScript files. Before
deploying to production, it’s best to concatenate the files so there are
fewer HTTP requests on the page. Exactly how many files and which files
should be concatenated with which is a project-specific decision. In any
case, Ant provides an easy way to concatenate multiple files.

The Task

The <concat> task is one of
the simplest in Ant. You simply specify a destfile attribute containing the destination
filename and then include as many <fileset> and
<filelist> elements as you want. At it’s simplest, you can have a
target such as:

This target concatenates all JavaScript files in the source
directory into a single file called build.js in the
build directory. Keep in mind that the files are concatenated in the order
in which they appear on the filesystem (alphabetically). If you want a
more specific ordering, you’ll need to specify it explicitly, such
as: