File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Ant, Maven and Other Build Tools and the fly likes learning Ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "learning Ant" Watch "learning Ant" New topic

learning Ant

Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
Hey all, I am trying to learn ANT and would love some help if there is any out there. I am trying to do something along the lines of moving every file that I have in a directory over to an Oracle database using the loadjava utility in Oracle. I am guessing that I will have to do this by:
1. looping through some kind of control statement while reading each .java file name in my directory

2. using the current value of some variable to specify what .java file I am moving (each loop through)

Here is what my load java command line argument looks like when I do it manually:

I�m not looking for a free answer to my problem, maybe just a point in the right direction. I�ve been to the manual at to read up a bit. After what I�ve read so far I think I may want to use the <arg/> tag, but I don�t know how I�m going to dynamically get all of the file names in a directory or how to do the looping. Here�s where I�m stuck in my buildfile (this could be WAY off from what it should be):

Any info would be great, Thanks!


I just saw the link at the top of the ANT, Maven and Other Build Tools forum page. I'll try looking there really quick.

[ July 25, 2005: Message edited by: Chris Staten ]
[ July 25, 2005: Message edited by: Bear Bibeault ]
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
Probably could have chosen something a little easier for your first build.xml

To answer your question about dynamically getting the filenames, you need to use a fileset, filelist, or path.

Gives you a list of all the files that end in .java (inc subdirs), in the dir specified by

Now, I anticipate your next question will behow do I loop in ant? (unless of course, loadjava can take a whole list of java files separated by ;, in which case you're done). Ant doesn't come with any looping facility, so you have to turn elsewhere for that.

Get ant-contrib from

Then you can do something like (and this almost a free answer, and should be enough to get you going)

Where the target loadjava is defined pretty much how you have it, you have the it somewhat wrong, you should be using the exec task which has nested arg elements. Each arg element is a single parameter, so you would have 4 nested arg elements, one each for "-thin" "-user" "scott/tiger@localhost:1522:TNSNAME" and ""

Mileage may vary depending on whether loadjava is a batch file or not when using exec (the ant docs tell you why)- in fact, unless you are set on learning ant, then I would have written a batch file to do this, if I'm not mistaken you could have something like :-

Which is both shorter, and less well documented, thus keeping you in a job while you do something more interesting


I have no java certifications. This makes me a bad programmer. Ignore my post.
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
I anticipate your next question will be how do I loop in ant?

Very perceptive!

Thank you very much for your reply. I'm also thinking about getting a book on ANT. I know that the manuals are at the Apache site, but I think it would be nice to have a few more examples as well. I saw a review on slashdot about Ant: The Definitive Guide from O'Reilly, it seems like a good choice.

As for the batch file, you're right about me being set on learning ANT.

I do have a question that I can't seem to find even when searching online (although I may be over complicating this or reading right over the answer). How do you explicitly invoke a target? Do you just call the target from inside another target with an element? This didn't seem to intuitive to me when I followed a tutorial - the one that builds UKlights. It looked like every target was called via a dependency attribute; this works fine if you want the target called before the current target, but what if you want it called during a sequence of actions? I guess that I may be making this difficult for myself for no reason, but the confident feeling that comes with clarity feels so darn good
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

You have a variety of tasks which let you explicitly call another target:
  • ant
  • antcall (if the target is in the same build file)
  • subant (if the target is in sub project)

  • Be aware that all these tasks are not very performant. antcall for example will reparse the entire build file. ant has a ClassLoader per project too, so using the ant target will reload new instances of classes already loaded in the project you called it from.

    There are also other (stupider) ways to do this - invoking Ant via the java or exec tasks with a target defined for example. Or including an ant/antcall in a macrodef and calling the macrodef. Or invoking ant from a script called via the script task. Like I say though: pretty dumb.

    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    Roseanne Zhang
    Ranch Hand

    Joined: Nov 14, 2000
    Posts: 1953
    From my experiences, the easiest way to learn ant is download a relative simple open source Java project, almost all of them are built by ant. Run the ant script, see the effect, play with it, make changes on it, and let it do different things. If you break it, try to fix it, or start over again.

    After 1-2 days, you are ready to do your own ant script. No book needed for me, ant manual is enough. I'm automating a huge companies's entire building, testing, distribution, installation processes.
    Roseanne Zhang
    Ranch Hand

    Joined: Nov 14, 2000
    Posts: 1953
    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 ...
    Chris Staten
    Ranch Hand

    Joined: Sep 24, 2004
    Posts: 101
    OK, I�m getting a bit excited here with ANT so bear with me please

    I�ve changed the way I�m going to build my java files and move them over to the database, here are my new steps:

  • I�m going to compile all of my .java files
  • Jar up the .class files
  • move the .jar file to the database using the loadjava utility
  • move the .jar file to a different directory
  • Then I�m going to use the SQL tag to create all of my PL/SQL wrappers (i.e. functions) in the database

  • I just need to figure out a few things. First thing I�m trying to figure out is which JDBC driver should I be using? In my datasource I link to Oracle�s classes12.jar for my JDBC classes, but I don�t link to a specific package or class. The way I read the SQL tag property for driver is that it requires a specific class. Anyone know how I can figure out just which class I need?

    The other thing I�m looking into is if I will need to change the way my PL/SQL wrapper calls a java stored procedure when it�s in a jar file.
    Roseanne Zhang
    Ranch Hand

    Joined: Nov 14, 2000
    Posts: 1953
    I tried sql tag 2 years ago, it was not very flexible since it did not allow me to use JNDI then, I wrote a customized ant task to do my job. Of course, my knowledge might be out-of-date...

    Just some thing I knew, which might not be helpful at all, but may help you to think in different paths...
    Chris Staten
    Ranch Hand

    Joined: Sep 24, 2004
    Posts: 101
    I think I may have found the right driver for using the SQL tag, I'm looking at using oracle.jdbc.driver.OracleSql.

    Here's what my SQL tag will look like:

    I'll have all of my PL/SQL wrappers in the sql.txt flat file.

    Chris Staten
    Ranch Hand

    Joined: Sep 24, 2004
    Posts: 101
    OK, I've all but finished this build file! Here's what it looks like now:

    A few things to note.

    1. I may have a few more things in the build file than I need, this is because after I learned how to do them I wanted to find a way to use them
    2. I may be doing the same type of thing two different ways, this is for the same reason as above.
    3. I could not get Oracle's dropjava utility to work with ANT (you can see it commented out in the code above) so I created a batch file and I just call it from my build file. I don't like this implementation, but it's working for now.
    4. I can't get the tstamp task to work right, I've read the manual and I just can't make it work. All I ever get is "" as my time. If anyone has any idea what I'm doing wrong please let me know .

    All in all I'm happy with my first time playing with ANT, these were my first steps into the world of build tools and it's amazing how much time they can save.
    [ August 16, 2005: Message edited by: Chris Staten ]
    I agree. Here's the link:
    subject: learning Ant
    It's not a secret anymore!