41

Is it possible to tell which directory the user ran Ant from?

For example, I might want to run only the unit tests in the current working directory, rather than all tests for the entire project.

I tried this:

<property environment="env" />
<echo>${env.CWD}</echo>

but that doesn't work.

Asaph
  • 159,146
  • 25
  • 197
  • 199
JW.
  • 50,691
  • 36
  • 115
  • 143
  • 1
    CWD is not part of a typical Windows set of environment variables. How are you setting it? The answer @Asaph provided is way more strightforward. – DaveParillo Oct 20 '09 at 18:16

4 Answers4

66

The whole Properties object returned by System.getProperties() is exposed by Ant. Try this:

<echo>${user.dir}</echo>
zb226
  • 9,586
  • 6
  • 49
  • 79
Asaph
  • 159,146
  • 25
  • 197
  • 199
45

${basedir} might also be useful.

In Netbeans under Linux, ${user.dir} seems always to equal ${user.home}, but ${basedir} gives me actual directory where ant is ran from (so it gives me the project dir).

java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
  • 8
    By default, basedir is the parent directory of the buildfile. This is not necessarily the same as the *working directory.* – Andy Thomas Jun 28 '11 at 21:40
  • That actually is exactly my problem. I can't figure out how to get my working directory to be some other directory when I invoke my ant script from a different ant script in another directory. I see user.dir is the directory of the calling ant script, and basedir is the directory that I want to execute my ant script from. I call my ant script using ``, but that only changes the basedir and not the current working directory. – searchengine27 Aug 09 '16 at 19:43
  • ${basedir} resolves to the directory containing the build file when/if you execute Ant with the *-f somebuildfileinsomeotherpath* flag. So that's not a good flag to use to get the current directory from where you are invoking ant. – luis.espinal Mar 21 '19 at 15:54
1

Unless you're using Windows, this will give you the current working directory:

<exec executable="sh" outputproperty="my.cwd"><arg line="-c pwd"/></exec>
0

and under windows

<exec executable="cmd" outputproperty="my.cwd"><arg line="/C echo %cd%"/></exec>

see also Using ant to detect os and set property

culmat
  • 1,156
  • 11
  • 12