Java Eclipse 无法识别 Gradle 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38171471/
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 20:03:34  来源:igfitidea点击:

Eclipse not recognizing Gradle dependencies

javaeclipsegradledependencies

提问by cvguy

I am new to Gradle and I was trying this tutorial https://spring.io/guides/gs/rest-service/I was able to compile the jar with the required dependencies and run it. However, I find it annoying that the libraries are not recognized by the IDE.

我是 Gradle 的新手,我正在尝试本教程https://spring.io/guides/gs/rest-service/我能够使用所需的依赖项编译 jar 并运行它。但是,我发现 IDE 无法识别这些库很烦人。

enter image description here

在此处输入图片说明

Is there anyway to do it?

有没有办法做到这一点?

采纳答案by dpr

You should use the gradle eclipse plugin. Add this to your build.gradlefile:

您应该使用 gradle eclipse 插件。将此添加到您的build.gradle文件中:

apply plugin: "eclipse"

This will add eclipse related tasks to your build. By executing

这会将与 Eclipse 相关的任务添加到您的构建中。通过执行

gradlew cleanEclipse eclipse

Gradle will regenerate all eclipse project and classpath files based on the current dependencies of your project(s). You will however need to refresh your IDE to make the changes visible.

Gradle 将根据项目的当前依赖项重新生成所有 Eclipse 项目和类路径文件。但是,您需要刷新 IDE 以使更改可见。

There is one more thing to consider. As eclipse is not really aware of the gradle dependencies - it knows them only by the generated classpath files - new dependencies will be visible to eclipse only after regenerating these files. Furthermore dependencies added in eclipse will not be visible to your gradle build and will be removed once the classpath files are regenerated.

还有一件事需要考虑。由于 eclipse 并不真正了解 gradle 依赖项 - 它仅通过生成的类路径文件知道它们 - 只有在重新生成这些文件后,新的依赖项才会对 eclipse 可见。此外,在 eclipse 中添加的依赖项对您的 gradle 构建不可见,并且在重新生成类路径文件后将被删除。

回答by specialk1st

After importing the project as a Java project, in Eclipse Neon you:

将项目作为 Java 项目导入后,在 Eclipse Neon 中:

Right-click on your project --> Configure --> Add Gradle Nature.

Your project will automatically refresh and you will see all the dependencies being downloaded by Gradle.

您的项目将自动刷新,您将看到 Gradle 正在下载的所有依赖项。

回答by nanosoft

What worked for me is :

对我有用的是:

right-click the build.gradle file -> Gradle -> Refresh Dependencies.

右键单击 build.gradle 文件 -> Gradle -> 刷新依赖项。

回答by Sachin Thapa

Following worked for me:

以下为我工作:

eclipse -> Window -> Preferences -> Gradle -> "Select Local Installation Directory"

Click on Browse button and provide path for Gradle directory

eclipse -> Window -> Preferences -> Gradle -> “选择本地安装目录”

单击浏览按钮并提供 Gradle 目录的路径

Cheers !

干杯!

回答by Rishikesh Fanse

In eclipse
Window -> preference -> Gradle
in dependency management section
check on -> remap jar to Gradle projects.
Hope this helps

在 Eclipse
窗口 -> 首选项 -> Gradle
在依赖管理部分
检查 -> 将 jar 重新映射到 Gradle 项目。
希望这可以帮助

回答by Pritam Banerjee

If you are on mac do what dpr mentioned. That is add the following in build.gradle:

如果您使用的是 mac,请执行 dpr 提到的操作。即在 build.gradle 中添加以下内容:

apply plugin: "eclipse" 

Then run the following:

然后运行以下命令:

gradle cleanEclipse eclipse 

回答by rupweb

I have had this problem importing Gradle projects from TFS. Unless the initial import uses the Gradle importer the build.gradle will not be setup. You have to remove the project from the Eclipse workspace (do not delete on disk) and then import the project again as a Gradle project. This updates the build path according to build.gradle

我在从 TFS 导入 Gradle 项目时遇到了这个问题。除非初始导入使用 Gradle 导入器,否则不会设置 build.gradle。您必须从 Eclipse 工作区中删除该项目(不要在磁盘上删除),然后将该项目作为 Gradle 项目再次导入。这会根据build.gradle更新构建路径

回答by Dheeraj

This is kind of a bug from the IDE's. I encounter this Gradle dependencies problem in both the Eclipse and Spring tool suite.

这是 IDE 的一个错误。我在 Eclipse 和 Spring 工具套件中都遇到了这个 Gradle 依赖问题。

My sincere suggestion is that first try this, right-click the build.gradle file -> Gradle -> Refresh Dependencies.

我真诚的建议是,首先尝试这个,右键单击 build.gradle 文件 -> Gradle -> Refresh Dependencies。

If this does not work, Best and working solution is
1) Do gradle clean build --refresh-dependencies from the command prompt. Then create a new workspace and IMPORT the project into this workspace.

如果这不起作用,最佳且有效的解决方案是
1) 从命令提示符执行 gradle clean build --refresh-dependencies。然后创建一个新的工作空间并将项目导入到这个工作空间中。

2) If the above thing failed, then in worst-case scenario one need to make a git clone of the project into a new folder and follow the above method.

2)如果上面的事情失败了,那么在最坏的情况下,需要将项目的git clone复制到一个新文件夹中并按照上述方法进行。