Nov 24, 2007

Ant scripting

Table of contents

  1. Introduction
  2. Define the project
  3. Define entry points
  4. Code snippets
    1. Use ant as a template engine
    2. Use ant to auto-compress your code
  5. Ressources

Introduction

Hy there, I just started to use Ant build, and I figured out that starting learning it wasn't as obvious as expected.
I first read the Manual homepage but it wasn't clear enought for a stupid guy as I am, so I decided to create this post with some Javascript dedicated code snippets and a (very) quick introduction to ant scripting. I hope it will be usefull to someone ^^

Table of content

Define the project :

Ant script as every XML document has to declare a root node. This root node is a project node and can take following arguments :

name :
your build project name
default :
your build default entry point
basedir :
the directory used as base for path evaluation

You can add to your script a description via the description tag, just put plain text as done here

<project name="yourProjectName" default="defaultTask" >
 <description>
   This project does....
 </description>
</project>

Table of content

Define the entry points :

An entry point in an Ant script is a stand alone task. It is represented with a target node. This node can accept following attributes :

name :
your task / entry point name
depends :
more or less the scripts to execute before.

So as an ant build is generally used to create or deploy a project, let's assume there is at least the two following tasks : CLEAN and DEPLOY. So our script now looks like :

<project name="yourProjectName" default="defaultTask" >
 <description>
   This project does....
 </description>

 <target name="CLEAN">
    <!-- include your tasks here -->
 </target>

 <target name="DEPLOY">
    <!-- include your tasks here -->
 </target>

</project>

So, here is our first ant script with two tasks. You can run it in any eclipse environnement by dragging this xml file to the ant view and double-clicking on one of the created tasks name.

Before going further, let's see 2 usefull functionnalities : how to call a task within your tasks and print a message.

To print a message use the echo tag. Two ways are possible :
Just print your message between the node like this :

<echo>
  Your message to print
</echo>
or use the message attribute :

<echo message="Your message to print" />

Now to call a task while processing an other, just use the antcall tag.
For example to call clean while processing the deploy task, just modify previous code with following :

<target name ="DEPLOY">
    <!-- include your tasks here -->
    <antcall target ="CLEAN">
 </target>

Easy isn't it ?
Well before giving some usefull(?) code snippets for Javascript developpement, here is the ant core functions references

Table of content

Code snippets:

Use ant as template engine:

Didn't you dream of a world in which you can define the version of your project in only one place ? A world where you can change your library namespace as many time as you want ?
So, Ant makes it possible whatever the platform you're working on !

First of all, you have to define all your variables in a myFile.properties file. Then, everywhere you want thoose variables to be replaced, just write it into dollar baces :

  • ${myVar}
  • ${PROJECT_VERSION}

Then in your myFile.properties file, define your values in following way :

  • myVar=MyVarReplaced
  • PROJECT_VERSION=1.0

It is recommended you externalizes the variables used for input and output :

  • destDir=absolute/path/or/relative/to/basedir
  • sourceDir=absolute/path/or/relative/to/basedir

So, here we are ready to process the datas using following code :

<target name="REPLACE_VAR" >

   <loadproperties srcfile="myFile.properties">

   <copy todir="${destDir}" overwrite="true" includeEmptyDirs="true" >
     <fileset dir="${sourceDir}" />
       <filterchain>
          <expandproperties/>
       </filterchain>
   </copy>

 </target>

To go further, just visit the filterChain manual page and especially the expandproperties section.

Table of content

Use ant to auto-compress your code:

Javascript is a a script language, so it is delivered as you publish it with comments and whitespaces. Well what if I tell you that you can compress it up to 80% ! Let's see how to do that ;)

First, you need to install some JAR files defining the functions we need ANT to use. I'm using Eclipse 3.3, so I will detail how to do with this platform. If you uses something else, just search the web !

  1. open the window > preferences pannel
  2. Choose the Ant > Ressources in the left box
  3. Select the ClassPath tab and click on global entries to enlight the line
  4. Press the Add external JARs... button
  5. select your JAR file and validate. You're now ready to use the newly intalled library !

I choosed the DOJO compressor ( relying on rhino ) you can download on the lcasoft website.
Just add rhino.jar and compress-js.jar to your ant libraries to make the compress-js function available in your ant build.
Let's see an example on how to use it :

 <!-- Import the task -->
 <taskdef name="compress-js" classname="com.webpanes.tools.ant.taskdefs.CompressJS" classpathref="cp"/>

 <target name="COMPRESS_CODE">
    <compress-js file="myFile.js" tofile="myCompressedFile.js"/>
 </target>

To go further you can read the compress-js documentation

Table of content

Ressources :

Ressources :

Jar files :

Table of content

Nov 23, 2007

Ouverture

Ca y est, j'ai enfin décidé d'ouvrir un blog. Le but est de faire partager mes expériences de programmation, publier les quelques bouts de code que je développe, et surtout concentrer en un seul lieu l'ensemble des articles que je trouve intéressants. bonne lecture à tous ! (bon ok, bonne lecture à moi ;p) Ce premier article est en francais, mais comme tout outil technique, les prochains postes seront en anglais.