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".
假设我有一个具有一组微服务的Monorepo,这些微服务形成了一个单一的分布式系统。它们都是基于JVM的,并使用最新的Gradle作为构建系统。它不是一个包含多个模块的单一Gradle项目。每个应用程序都有自己的独立项目(settings.gradle.kts),尽管它们与所谓的“传统Gradle插件”共享一些构建逻辑。
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".
作为一名开发人员,我希望能够在IntelliJ中导入单个微服务项目,以避免IDE做索引和重新索引整个代码库的繁重工作。没问题,因为每个微服务都是Gradle项目,所以我可以单独导入每个微服务。但当我再添加一个需求时,问题就出现了:“当我导入任何单个微服务/项目时,我希望使用位于存储库根目录中的单个Gradle包装器”。
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:
在下一句话中,Gradle Docs增加了一个小障碍:
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?
这正是我的问题所在。如何手动配置IntelliJ使其指向正确的包装器?
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:
如果我在根目录中只有Gradle包装器(就像Gradle文档中这个非常具有说明性的构建组织示例),那么在终端中,我可以很容易地使用根目录中的包装器,如下所示:
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:
但如果我只想在IntelliJ中导入一个Gradle项目(它不包含自己的gradlew文件):
- 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?
..。IntelliJ不会从根目录使用包装器,而只是生成一个缺失的包装器,并将一些Gradle版本硬编码到当前的IntelliJ发行版中。这种自动行为是意料之中的:IntelliJ不知道包装器在哪里,如果它不在Gradle的项目根目录中(以及settings.gradle.kts)。因此,在这种情况下,我希望将IntelliJ配置为将其指向正确的gradlew,但我在IntelliJ中找不到相应的设置。是否可以在IntelliJ设置中的任何位置配置Gradle包装器的自定义位置?
更多回答
I don't think IDEA has a such feature. Closest option is to point each Project to a local Gradle installation (not a Wrapper), but I don't think this would be a viable solution for you. Might be worth creating an Issue in IDEA's YouTrack youtrack.jetbrains.com/issues/IDEA
我不认为Idea有这样的特点。最接近的选择是将每个项目指向本地Gradle安装(而不是包装器),但我认为这对您来说不是一个可行的解决方案。可能值得在IDEA的YouTrack Youtrack.jetbrains.com/Issues/Idea上创建一个问题
Based on @romanv-jb comment, right now it's not possible to configure custom location for Gradle wrapper in Intellij.
基于@Romv-JB的评论,目前无法在IntelliJ中配置Gradle包装器的自定义位置。
IDEA-332154 feature request created. Follow the link and give it a "thumbs up" if you are also interested in this feature.
已创建IDEA-332154功能请求。如果您也对此功能感兴趣,请点击链接并竖起大拇指。
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
:
就目前而言,如果您希望硬锁定供IntelliJ使用的Gradle版本,同时希望避免提交额外的非根Gradle包装器,只需在您的Build.gradle.kts中使用以下DSL:
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.
有了上述包装器任务配置,IntelliJ将使用您显式指定的版本将您的项目导入到IDE中。
更多回答
我是一名优秀的程序员,十分优秀!