Change gradle working directory in a standard operating system way: where gradle is launched, and not where the build.gradle is located

how is it possible to separate the build.gradle from the source code in a different directory? we have many users not using gradle so we do not want to check it into the source code (yet?), but provide a build script outside the source tree.

we tried with -b, -p but gradly strangely sets the working directory where the script is located. how do we set the working directory in a “standard” way, i.e. where the command is executed?

There is no way at the moment to force the ‘working dir’ of gradle process. Normally, we strongly recommend that your build.gradle should not depend on the working dir. Instead of new File(projectDir, “foo.txt”), just use file(“foo.txt”). Take a look at other file-related methods that are available on project object (file, files, fileTree, etc.)

Hope that helps!

how would this solve the problem? if the gradle file is in /usr/ci/build.gradle, and one want to build source code in /usr/tmp/workspace/myproject and one uses file(“foo.txt”) it tries to open it in /usr/ci ?

Hi Rupert,

The file() method (and friends) always resolve files relative to the project directory, not the JVM working directory. Defining all of your files this way makes your build agnostic to the working dir of the JVM.

Using file() with a combination of -b and -p should get you what you need.

hi luke, szczepan, what is the reason that gradle behaves non-standard with respect to the working directory?

In what way is it non standard?

System.getProperty(‘user.dir’)) returns the directory passed with -b, and not the directory where gradle/java is started.

Is this when running with the daemon?

I put my source repositories into the cloud and then need to build to a separate directory so that the build outputs are not constantly uploaded. As a workaround I’ve now created a separate local branch. In my experience tools like automake and cmake support building into a ‘current working directory’ rather than the build.gradle directory.

Hi, I am a new user, can anyone explain me the correct behavior of file() method? I did some testing using: GRADLE_HOME\samples\java\apiAndImpl\build.gradle

jar {
    def dir = new File('test')
    dir.mkdir()
...

if I run gradle jar inside apiAndImpl a test directory is created in apiAndImpl

GRADLE_HOME\samples\java\apiAndImpl\test

But if I run gradle -b apiAndImpl\build.gradle jar inside java directory, a test Directory is created in java instead of apiAndImpl

GRADLE_HOME\samples\java\test

is that behavior correct? I think in both cases a test directory should be created in apiAndImpl