简体   繁体   English

Eclipse 无法识别 Gradle 依赖项

[英]Eclipse not recognizing Gradle dependencies

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.我是 Gradle 的新手,我正在尝试本教程https://spring.io/guides/gs/rest-service/我能够使用所需的依赖项编译 jar 并运行它。 However, I find it annoying that the libraries are not recognized by the IDE.但是,我发现 IDE 无法识别这些库很烦人。

在此处输入图片说明

Is there anyway to do it?有没有办法做到这一点?

You should use the gradle eclipse plugin.您应该使用 gradle eclipse 插件。 Add this to your build.gradle file:将此添加到您的build.gradle文件中:

apply plugin: "eclipse"

This will add eclipse related tasks to your build.这会将与 Eclipse 相关的任务添加到您的构建中。 By executing通过执行

gradlew cleanEclipse eclipse

Gradle will regenerate all eclipse project and classpath files based on the current dependencies of your project(s). Gradle 将根据项目的当前依赖项重新生成所有 Eclipse 项目和类路径文件。 You will however need to refresh your IDE to make the changes visible.但是,您需要刷新 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.由于 eclipse 并不真正了解 gradle 依赖项 - 它仅通过生成的类路径文件知道它们 - 只有在重新生成这些文件后,新的依赖项才会对 eclipse 可见。 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 构建不可见,并且在重新生成类路径文件后将被删除。

What worked for me is :对我有用的是:

right-click the build.gradle file -> Gradle -> Refresh Dependencies.右键单击 build.gradle 文件 -> Gradle -> 刷新依赖项。

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 正在下载的所有依赖项。

In eclipse在日食
Window -> preference -> Gradle窗口 -> 首选项 -> Gradle
in dependency management section在依赖管理部分
check on -> remap jar to Gradle projects.检查 -> 将 jar 重新映射到 Gradle 项目。
Hope this helps希望这有帮助

Enable auto build gradle using below settings to avoid this issue使用以下设置启用自动构建 gradle 以避免此问题

Go to Eclipse/STS in Windows -> Preference -> Gradle, Click Gradle Workspace Settings, there Enable Auto project synchronization转到 Windows 中的 Eclipse/STS -> Preference -> Gradle,单击 Gradle Workspace Settings,在那里启用自动项目同步

It automatically refreshes the Gradle when saving the changes in build.gradle file.build.gradle文件中保存更改时,它会自动刷新 Gradle。

在此处输入图片说明

Finally, click Project -> Clean-> choose your project and click Clean button最后,点击Project -> Clean-> choose your project并点击Clean按钮

If you are on mac do what dpr mentioned.如果您使用的是 mac,请执行 dpr 提到的操作。 That is add the following in build.gradle:即在 build.gradle 中添加以下内容:

apply plugin: "eclipse" 

Then run the following:然后运行以下命令:

gradle cleanEclipse eclipse 

This is kind of a bug from the IDE's.这是 IDE 的一个错误。 I encounter this Gradle dependencies problem in both the Eclipse and Spring tool suite.我在 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. 1)从命令提示符执行 gradle clean build --refresh-dependencies 。 Then create a new workspace and IMPORT the project into this workspace.然后创建一个新的工作空间并将项目导入到这个工作空间中。

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复制到一个新文件夹中并按照上述方法进行。

Add the two classpath variables GRADLE_HOME and GRADLE_REPO in eclipse in the below location:在 eclipse 中的以下位置添加两个类路径变量 GRADLE_HOME 和 GRADLE_REPO:

Windows -> Preferences -> Java -> Build Path -> Classpath Variables

GRADLE_HOME to be set to the Gradle Installation directory GRADLE_HOME设置为 Gradle 安装目录

GRADLE_REPO to be set to the cache folder inside .gradle (EXAMPLE : C:\\users\\user1\\.gradle\\caches :This location jars will be saved from repo. GRADLE_REPO将被设置为GRADLE_REPO中的缓存文件夹(例如:C:\\users\\user1\\.gradle\\caches:这个位置 jars 将从 repo 中保存。

Once the variables are set make sure build.gradle has the proper plugin defined depending on the eclipse version you are using:一旦设置了变量,请确保 build.gradle 根据您使用的 eclipse 版本定义了正确的插件:

apply plugin 'eclipse'
apply plugin 'eclipse-wtp'

Use the below configuration in build.gradle to resolve deployment issue:在 build.gradle 中使用以下配置来解决部署问题:

eclipse {
    wtp {
        component {
            libConfigurations += [configurations.runtimeClasspath]
        }
    }
}

Then follow the below steps:然后按照以下步骤操作:

Project -> clean
Right click on the Project ->Gradle ->Refresh Gradle Project

If the above steps doesn't resolve the issue it is advise to check if the build process is able to download the jars from artifactory/nexus as due to authentication error or wrong url of artifactory/nexus jar download fails.如果上述步骤没有解决问题,建议检查构建过程是否能够从 artifactory/nexus 下载 jars,因为身份验证错误或 artifactory/nexus jar 下载的错误 url 失败。

Following worked for me:以下为我工作:

eclipse -> Window -> Preferences -> Gradle -> "Select Local Installation Directory" eclipse -> Window -> Preferences -> Gradle -> “选择本地安装目录”

Click on Browse button and provide path for Gradle directory单击浏览按钮并提供 Gradle 目录的路径

Cheers !干杯!

I have had this problem importing Gradle projects from TFS.我在从 TFS 导入 Gradle 项目时遇到了这个问题。 Unless the initial import uses the Gradle importer the build.gradle will not be setup.除非初始导入使用 Gradle 导入器,否则不会设置 build.gradle。 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.您必须从 Eclipse 工作区中删除该项目(不要在磁盘上删除),然后将该项目作为 Gradle 项目再次导入。 This updates the build path according to build.gradle这会根据build.gradle更新构建路径

To solve this issue : 1)right-click the build.gradle file -> Gradle -> Refresh Dependencies.要解决此问题: 1) 右键单击​​ build.gradle 文件 -> Gradle -> 刷新依赖项。

在此处输入图片说明

2)if this didnt help (happened to me ) Project -> Clean-> choose your project and press "Clean" 2)如果这没有帮助(发生在我身上)项目 - >清理 - >选择你的项目并按“清理” 在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM