Table of contents
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 ^^
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>
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>
<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
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.
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 !
- open the window > preferences pannel
- Choose the Ant > Ressources in the left box
- Select the ClassPath tab and click on global entries to enlight the line
- Press the Add external JARs... button
- 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
Ressources :
Ressources :
Jar files :
- lcasoft website for compress-js
- Ant contrib. To go further just read the Ant-Contrib manual and the tasks definitions
1 comment:
Post a Comment