tags:

views:

77

answers:

2

Let's say I have a file-system that looks a little something like this:

  • C:\stuff\build.xml
  • C:\stuff\myfolder\library1.jar
  • C:\stuff\myfolder\library2.jar

Inside build.xml, I want to define a path that looks like this:

<path id="some.id">
    <fileset dir="myfolder">
        <include name="**/*.jar"/>
    </fileset>
</path>

Normally that works fine. However, I am calling my own custom Ant Task that will inherit any references (including the path "some.id") and that custom Ant Task will call a build.xml that lives in a different basedir. Therefore, the "dir" attribute in the fileset is no longer valid.

Is there a way to define a "dir" such that it remains valid no matter where the second build.xml lives?

I essentially want to do something like this:

<fileset dir="${expand.current.directory}/myfolder">

So when I call the second build.xml it will understand that the "dir" attribute is the location of:

<fileset dir="c:\stuff\myfolder">

Edit: Furthermore, I want a solution that allows me to copy the "stuff" project from one machine to another without requiring a change to the build. For example, if the "stuff" project is on the C: drive and I copy the project over to a D: drive on another machine, I want the build to continue to work without me having to go into the build and change the letter C to the letter D.

+1  A: 

Option 1: You can define an Ant property.

At the beggining of the file you can define this property:

<property name="myproject.root.path" location="C:/stuff/"/>

And then, use it:

<fileset dir="${myproject.root.path}/myfolder">

Option 2: You can also define it at an external build.properties file, located at the same folder in wich the build.xml file is.

File C:\stuff\build.properties

myproject.root.path=C:/stuff/

And, to make use of this file you have to add this line at the Ant XML file (recommended before the tasks definition):

<property file="build.properties"/>

Once you have this file included, you can use the properties along the project, the same way as seen at option 1:

<fileset dir="${myproject.root.path}/myfolder">

You can add more than one properties file.

Note that paths are defined using slashes, and not back-slashes.

Tomas Narros
Sorry I probably should have clarified in my question that I would like to make sure the solution is actually machine independent. If I were to copy the "stuff" project onto a machine that has a D: drive, then the above solution wouldn't work without updating the ant/properties file. Apologies for forgetting to add that in my original post.
digiarnie
That's a good reason to extract it to a properties file, where you could change the environment dependent values without the need to rewrite your build.xml file.
Tomas Narros
Other option could be what's told by martin clayton at the other answer, access the java runtime properties (as ${user.dir} ).
Tomas Narros
+1  A: 

I think you're after the ${user.dir} property - which is the current working directory.

All java System.properties are available as ant properties.

martin clayton