Java 使用 Gradle 构建在 IntelliJ IDEA 中获取 Gradle 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27694442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:52:18  来源:igfitidea点击:

Getting Gradle dependencies in IntelliJ IDEA using Gradle build

javaintellij-ideagradlebuild.gradle

提问by Andrew

Grade build, even from inside IntelliJ IDEA does not put the dependencies into the "External Libraries" folder, so these classes don't show up as suggestions in the editor and when I manually add them as an import there is a compile error.

分级构建,即使从 IntelliJ IDEA 内部也不会将依赖项放入“外部库”文件夹中,因此这些类不会在编辑器中显示为建议,并且当我手动将它们添加为导入时,会出现编译错误。

How can I get IntelliJ to automatically incorporate the dependencies in my build.gradlefile, for instance:

如何让 IntelliJ 自动将依赖项合并到我的build.gradle文件中,例如:

compile 'com.google.code.gson:gson:1.7.2

compile 'com.google.code.gson:gson:1.7.2

采纳答案by tddmonkey

You either need to import the project as a Gradle project from within Idea. When you add a dependency you need to open the Gradle window and perform a refresh.

您需要将项目作为 Gradle 项目从 Idea 中导入。添加依赖项时,您需要打开 Gradle 窗口并执行刷新。

Alternatively generate the project files from gradle with this:

或者使用以下命令从 gradle 生成项目文件:

build.gradle:

构建.gradle:

apply plugin: 'idea'

And then run:

然后运行:

$ gradle idea

If you modify the dependencies you will need to rerun the above again.

如果您修改了依赖项,您将需要再次重新运行上述内容。

回答by Andrey

After adding dependencies open "Gradle" ('View'->Tool Windows->Gradle) tab and hit "refresh"

添加依赖项后,打开“Gradle”(“视图”->“工具窗口”->“Gradle”)选项卡并点击“刷新”

example of adding (compile 'io.reactivex:rxjava:1.1.0'):

添加示例(编译 'io.reactivex:rxjava:1.1.0'):

hit refresh

点击刷新

If Idea still can not resolve dependency, hence it is possibly the dependency is not in mavenCentral() repository and you need add repository where this dependency located into repositories{}

如果 Idea 仍然无法解析依赖项,则可能依赖项不在 mavenCentral() 存储库中,您需要将此依赖项所在的存储库添加到存储库中{}

回答by Qix - MONICA WAS MISTREATED

When importing an existing Gradle project (one with a build.gradle) into IntelliJ IDEA, when presented with the following screen, select Import from external model-> Gradle.

将现有 Gradle 项目(带有build.gradle)导入 IntelliJ IDEA 时,出现以下屏幕时,选择从外部模型导入-> Gradle

Import project from external model

从外部模型导入项目

Optionally, select Auto Importon the next screen to automatically import new dependencies.

或者,在下一个屏幕上选择自动导入以自动导入新的依赖项。

回答by Beezer

Andrey's above post is stillvalid for the latest version of Intellij as of 3rd Quarter of 2017. So use it. 'Cause, build project, and external command line gradle build, does NOT add it to the external dependencies in Intellij...crazy as that sounds it is true. Only difference now is that the UI looks different to the above, but still the same icon for updating is used. I am only putting an answer here, cause I cannot paste a snapshot of the new UI...I dont want any up votes per se. Andrey still gave the correct answer above: enter image description here

截至 2017 年第 3 季度,Andrey 的上述帖子对 Intellij 的最新版本仍然有效。所以请使用它。因为,构建项目和外部命令行 gradle 构建,不会将它添加到 Intellij 中的外部依赖项中......听起来很疯狂,这听起来是真的。现在唯一的区别是 UI 看起来与上面不同,但仍然使用相同的更新图标。我只是在这里给出答案,因为我无法粘贴新 UI 的快照......我本身不想要任何投票。安德烈还是给出了上面的正确答案: 在此处输入图片说明

回答by Jingxuan Zhou

For those who are getting the "Unable to resolve dependencies" error:
Toggle "Offline Mode" off
('View'->Tool Windows->Gradle)

对于那些收到“无法解决依赖项”错误的人:
关闭“离线模式”
(“视图”->工具窗口->Gradle)

gradle window

渐变窗口

回答by Saurabh

In my case,I was getting error while refreshing gradle ('View'->Tool Windows->Gradle) tab and hit "refresh" and getting this error no such property gradleversion for class jetgradleplugin.

就我而言,我在刷新 gradle ('View'->Tool Windows->Gradle) 选项卡并点击“刷新”时遇到错误,并收到此错误没有此类属性 gradleversion 类 jetgradleplugin。

Had to install latest intellij compatible with gradle 5+

必须安装与 gradle 5+ 兼容的最新 Intellij