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.
Breako Breako
  • 6,353
  • 14
  • 40
  • 59

6 Answers6

195

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

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 3
    Since this moment I started loving Gradle! – Namek Jul 17 '15 at 21:37
  • Great little sub-menu: the "Gradle (STS)" plugin should probably have made it into an Eclipse main menu (if this is possible)... Ctrl-F5 refreshes all... – mike rodent Sep 03 '16 at 08:08
  • 14
    In the project context menu, I had to click **Add Gradle Nature** under **Configure >** before the **Gradle** option appeared. – shanesolo Sep 18 '16 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 Aug 19 '17 at 10:56
  • This solution has a bug. Every time I perform the refresh, the JRE is also [unnecessarily] changed to another one I don't want. – The Impaler Feb 14 '19 at 16:16
  • 3
    I only see Refresh Gradle Project as the only option under the gradle submenu when right clicking a project within eclipse – user1821961 Aug 13 '19 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? – RenatoIvancic Apr 15 '21 at 12:17
  • For me it was same situation as in the question, i simply was executing gradle clean and gradle build in STS run configuration, but after giving the eclipse task (gradle eclipse), the unresolved errors are gone – rinilnath Jul 27 '21 at 05:46
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

RenatoIvancic
  • 1,798
  • 3
  • 21
  • 36
Kasun Kariyawasam
  • 2,306
  • 3
  • 21
  • 35
  • 3
    I found this less effort than trying to get the Buildship Gradle plugin working in Eclipse. – Holly Cummins Aug 03 '16 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` – John Fisher May 18 '17 at 04: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 Mar 16 '18 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
Agustín
  • 1,569
  • 1
  • 14
  • 9
  • 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 Oct 11 '19 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.

yoram givon
  • 612
  • 7
  • 7
  • 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 Feb 08 '15 at 08: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 Oct 25 '15 at 05: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. Jan 29 '16 at 14:57
  • Does not work in Eclipse Photon. No context menu, and no Configure option either. – The Impaler Feb 14 '19 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 Sep 07 '20 at 08:21
2

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 !

Community
  • 1
  • 1
Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
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

EM-Creations
  • 4,195
  • 4
  • 40
  • 56