Using the ant script, you can make ant do almost everything. This flexibility comes with a price which is complexity. Yet, if you wrote the script well enough, you will be able to maintain it for years.
Installing Ant
- Download the Ant zip file from ant’s web site.
- Extract the zip file in an easy to use folder, let’s say c:\tools\ant.
- Add the ant bin directory, in this case c:/tools/ant/bin, to your system PATH variable.
You can do this by going to control panel / System / Advanced Tab / env. Variables then choose from system variables “PATH” . Now just add your bin directory, separated from the other values by semicolon. - Add the “ANT_HOME” env. Variable [c:/tools/ant] using the “New” button in the env. Variables screen.
- Make sure that the “JAVA_HOME” env. Variable is correctly set to the directory where your JDK is installed.
- Finally, once these steps are complete, you can run ant from the cmd.
Ant –version.
Apache Ant version 1.7.1 compiled on June 27 2008.
Ant basic concepts.
- Ant build files are written in very easy, straight forward XML using a quite readable set of tags.
- Ant build files are used to build one ,and only one project .
- Two basic tags which will make your build file.
-Target
-Task - Targets: Are the goals you want to achieve with your build process, these goals could be, compiling your source code, running your unit tests, and preparing a pre-release version of your project. Usually you define many targets in an ant build file
- Tasks: Let you define the piece of work which you want to get it done. For example, compiling some java source code using javac, or doing you unit testing using junt.
- There are three types of tasks: The core tasks: which are built into ant, and need no special configurations, the optional tasks: which are maintained by the ant team and delivered with ant, like Junit, Third part tasks: which are written and maintained outside the ant project.
- Ant version 1.7.1 is delivered with almost 100 core tasks, and around 50 optional tasks.
A simple ant build file.
- Ant doesn’t force and directory structure , but we are recommended to use the following.
- src directory: where you keep your Application Source Code.
- test directory: where you keep your unit test code.
- lib directory: where you put your project dependencies.
- build directory: where you put Build generated files.
- build/classes directory: will hold your compiled classes.
- build/test-classes directory: will hold your compiled test classes.
- dist directory: any distribution files, your project in jar or war format.
Now we will write a simple ant build file, but first am going to say what this build file does.
1-Create two directories, build and dist directory under you project root.
2-Build your source code.
3-Package you project in a form of a jar file.
4-Delete both the built classes and the dist directory.

- the first target is the init target, which is used to create build/classes, dist directories using the mkdir task and the attribute dir.
- my second target is compile, which as the name indicates it compile my source files. what ant needs to know is if that target depends on any other target, which in this case is true.we have to create the build/classes first before we can compile our classes. for that we used the depends attribute which indicate the compile target dependency on the init target. the task used in this target is javac, which is a straight forward task. I used the srcdir attribute to indicate my source file directory, and used the distdir to indicate where i want my compiled classes to go.
- My third target is package, which packages my compiled classes into one jar file using jar task.
- and finally the clean target, which is used to delete both build and dist directory using the delete task.
what i didn't mention here is the default attribute in the project tag. this attribute indicates the default target to run when using this build file and in this case the default target is "package".
Now, to use this build file.
- open the CMD.
- go to your project root folder (the folder where you keep your scr directory along with the build.xml file).
- type ant in the cmd
- set back and watch :).
you will get something like that.
init:
compile:
package: [jar] Building jar: D:\first\dist\tax-calculator.jar
BUILD SUCCESSFULTotal time: 0 seconds
by default ant looks for build.xml file in the directory where you stand, once it finds the build file and no target name is passed as a parameter, ant will look for the default target to run and in this case "package".
in case you want to run a specififc target, you can pass its name as a parameter for ant, it would look like this
C:>first ant clean
Buildfile: build.xml
clean:
[delete] Deleting directory D:\first\build
[delete] Deleting directory D:\first\dist
BUILD SUCCESSFULTotal time: 0 seconds
I hope this post is helpful. stay tuned for more about ant.
thank you my dearest friend, wait until you see part 2 :)
ReplyDelete用心經營的blog~您的部落格文章真棒!!..................................................
ReplyDeleteThank you nice, I'll try to submit part 2 soon
ReplyDelete