Hello,
 
I'm sort of surprised nobody has mentioned having this problem before...
 
I want to substitute a class path from my build file into configuration files for my application. However, the way that Ant works right now, it converts paths to canonical paths, which causes a problem for me because I use symbolic links under unix (solaris) which I don't want resolved to their canonical form because the canonical path won't necessarily match what is on all the machines the application will be deployed on, but the absolute path will.
 
This patch solves my problem; if you set a property "ant.project.useabsolute" then Project.resolveFile() will use getAbsolutePath() rather than getCanonical().
 
It has patches to the Project.java and Path.java file.
 
Julian.
Index: src/main/org/apache/tools/ant/Project.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.53
diff -u -r1.53 Project.java
--- src/main/org/apache/tools/ant/Project.java  2001/02/03 14:45:07     1.53
+++ src/main/org/apache/tools/ant/Project.java  2001/02/14 02:45:05
@@ -538,16 +538,31 @@
     }
 
     public File resolveFile(String fileName) {
+        String useAbsolute = getProperty("ant.project.useabsolute");
+        if (useAbsolute != null && toBoolean(useAbsolute)) {
+            return resolveFile(fileName, true);
+        }
+        else {
+            return resolveFile(fileName, false);
+        }
+    }
+
+    public File resolveFile(String fileName, boolean useAbsolute) {
         fileName = fileName.replace('/', File.separatorChar).replace('\\', 
File.separatorChar);
 
         // deal with absolute files
         if (fileName.startsWith(File.separator)) {
-            try {
-                return new File(new File(fileName).getCanonicalPath());
-            } catch (IOException e) {
-                log("IOException getting canonical path for " + fileName 
-                    + ": " + e.getMessage(), MSG_ERR);
-                return new File(fileName);
+            if (useAbsolute) {
+                    return new File(new File(fileName).getAbsolutePath());
+            }
+            else {
+                try {
+                    return new File(new File(fileName).getCanonicalPath());
+                } catch (IOException e) {
+                    log("IOException getting canonical path for " + fileName 
+                        + ": " + e.getMessage(), MSG_ERR);
+                    return new File(fileName);
+                }
             }
         }
 
@@ -596,14 +611,18 @@
                 file = new File(file, part);
             }
         }
-
-        try {
-            return new File(file.getCanonicalPath());
-        }
-        catch (IOException e) {
-            log("IOException getting canonical path for " + file + ": " +
-                e.getMessage(), MSG_ERR);
+        if (useAbsolute) {
             return new File(file.getAbsolutePath());
+        }
+        else {
+            try {
+                return new File(file.getCanonicalPath());
+            }
+            catch (IOException e) {
+                log("IOException getting canonical path for " + file + ": " +
+                    e.getMessage(), MSG_ERR);
+                return new File(file.getAbsolutePath());
+            }
         }
     }
 
Index: src/main/org/apache/tools/ant/types/Path.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/types/Path.java,v
retrieving revision 1.14
diff -u -r1.14 Path.java
--- src/main/org/apache/tools/ant/types/Path.java       2001/01/12 14:08:51     
1.14
+++ src/main/org/apache/tools/ant/types/Path.java       2001/02/14 02:45:05
@@ -112,16 +112,7 @@
         private String[] parts;
 
         public void setLocation(File loc) {
-            try {
-                parts = new String[] {translateFile(loc.getCanonicalPath())};
-            } catch(IOException e) {
-                // XXX I'd like to log something here but if I don't
-                //     have a Project I can't
-                if (project != null) {
-                    project.log(e.getMessage(), Project.MSG_WARN);
-                }
-                parts = new String[] {translateFile(loc.getAbsolutePath())};
-            }
+            parts = new String[] 
{translateFile(project.resolveFile(loc.toString()).toString())};
         }
 
         public void setPath(String path) {
@@ -306,11 +297,7 @@
                 for (int j=0; j<s.length; j++) {
                     String canonicalPath;
                     File f = new File(dir, s[j]);
-                    try {
-                        canonicalPath = f.getCanonicalPath();
-                    } catch(IOException e) {
-                        canonicalPath = f.getAbsolutePath();
-                    }
+                    canonicalPath = 
project.resolveFile(f.toString()).toString();
                     addUnlessPresent(result, translateFile(canonicalPath));
                 } 
             }
@@ -447,12 +434,7 @@
     private static String resolveFile(Project project, String relativeName) {
         if (project != null) {
             File f = project.resolveFile(relativeName);
-            try {
-                return f.getCanonicalPath();
-            } catch(IOException e) {
-                project.log(e.getMessage(), Project.MSG_WARN);
-                return f.getAbsolutePath();
-            }
+            return f.getAbsolutePath();
         }
         return relativeName;
     }

Reply via email to