如何使IntelliJ将Gradle源与build.gradle相关联? [英] How to get IntelliJ to associate Gradle sources with build.gradle?
问题描述
在为我的Java项目编写Gradle脚本时,特别是在编写 build.gradle 文件时,IntelliJ无法识别Gradle API.
When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
例如,诸如apply
,dependencies
configure
之类的Gradle方法调用在其下方显示为黑线,并且无法导航至方法声明,没有自动完成等.
For instance, Gradle methods calls like apply
, dependencies
configure
appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
我设法通过在构建的dependencies
块中添加compile gradleApi()
来解决此问题.但是,我不想在我的代码中有这种显式依赖.
I managed to work around this by adding compile gradleApi()
to the build's dependencies
block. However, I don't want to have this explicit dependency in my code.
我尝试编辑IntelliJ的项目结构,并在我的模块中添加了对Gradle库的依赖(尝试了 gradle-core 和 gradle-all ),没有效果.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
是否可以使IntelliJ将所有 build.gradle 文件与Gadle源关联起来?
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
推荐答案
我如下解决了这个问题:
I solved this problem as follows:
-
正如已经发布的答案中所述,配置gradle
-
更新
gradle/wrapper/gradle-wrapper.properties
文件
在distributionUrl中将bin
更改为all
,即
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
到
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
update
gradle/wrapper/gradle-wrapper.properties
file
changebin
toall
in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
更新完成任务
wrapper {
gradleVersion = '5.4.1'
distributionType = Wrapper.DistributionType.ALL
}
- 在将项目导入IntelliJ-Idea IDE之前,更新
build.gradle
并将java
和idea
插件添加到插件列表中
- Before importing the project to IntelliJ-Idea IDE, update
build.gradle
and addjava
andidea
plugin to the plugins list
plugins {
id "java"
id "idea"
}
- 从终端执行
./gradlew clean build idea
或简单地./gradlew idea
- 将项目导入智能想法.
转到Preferences
->build,Execution,Deployment
->BuildTools
->Gradle
您可以看到 - 重新启动IntelliJ idea IDE.
- From terminal, execute
./gradlew clean build idea
or simply./gradlew idea
- Import project to intelliJ idea.
Go toPreferences
-->build,Execution,Deployment
-->BuildTools
-->Gradle
You can see - Restart IntelliJ idea IDE.
因此,在上文中,我们已经配置了这两个选项,因此请选择其中两个选项,指定位置选项除外.就是这样.
So above we have configured to both of the options so choose either of them, except specified location option. That's it.
此答案中提到的自动完成功能.
Autocomplete functionality as mentioned in this answer.
这篇关于如何使IntelliJ将Gradle源与build.gradle相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!