Error while following the MultiProject build user guide

(Hazem Mammu) #1

I am following the user guide at https://docs.gradle.org/5.0/userguide/multi_project_builds.html#multi_project_builds and trying to replicate the examples.

In a multi-project Gradle project, a task defined in the root project is modified in a subproject. When running the task from the subproject directory (subdirectory), Gradle complains that it doesn’t identify the task. It’s logical but the user guide assumes it will work fine and that is what is confusing. I hope someone can clarify this.

water/settings.gradle:

rootProject.name = ‘water’
include ‘bluewhale’,‘krill’,‘tropicalFish’

water/build.gradle:

allprojects {
task hello {
doLast{ task ->
println “I’m $task.project.name”
}
}
}

water/bluewhale/build.gradle

task hello.doLast{
println “- I’m the largest animal that has ever lived on this planet”
}

Console

water/bluewhale
$ ./gradlew.bat :hello

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘water\bluewhale\build.gradle’ line: 33

  • What went wrong:
    A problem occurred evaluating root project ‘bluewhale’.

Could not get unknown property ‘hello’ for root project ‘bluewhale’ of type org.gradle.api.Project.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1s

(James Justinic) #2

The code you’ve listed shouldn’t work. The hello task is defined in the root project called water and therefore you should have problems with the task hello.doLast { line for several different reasons, including syntax and attempting to overwrite an existing task. There are several different states in the example, and task should not be there for the state you’re in going through the example.

The bigger issue though is that bluewhale, not water is considered the root project of your build. You likely have a settings.gradle file in your water/bluewhale/ directory that should not be there. It would still work fine from the root with this, but not from the subdirectory as it will only search upwards until it finds settings.gradle, which should only be in the root.

1 Like
(Hazem Mammu) #3

That’s exactly what it was. It worked when I removed the settings.gradle file in bluewhale. The user guide did not have the file, I added it by mistake.

I didn’t know that settings.gradle should only be in the root. Thanks for the tip.

I guess that means if I have to name a subproject or depend on a sibling subproject, I should declare it in the root settings.gradle file. It would have been nicer if gradle did not stop at the first settings.gradle file it finds but I think there must have been really good reasons (like performance) for designing it that way.