Passing --project-dir seems to have no effect when paired with --build-file

I want to publish a build script for my coworkers. The build script lives in a read-only directory away from the project. In order for many things (e.g. Java plugin source sets) to work correctly, the project directory needs to be a directory other than the one that contains the build script.

Gradle has a command line option --project-dir to configure this, but it seems to have no effect when paired with --build-file. Example below.

// subdirectory/build.gradle
println project.rootDir
println project.projectDir

 

$ gradle --build-file subdirectory/build.gradle
$PWD/subdirectory
$PWD/subdirectory
$ gradle --build-file subdirectory/build.gradle --project-dir $PWD
$PWD/subdirectory
$PWD/subdirectory

I’ve tried playing with the current directory and with settings.gradle, but to no avail. I’m hoping I just don’t know how to do it right, and that there is no bug here.

I am running Gradle 3.4.

Related issue:

What is the reason for not putting the buildscript to the project directory? If the reason is, that this buildscript is used for several projects, you should probably still have one buildscript per project and then apply your common file with apply from: 'your/common/file.gradle', or even make your common stuff a plugin they apply, e. g. if you have some company repository which you can use for dependencies.

Indeed. I am using that first method (apply from) for now, but I want to save users from having the clutter of a file with a single, standard line, if possible. Is it possible?

Well, as you seem to have no problems with parameters, you could change your common file to be an init script instead, that should work. Or you can change it to be a settings script and apply it in the settings.gradle file.

You could package up a Gradle distribution containing an init script that applies your custom logic. Your colleagues then just point their wrapper.properties at that distribution. Though I’m honestly not sure what’s so bad about having a single line of code in the root project. That way you see that something special is being applied.

1 Like

The init script worked nicely. I just had to wrap everything in a closure for allprojects. Thank you!