Execute task from standalone gradle file

Viewed 933

I'm working on a pretty large project (140+ subprojects) and everything is build with gradle. Due to the amount of subprojects the configuration phase of the gradle build takes a while (30+ seconds). Gradle is not only used to build the projects but also to start external tools like a shell with correct environment, consul, ... These non-build tasks actually don't have any dependency to any other projects und thus could simply be executed without configuring all projects. These tasks also live in their own gradle files. For consul f.e. there is a consul-starter.gradle file in a gradle subdir of the root project. That is the structure looks like this:

root-project
  ├ gradle
  │  └ consul-starter.gradle
  ├ subproject 1 - N
  ├ gradlew
  ├ build.gradle
  └ settings.gradle

I want to execute the startConsul task in gradle/consul-starter.gradle without executing the configuration phase for all subprojects, to skip the 30 seconds config phase.

I tried to specify gradle/consul-starter.gradle as build file:

gradlew -b gradle/consul-starter.gradle startConsul

But gradle complains:

Build file '/path/to/root-project/gradle/consul-starter.gradle' is not part of the build defined by settings file '/path/to/root-project/settings.gradle'. If this is an unrelated build, it must have its own settings file.

I tried to specify an empty dummy settings file but got the same error (with the dummy file).

When I move the consul-starter.gradle from the gradle sub folder to the root-project folder execution of the task works and skips the complete configuration phase as desired, but I don't want to move the file to the root-project folder to keep things sorted.

Is there anything else I could do? I "simply" want the execute a task from a standalone gradle file...


Update 2020-04-06:

Indeed after putting an empty settings.gradle file next to the consul-starter.gradle solved the issue. I first tried to specify a settings file using gradle's --config command line switch. But for some reason this didn't work...

1 Answers

I suppose, your only chance of getting the startConsul task to run without configuring the rest of the build will be to run it as part of a separate/“unrelated” build. As you saw, simply changing the build file doesn’t suffice to make Gradle consider a new/unrelated build – as long as the only settings.gradle file is in a parent directory.

To create a separate build, you can simply add an empty settings.gradle file to your gradle/ directory and run your task as before:

./gradlew -b gradle/consul-starter.gradle startConsul

I’ve tried that with Gradle 6.3 and it works for me. (Not sure if that’s what you had tried, too, when you said you had specified “an empty dummy settings file”?)

Related