Manipulating directories in Java Ant

40 views Asked by At

I have an Ant xml file. In this file I want to set two properties based on the current working directory. The current working directory is always of the form /some/base/dir/SRC/sub/dir.

The first property must be set to the current working directory. The second property must be set to the part of the current working directory up to /SRC.

I can set the first property without any issue using the PWD environment variable , but I cannot figure out the second.

<property name="my.dir" value="${env.PWD}" />
<property name="src.dir" value="{what do I put here?}" />

I've heard this can be done with bash-style string manipulation (e.g. ${PWD%*/SRC}/SRC) using StringOps, but I cannot find any good examples.

1

There are 1 answers

0
martin clayton On

One approach is to use the <pathconvert> task, perhaps like this:

<property name="my.dir" value="/some/base/dir/SRC/sub/dir"/>

<pathconvert property="src.dir">
  <path path="${my.dir}"/>
  <mapper type="regexp" from="^(.*)/SRC" to="\1" />
</pathconvert>
<echo message="src.dir=${src.dir}" />

Which gives:

[echo] src.dir=/some/base/dir

There are other mappers available which might work better for you than regexp.