1)What is ant?
Ant is a small animal who can build magnificent buildings. Ant builds!
ANT is a Java based building tool, which is similar to make, and so much better than make.
ANT, what a smart name for a building tool, even the original author of ANT, James Duncan Davidson, meant "Another Neat Tool".
A win-win ant learning method
There is a shortcut.
If you download a small jakarta project, such as Log4J, which is built by ant. It is a good and simple example for you to learn ant. Actually, you hit two birds with one stone.
Ant is easy!
The hard part is how to make a very complicated diversified system work very simple and elegant. Knowledge about ant is not enough, you need an elegant and simple design, you need great naming convention, you need to optimize the code reusability and flexibility, you need a least maintenance system...
Then it is not easy now ..
2)How do I get started to use ant? Can you give me a "Hello World" ant script?
Simple.
* Download the most recent version of ant from Apache; unzip it some where on your machine.
* Install j2sdk 1.4 or above.
* Set JAVA_HOME and ANT_HOME to the directory your installed them respectively.
* Put %JAVA_HOME%/bin;%ANT_HOME%/bin on your Path. Use ${JAVA_HOME}/bin:${ANT_HOME}/bin on UNIX. Yes, you can use forward slash on windows.
* Write a "Hello world" build.xml
* Type ant in the directory your build.xml located.
* You are ready to go!!!!
3)How to delete files from a directory if it exists?
The following code fails when directory does not exist!
Your code has many problems.
1. You should not use implicit fileset, which is deprecated. You should use nested fileset.
2. If dir does not exist, the build will fail, period!
3. If you are not sure, use a upper level dir, which exists for sure. See the following fileset.
4)How do I set classpath in ant?
Here is some snippet of code
5)How does ant read properties? How to set my property system?
Ant sets properties by order, when something is set, the later same properties cannot overwrite the previous ones. This is opposite to your Java setters.
This give us a good leverage of preset all properties in one place, and overwrite only the needed. Give you an example here. You need password for a task, but don't want to share it with your team members, or not the developers outside your team.
Store your password in your ${user.home}/prj.properties
pswd=yourrealpassword
In your include directory master prj.properties
pswd=password
In your build-common.xml read properties files in this order
1. The commandline will prevail, if you use it: ant -Dpswd=newpassword
2. ${user.home}/prj.properties (personal)
3. yourprojectdir/prj.properties (project team wise)
4. your_master_include_directory/prj.properties (universal)
8)How to loop on a list or fileset?
Use ant-contrib
General to say, use
9)Why do I get en exception when I use location="D:\\Code\\include" as attribute of includepath?
See here.
You need to escape the string to "D:\\\\Code\\\\include" or use "D:/Code/include" instead!
Believe me or not? Forward slash works on windows in all ant or java code. It also works in windows environment variables. It does not work in cmd (dos) window before XP. It also works in XP dos window now!
10)Can I put the contents of a classpath or fileset into a property?
Yes, you can.
This is very similar to the call of Java class toString() method and actually it is calling the toString() method inside ant. For example
No comments:
Post a Comment