SOS

Shane O'Sullivan's technical blog… really ties the room together

Setting the working directory for an Ant Java task

Posted by Shane O'Sullivan on January 3, 2007

Just a tiny Ant tip – I’ve been fooling around with Apache Ant for the first time, and needed to change the working directory so that, rather than it being the folder in which the build.xml file is located, it be somewhere else. This enables files in other directories (which I don’t control) to use relative paths to other files. I tried setting the System’s “user.dir” and “basedir” properties, but these didn’t work.

So, the solution is very simple, in your <java> task, set “fork” to true, and then set the “dir” property to the folder you want to use as a working directory.

e.g.

<target name=”doMyBidding”>

<java fork=”true” dir=”c:\work\lib”>

……..

</java>

</target>

No doubt this has been documented somewhere else, but a quick search on Google didn’t readily find it for me, so here you go. Enjoy!

About these ads

10 Responses to “Setting the working directory for an Ant Java task”

  1. Arnold said

    Thank you very mutch. I was looking for this for a long time. Think that it was so simple :)
    Chears
    /Arnold

  2. Hi Arnold,

    You’re very welcome – I think a lot of people could be struggling with this. It doesn’t seem to appear anywhere when I google it.

    Shane

  3. Chris said

    I had the same problem and this came up in google, thanks for the tip Shane!

    Chears
    /Chris

  4. Mayur Naik said

    Thanks for taking the time to write down this useful tip … I too came across it by googling.

  5. Samir Savla said

    Thanks for this…I googled too and saw this info and got the solution to a problem i was stuck on :)

  6. Chris, Samir and Mayur,

    You’re all very welcome :-)

    Shane

  7. I was looking to use the current directory as my working directory, with the build file located in a fixed location. It turns out that a command line option -Dbasedir=. will set the working directory, without needing to then supply the dir attribute for the java task. As in:

    ant -buildfile C:\whatever\build.xml -Dbasedir=.

  8. Eugene said

    Oh Dear, thanks a lot. %Tears of happiness%

  9. shobana said

    Hi thank u so much for ur help

  10. noboundaries said

    Thanks much! It works for me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 533 other followers

%d bloggers like this: