Search code examples
gradleintellij-ideagradlew

How to configure Intellij to use Gradle wrapper from directory other than project's root?


Suppose I have a monorepo with a bunch of microservices which form a single distributed system. All of them are JVM-based and use the most up-to-date Gradle as a build system. It's not a single Gradle project with multiple modules. Each app has it's own independent project (settings.gradle.kts), even though they share some build logic with so-called "conventional Gradle plugins".

As a developer, I want to have an ability to import single microservice project in Intellij to prevent IDE from doing the hard work of indexing and re-indexing the whole codebase. No problem, since each microservice is a Gradle project, I can import each one individually. But the problem appears when I add one more requirement: "when I import any single microservice/project, I want to use single Gradle wrapper located in repository's root".

Why?

  • because I do not want more than one Gradle wrapper's binary in my monorepo
  • because I want one place where Gradle version is declared (gradle-wrapper.properties of the repo's root)
  • and because based on Gradle docs it's a recommended approach:

If all components live in the same repository, you should only have one Gradle wrapper in the root of the repository. If you have an umbrella build there, you can use that to manage the wrapper.

And right in the next sentence Gradle docs add one small snag:

However, if you import an individual component in an IDE, it might have issues finding the wrapper and you might need to configure a Gradle installation manually.

This is exactly what my question is about. How to configure Intellij manually to point it to the right wrapper?

If I only have Gradle wrapper in the root (like this very illustrative build organisation example from Gradle docs), in terminal I can easily use wrapper from the root, like this:

cd server-application
../gradlew :clean :build # note the '../'

But if I want to import only single one Gradle project (which does not contain it's own gradlew files) in Intellij:

  • either by opening single sub-directory in Intellij
  • or by unlinking root umbrella project and by right-clicking project's *.gradle.kts file and then → "Link Gradle project" context menu option

... Intellij won't use wrapper from the root and will simply generate a missing one instead, with some Gradle version hard-coded into the current Intellij release. This automatic behavior is quite expected: Intellij does not know where the wrapper is, if it's not in the Gradle's project root (together with settings.gradle.kts). So, in this situation I would want to configure Intellij to point it to the right gradlew, but I'm failing to find corresponding setting in Intellij. Is it possible at all to configure custom location of Gradle wrapper, anywhere in Intellij settings?


Solution

  • Based on @romanv-jb comment, right now it's not possible to configure custom location for Gradle wrapper in Intellij.

    IDEA-332154 feature request created. Follow the link and give it a "thumbs up" if you are also interested in this feature.

    As for now, if you want to hard-pin Gradle version to be used by Intellij and at the same time you would prefer to avoid committing additional non-root Gradle wrapper, just use the following DSL in your build.gradle.kts:

    tasks.wrapper {
        distributionType = Wrapper.DistributionType.BIN
        gradleVersion = "8.3"
    }
    

    Having the above wrapper task configuration, Intellij will import your project to IDE with the version you specified explicitly.