3.9 Defining Platform-Independent Paths

3.9.1 Problem

You want to define paths that work on Windows, Unix, and other operating systems.

3.9.2 Solution

Define your paths, as shown in Recipe 3.8. Ant takes care of converting the paths to whatever platform you are running on. Use forward-slashes (/) between directories. Use either semi-colons (;) or colons (:) between paths; Ant handles both.

3.9.3 Discussion

Ant determines what operating system you are running on and converts paths accordingly. You should avoid Windows-style drive letters whenever possible; they will not work on Unix. If you must refer to a drive letter, use a system property as outlined in Recipe 3.6 to avoid hardcoding the path.

Use the pathconvert task to convert an Ant path to native format and store it in a property. Here is how you define a path and then convert it to Unix format:

  <path id="path.test">
    <!-- find all unit tests under the build directory -->
    <fileset dir="${dir.build}">
      <include name="**/Test*.class"/>
    </fileset>
  </path>  

  <!-- convert the path to UNIX format, storing it in a property -->
  <pathconvert targetos="unix" property="unixPath" refid="path.test"/>

3.9.4 See Also

See the Ant user manual for more examples of the pathconvert task.