43

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.

enter image description here

Is there anyway to do it?

blacktide
  • 10,654
  • 8
  • 33
  • 53
cvguy
  • 433
  • 1
  • 5
  • 6
  • 3
    I'm guessing that your IDE is Eclipse. Install the Buildship eclipse plugin, and follow its instructions to load a gradle project into Eclipse. http://marketplace.eclipse.org/content/buildship-gradle-integration – JB Nizet Jul 03 '16 at 15:32
  • My IDE is the Spring Tool Suite and as far as I know it is not necessary to download it. – cvguy Jul 03 '16 at 15:35
  • 2
    Then just follow its instructions. You must have something like File - import - gradle project or something like that. – JB Nizet Jul 03 '16 at 15:37
  • 7
    Is your Eclipse project a Gradle project or just a plain Java project? If Gradle, right-click project and select "Gradle (STS)" > "Refresh All". If not Gradle, right-click project and select "Configure" > "Convert to Gradle (STS) Project". – Andreas Jul 03 '16 at 15:40
  • It is solved, as JB Nizet suggested Buildship is necessary and the project needed to be transformed to Gradle as Andreas said. Thank you all – cvguy Jul 03 '16 at 16:10

12 Answers12

82

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

apply plugin: "eclipse"

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

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.

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.

dpr
  • 10,591
  • 3
  • 41
  • 71
  • 3
    The script should actually be `gradle cleanEclipse eclipse` (no `w` should be in there), tried to edit, but since it wasn't 6 characters I was blocked – NateH06 Mar 19 '19 at 23:34
  • @NateH06 `gradlew` is correct. This is the gradle wrapper (can be created with `gradle wrapper`). By using the wrapper you are not tied to a globally installed gradle version but can specify the gradle version with your source code. – dpr Mar 20 '19 at 06:33
  • Ah, got it. I didnt have that set up, so it didn't work. – NateH06 Mar 21 '19 at 12:58
  • `gradlew cleanEclipse eclipse` this works awesome. Thanks for this info. Note: if `gradlew` is not work for you, we can try `gradle`. – SRIHARIRAO M Dec 16 '19 at 11:46
  • what about enviroment. should I keep it locally or can I push it to git. What I meant to say, if from a machine where eclipse is not there & I run this command or 'gradle clean build -x Test', will it throw error? – Satish Patro May 04 '20 at 13:42
  • 1
    @PSatishPatro It’s save to push this to SCM. First the eclipse tasks won’t be executed, if you simply build your repo (at least if your build is configured properly), second there is no dependency on any eclipse binaries or resources in these tasks. – dpr May 04 '20 at 13:44
  • Follow this approach: https://stackoverflow.com/questions/38171471/eclipse-not-recognizing-gradle-dependencies/63228389#63228389 – Sathiamoorthy Aug 28 '20 at 04:33
29

What worked for me is :

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

nanosoft
  • 2,913
  • 4
  • 41
  • 61
  • 6
    Great. I would prefer this over the [Eclipse Plugin](https://docs.gradle.org/4.1/userguide/eclipse_plugin.html); the latter one may confuse my colleagues who use Gradle without Eclipse. It should be the IDE's job to understand the build script. It feels really weird to mention Eclipse in build script which do not actually rely on Eclipse at all. – Franklin Yu Aug 10 '17 at 18:46
  • wow. Eclipse confuses me sooo much. Thanks though, this was anything but clear to me. – Matthew Turner Sep 13 '17 at 07:36
  • @mtpain - I spent half a day on this issue as well. I don't understand why eclipse can't just refresh the dependencies automatically once the build.gradle file is changed. – dcp Sep 20 '17 at 15:55
  • Refresh Gradle Project was the only option available to me under Gradle, and this worked. – java-addict301 Feb 04 '19 at 13:30
  • Thank you. The last thing I wanted to do was change the project configuration to work on my system when it works on everyone else's system just fine. – froggythefrog Jul 19 '19 at 20:04
  • Follow this approach: https://stackoverflow.com/questions/38171471/eclipse-not-recognizing-gradle-dependencies/63228389#63228389 – Sathiamoorthy Aug 28 '20 at 04:33
18

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

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.

specialk1st
  • 1,717
  • 21
  • 20
7

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

apply plugin: "eclipse" 

Then run the following:

gradle cleanEclipse eclipse 
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
7

Enable auto build gradle using below settings to avoid this issue

Go to Eclipse/STS in Windows -> Preference -> Gradle, Click Gradle Workspace Settings, there Enable Auto project synchronization

It automatically refreshes the Gradle when saving the changes in build.gradle file.

enter image description here

Finally, click Project -> Clean-> choose your project and click Clean button

Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
4

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

Rishikesh Fanse
  • 151
  • 1
  • 11
2

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

My sincere suggestion is that first try this, right-click the build.gradle file -> 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.

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.

Dheeraj
  • 332
  • 3
  • 4
1

Add the two classpath variables GRADLE_HOME and GRADLE_REPO in eclipse in the below location:

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

GRADLE_HOME to be set to the Gradle Installation directory

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.

Once the variables are set make sure build.gradle has the proper plugin defined depending on the eclipse version you are using:

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

Use the below configuration in build.gradle to resolve deployment issue:

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.

soumitra chatterjee
  • 2,268
  • 9
  • 26
  • 48
1

To solve this issue : 1)right-click the build.gradle file -> Gradle -> Refresh Dependencies.

enter image description here

2)if this didnt help (happened to me ) Project -> Clean-> choose your project and press "Clean" enter image description here

enter image description here

Vladi
  • 1,662
  • 19
  • 30
0

Following worked for me:

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

Click on Browse button and provide path for Gradle directory

Cheers !

Community
  • 1
  • 1
Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
0

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

rupweb
  • 3,052
  • 1
  • 30
  • 57
0

For me this happened recently when I updated my whole OS and lost my configuration plugins from Eclipse.

It turns out, my projects (libraries sometimes) use Lombok, so the annotation processor wasn't wiring everything as I was expecting it. I did what this guide suggested and it worked (In addition to quitting and opening Eclipse, as the prompted restart doesn't quite work)