22

I don't see any obvious problems, but I'm wondering if it's ok to use / rather than File.separator when I'm writing my build files. Using File.separator makes it very difficult to read some of the paths. Ex:

dependsDir = "${buildDir}${File.separator}depends"

vs

dependsDir = "${buildDir}/depends"
Ryan J
  • 2,502
  • 5
  • 31
  • 41

4 Answers4

29

The forward slash (/) is a legal path separator on Windows, as well as Unix (including Linux and Mac OSX). So unless you need the build to run on other operating systems, it shouldn't be a problem.

Avi
  • 19,934
  • 4
  • 57
  • 70
8

Gradle for the most part just relies on java.io.File to do all path related operations, which in turn gracefully handles / on both Windows and Linux. So using / in Gradle API is unlikely to cause any problems.

I am using / in a fairly large project which runs on both Windows and Linux, and so far I hadn't a single problem. Hope this helps.

rodion
  • 14,729
  • 3
  • 53
  • 55
2

When required, use the platform-independent constant File.separator eg. ${File.separator}.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
1

in File Class

 public static final String separator = "" + separatorChar;

where separatorChar is The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\'.
and the separatorChar created from

static private FileSystem fs = FileSystem.getFileSystem();
public static final char separatorChar = fs.getSeparator();

For you based on your operating system the separator will be changed, while using File.separator.
by using / in your code, it won't support for other OS.

Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90