Automating builds with Ant
Tagged ant, aptana, automation, packer
I started working on Plotr again and I needed a simple way to automate tasks like concatenating files,replacing some variables and packing with Dean Edwards packer. Then I found Apache Ant, which was already integrated in Aptana (which is my favorite javascript IDE). Well, Ant isn’t integrated in Aptana but in Eclipse, and because I’ve installed Aptana as a plugin for Eclipse, Ant is already installed. Anyway, I just wanted to share this info, because it’s saving me so much time. In this article I’ll explain how to write a simple Ant build-file.
Setting up the environment
To start working with Aptana and Ant you need to install Eclipse (which needs Java). I’ll assume you know how to do this. After that go to www.aptana.com and read how to install Aptana as an Eclipse plugin.
Now you’ve got everything up and running, create a new (Web) Project. Let’s say our project looks like this:
Screenshot of the Aptana project.
Ant takes a XML file. In build.xml we define a set of tasks, and Ant is going to execute these tasks. Let’s say we want to build our project by concatenating the three files in the js folder. We also want to pack the concatenation and store the result in another file. Ant comes with a set of built-in tasks. Before we can use these tasks we need to define our project.
The Ant build file
We define our project in the buildfile. First this is what the Ant manual has to say about build files.
Ant’s buildfiles are written in XML. Each buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique.
Ok, so we need to define a project, and some targets. Well, our project is called ‘AutomationProject’ and our targets are concatenating files and packing the result afterwards. Here’s what I came up with:
<project name=\"AutomationProject\" default=\"pack\">
<property name=\"packer_dir\" location=\"${basedir}\\packer\" />
<property name=\"js_dir\" location=\"${basedir}\\js\" />
<target name=\"concat_file\">
<!-- Concatenate three file in the following order: file0.js, file1.js, file2.js. -->
<concat destfile=\"${js_dir}\\sol_uncompressed.js\">
<filelist dir=\"${js_dir}\" files=\"file0.js,file1.js,file2.js\" />
</concat>
<echo>file0.js, file1.js, file2.js concatenated.</echo>
</target>
<target name=\"version\" depends=\"concat_file\">
<!-- Replace \'__version__\' with \'0.1.1\' in the sol_uncompressed.js file. -->
<replace file=\"${js_dir}\\sol_uncompressed.js\" token=\"__version__\" value=\"0.1.1\" />
<echo>\'__version__\' replaced with \'0.1.1\' in the sol_uncompressed.js file.</echo>
</target>
<target name=\"pack\" depends=\"version\">
<!-- Pack the sol_uncompressed.js file. -->
<exec dir=\"${packer_dir}\" executable=\"cmd\">
<arg line=\"/c CScript /nologo pack.wsf ${js_dir}\\sol_uncompressed.js 62 1 1 > ${js_dir}\\sol_packed.js\"/>
</exec>
<echo>File sol_uncompressed.js packed to sol_packed.js</echo>
</target>
</project>
So basically, this build.xml does the following:
- Concatenate the three files in the js folder and save the result as
sol_uncompressed.js. - Replace ‘__version__’ with ‘0.1.1′ in the
sol_uncompressed.jsfile. - Pack
sol_uncompressed.jswith Dean Edwards packer and save it assol_packed.js
You can run the build file by left clicking the file in the Project View and do Run as > Ant Build.
Running build.xml to build the project using Ant.
As you can see in the build file I’m executing ‘cmd’ with some parameters to pack the sol_uncompressed.js file. I used the WSH version of packer. This version can be executed from the commandline. It seems this WSH port of packer is not yet version 3.0, but Dean said on his blog the ports of the latest version are ready to be released.
Download
If you want to try it yourself make sure you grab the zipped Project folder of the Project above. I recommend you read the Ant manual. It has lots of code examples that get you up and running in no time. If you have any questions, don’t hesitate to ask!
Hey Cool That was very descriptive I need to try it myself. and hope to see some more cool things in plotr.