89

I have a simple gradle project in my eclipse. I update the build.gradle to include a few apache http jars...

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    compile 'org.apache.httpcomponents:httpclient:4.2.3'
    compile "org.apache.httpcomponents:httpmime:4.2.3"
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

However in my code, where I reference some Apache HTTP stuff I get:

The import org.apache.http cannot be resolved   

Any tips on what I need to do to make Eclipse see the new dependencies in my build.gradle?

I tried doing a clean but that does not work. My gradle plugin is:

  Gradle IDE    3.3.0.201307040643-RELEASE  org.springsource.ide.eclipse.gradle.feature.feature.group   GoPivotal, Inc.

6 Answers 6

197

You have to select "Refresh Dependencies" in the "Gradle" context menu that appears when you right-click the project in the Package Explorer.

8
  • 3
    Since this moment I started loving Gradle!
    – Namek
    Commented Jul 17, 2015 at 21:37
  • 14
    In the project context menu, I had to click Add Gradle Nature under Configure > before the Gradle option appeared.
    – shanesolo
    Commented Sep 18, 2016 at 22:11
  • 2
    for me gradle only gives the option "refresh gradle project". But this may also be due to the error in eclipse "Invalid Gradle project configuration file: .settings/org.eclipse.buildship.core.prefs"
    – xeruf
    Commented Aug 19, 2017 at 10:56
  • 3
    I only see Refresh Gradle Project as the only option under the gradle submenu when right clicking a project within eclipse Commented Aug 13, 2019 at 20:27
  • 2
    Do you know what does this do in the background? Is it actually executing gradle clean and then gradle eclipse? Or any other commands? Commented Apr 15, 2021 at 12:17
86

Follow those steps to update Gradle project in Eclipse:

  1. First, please check you have include eclipse gradle plugin. apply plugin : 'eclipse'
  2. Then go to your project directory in Terminal. Type gradle clean and then gradle eclipse.
  3. Then go to project in eclipse and refresh the project (Go to Project Explorer, select root of the project and hit F5 to refresh, or right click and select refresh in context menu).

enter image description here

3
  • 3
    I found this less effort than trying to get the Buildship Gradle plugin working in Eclipse. Commented Aug 3, 2016 at 20:50
  • Executing these allowed me to see that there was actually a problem. In my case, I saw Could not resolve: org.apache.commons:commons-lang3:3.4 Commented May 18, 2017 at 4:23
  • 1
    In my case... I removed an dependence, and after, included the same dependence. But eclipse doesn't download again. To solve, I also had to perform "gradle eclipse" on command line.
    – Akostha
    Commented Mar 16, 2018 at 19:56
29

Looking at the Eclipse plugin docs I found some useful tasks that rebuilt my classpath and updated the required dependencies.

  • First try gradle cleanEclipse to clean the Eclipse configuration completely. If this doesn;t work you may try more specific tasks:
    • gradle cleanEclipseProject to remove the .project file
    • gradle cleanEclipseClasspath to empty the project's classpath
  • Finally gradle eclipse to rebuild the Eclipse configuration
1
  • This works great. One note, be sure permissions are correct on OSX and Linux. I was running Eclipse as me and gradle as su. That caused Eclipse to not be able to access the config files.
    – Hodglem
    Commented Oct 11, 2019 at 16:07
7

You have to make sure that "Dependency Management" is enabled. To do so, right click on the project name, go to the "Gradle" sub-menu and click on "Enable Dependency Management". Once you do that, Gradle should load all the dependencies for you.

5
  • 8
    Eclipse Market place shows that I have gradle integration installed, but I don't see the context menu when I right click on the project! I do have apply plugin: 'eclipse'. any ideas? thanks.
    – endless
    Commented Feb 8, 2015 at 8:41
  • You Sir, have made my day, thank you. I am baffled on why this is not done automatically, however I did try to add everything manually, must have missed something else.
    – Toguard
    Commented Oct 25, 2015 at 5:30
  • If you don't see "Dependency Management" right click on your project, and go to Configure -> Convert to Gradle Project. This enabled dependency management for me. It's weird though because I imported it with the Gradle importer, you would think that it would already BE a Gradle project.
    – Ben L.
    Commented Jan 29, 2016 at 14:57
  • Does not work in Eclipse Photon. No context menu, and no Configure option either. Commented Feb 14, 2019 at 16:17
  • 2
    Still the same in eclipse 2020-06: No context menu "Gradle/Dependency Management", and no "Configure/Convert to Gradle project" (but a lot of other "convertTo...").
    – Heri
    Commented Sep 7, 2020 at 8:21
3

I tried all above options but was still getting error, in my case issue was I have not setup gradle installation directory in eclipse, following worked:

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

Click on Browse button and provide path.

Even though question is answered, thought to share in case somebody else is facing similar issue.

Cheers !

1

In my case, I solved weird issues in Spring Tools Suite (essentially Eclipse) after I tried to upgrade Spring Boot and the changes didn't seem to be taking effect.

Right click on your project -> Gradle -> Refresh Gradle Project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.