Search code examples
javagradleintellij-ideamulti-module

Why does Intellij not like my Gradle build?


We have a Gradle v6.8.2 multimodule project that is structured like this:

.
├── searchapi
│   ├── build.gradle
│   └── settings.gradle
├── searchcommon
│   ├── build.gradle
│   └── settings.gradle
├── searchconfig
│   ├── build.gradle
│   └── settings.gradle
├── searchindexer
│   ├── build.gradle
│   └── settings.gradle
├── build.gradle
└── settings.gradle

The child modules have a number of overlapping dependencies (e.g. commons-lang) and we are specifying the version of these dependencies within the parent build.gradle using variables like this:

ext.commonslangversion = "[2,3)"

Then, within the child projects, we specify the dependency like this:

implementation "commons-lang:commons-lang:${commonslangversion}"

The top-level build itself works great from the CLI and within Intellij (v2022.1.4).

The issue I'm having is when I try to run code within one of the child projects from Intellij. Specifically, within the searchindexer module, I have a class that contains a main() method. When I right click and choose to run this method, I get a build error that says:

A problem occurred evaluating root project 'searchindexer'.
> Could not get unknown property 'commonslangversion' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Any thoughts?

To further confuse things, if I move the body of the main() method into one of my JUnit test class methods and execute the test from within Intellij, it builds and runs fine. WTF!


Solution

  • In a gradle multiproject build, you must have only one settings.gradle script, located in the root project level. Here you have put one settings file under each subprojec, so neither gradle nor intellij will consider this as a multiproject build: subprojects will be treated as standalone projects, and will not inherit configuration or properties set on root project level.

    just remove settings.gradle files from subprojects.