Changing rootProject projectDir from settings.gradle does not work


(Björn Kautler) #1

The User Guide says that you can change the location of the root project from settings.gradle. But I cannot get this working properly. If I change ‘’‘rootProject.projectDir’’’ or ‘’‘project(’:’).projectDir’’’ in settings.gradle it seems to have no effect. Furthermore, if I add subprojects, they are not not found if I tried to change ‘’‘rootProject.projectDir’’’ or ‘’‘project(’:’).projectDir’’’ in settings.gradle.


How to change gradle root project after we rename folder
Passing --project-dir seems to have no effect when paired with --build-file
(Peter Niederwieser) #2

Please give a minimal self-contained example and explain the desired vs. actual outcome (i.e. how do you tell that it doesn’t work).


(Björn Kautler) #3

Sure, here the example: https://github.com/Vampire/change-root-project-dir-showcase

Command: ‘’’./gradlew test -c settings1.gradle && ./gradlew test -c settings2.gradle’’’

Actual oucome:

D:\cygwin\home\bkautler\testing
D:\cygwin\home\bkautler\testing\root\sub
:test
project ':sub'
:sub:test
  BUILD SUCCESSFUL
  Total time: 2.518 secs
D:\cygwin\home\bkautler\testing
:test
  BUILD SUCCESSFUL
  Total time: 2.474 secs

Expected oucome:

D:\cygwin\home\bkautler\testing
D:\cygwin\home\bkautler\testing\root\sub
:test
project ':sub'
:sub:test
  BUILD SUCCESSFUL
  Total time: 2.518 secs
D:\cygwin\home\bkautler\testing\root
D:\cygwin\home\bkautler\testing\root\sub
:test
project ':sub'
:sub:test
  BUILD SUCCESSFUL
  Total time: 2.474 secs

or

Command: ‘’’./gradlew :test :sub:test -c settings1.gradle && ./gradlew :test :sub:test -c settings2.gradle’’’

Actual oucome:

D:\cygwin\home\bkautler\testing
D:\cygwin\home\bkautler\testing\root\sub
:test
project ':sub'
:sub:test
  BUILD SUCCESSFUL
  Total time: 2.76 secs
D:\cygwin\home\bkautler\testing
  FAILURE: Build failed with an exception.
  * What went wrong:
Project 'sub' not found in root project 'testing'.
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED
  Total time: 2.599 secs

Expected oucome:

D:\cygwin\home\bkautler\testing
D:\cygwin\home\bkautler\testing\root\sub
:test
project ':sub'
:sub:test
  BUILD SUCCESSFUL
  Total time: 2.76 secs
D:\cygwin\home\bkautler\testing\root
D:\cygwin\home\bkautler\testing\root\sub
:test
project ':sub'
:sub:test
  BUILD SUCCESSFUL
  Total time: 2.76 secs

Note: The build.gradle next to the settingsX.gradle files is only there to have the test task working as the root project directory did not get changed. In the correctly working version it should not be present!


(Peter Niederwieser) #4

Setting ‘rootProject.projectDir’ indeed gets everything derailed. Raised GRADLE-3086. Thanks for the report.


(Björn Kautler) #5

Ok, so it is at least not me trying to do it wrongly. :slight_smile: Btw. I tested it with 1.0 and 1.11 and it behaved the same, so this seems to be there for long already. I guess no-one ever changes the root project dir, at least not up to now.

Btw. I found another interesting edge-case. Probably related. I added settings3.gradle to the showcase. If you call the command with this file, you get the output

FAILURE: Build failed with an exception.
  * What went wrong:
Could not select the default project for this build. No projects in this build have project directory 'D:\cygwin\home\bkautler\testing'.
> No projects in this build have project directory 'D:\cygwin\home\bkautler\testing'.
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED
  Total time: 2.219 secs

(Gradle Jira) #6

Some more information. I see now that settings3.gradle is working as expected as far as it gets, as it complains that the directory where I invoke gradle is not part of the build which is kind of correct as it is no project directory anymore but only the settings directory. May I suggest to use the root project as default project in this case? Adding {{-p root}} to the command line with settings3, resulting in {{./gradlew :test :sub:test -c ./settings3.gradle -p root}} will yield again the same result as settings2.gradle, except that the root project is correctly the one from {{root}}, not from {{master}}, just the sub project is missing.

Furthermore, {{cd root && …/gradlew :test :sub:test -c …/settings2.gradle}} and {{./gradlew :test :sub:test -c ./settings2.gradle -p root}} work correctly as I just found out. So it just is broken if you call gradle from the settings dir as it seems.