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

(rupert THURNER) #1

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?

(Szczepan Faber) #2

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!

(rupert THURNER) #3

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 ?

(Luke Daley) #4

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.

(rupert THURNER) #5

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

(Luke Daley) #6

In what way is it non standard?

(rupert THURNER) #7

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

(Luke Daley) #8

Is this when running with the daemon?

(Stephen Holstein) #9

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.

Passing --project-dir seems to have no effect when paired with --build-file
(xavier.figueroa) #10

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')

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


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


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