如何使IntelliJ将Gradle源与build.gradle相关联? [英] How to get IntelliJ to associate Gradle sources with build.gradle?

查看:124
本文介绍了如何使IntelliJ将Gradle源与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.

例如,诸如applydependencies 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:

  1. 正如已经发布的答案中所述,配置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
    change bin to all 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
}

  1. 在将项目导入IntelliJ-Idea IDE之前,更新build.gradle并将javaidea插件添加到插件列表中
  1. Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list

plugins {
    id "java"
    id "idea"
}

  1. 从终端执行./gradlew clean build idea或简单地./gradlew idea
  2. 将项目导入智能想法.
    转到Preferences-> build,Execution,Deployment-> BuildTools-> Gradle
    您可以看到
  3. 重新启动IntelliJ idea IDE.
  1. From terminal, execute ./gradlew clean build idea or simply ./gradlew idea
  2. Import project to intelliJ idea.
    Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
    You can see
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆