Defining Platform-Independent Paths

Problem

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

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.

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"/>

See Also

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

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.