6

I refreshed my gradle project but it gives me error that " The import org.springframework.data cannot be resolved "

The following are some imports which it doesn't understands

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.ScriptOperations;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.script.ExecutableMongoScript;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

There are more imports which it is unable to resolve.

Please give me any suggestion to resolve this issue.

Thanks in advance.

CandleCoder
  • 1,387
  • 4
  • 21
  • 45
  • 1
    Then its would be not the Gradel issue . I dont know how Gradel work in eclipse but try to update the dependency in Maven we did this `Maven->Update Dependencies` – Subodh Joshi Sep 03 '15 at 04:29
  • Its a gradle project so we edited the dependency in build.gradle file :) @subodh – CandleCoder Nov 22 '15 at 01:44

3 Answers3

9

At the top of your build.gradle I recommend that you add the eclipse and intellij plugins.

apply plugin: 'eclipse'
apply plugin: 'idea'

These plugins function to generate the .project and .classpath files that the eclipse IDE uses (it does the same for IntelliJ, but I don't really know what those files are; *.iml maybe?).

Then from the command line you just do...

gradle eclipse

...and it figures out the dependencies, pulls the JARs over, and generates the .classpath and .project. If you have eclipse open while you are doing this, refresh the project and Voila. Every time you add a dependency in the build.gradle you do this workflow again. It works like a champ for me.

There is probably some Eclipse plugin to allow you to just do this whole thing from within the IDE. I've been doing it from the commandline for a while now because its just simple.

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • 1
    Sorry bob but I have already did this stuff..But it doesn't solved my problem. – CandleCoder Sep 03 '15 at 05:40
  • 1
    @Raj really? Hmmm. What does your build.gradle look like? Mine has compile 'org.springframework.data:spring-data-commons-core:1.4.1.RELEASE' and compile 'org.springframework.data:spring-data-mongodb:1.7.2.RELEASE' and the imports work fine. https://github.com/robertkuhar/StackOverflow/blob/master/src/main/java/org/rekdev/so/WTFSpringData.java – Bob Kuhar Sep 03 '15 at 06:00
  • 'org.springframework.data:spring-data-mongodb:1.7.2.RELEASE' This dependency solved my problem :) thanks – CandleCoder Sep 03 '15 at 06:02
  • What does 'refresh the project" entail? Thanks! – GreenAsJade Dec 30 '18 at 03:10
  • @GreenAsJade When I wrote this answer Eclipse had a F5 - Refresh project or something like that. I don't see it anymore (Eclipse 4.6.2) and I've become an IntelliJ user in any event. These days, Gradle integrates with the IDEs directly, so even the "add these plug-ins" isn't necessary. – Bob Kuhar Jan 02 '19 at 03:12
4

Added a dependency into build.gradle file as :

org.springframework.data:spring-data-mongodb:1.7.2.RELEASE

This solved my problem.

T3 H40
  • 2,326
  • 8
  • 32
  • 43
CandleCoder
  • 1,387
  • 4
  • 21
  • 45
1

I ran into a similar spring dependency problem, only in the Eclipse environment, and the answer wasn't a programming issue, it was just that Eclipse was failing to handle changes to the build.gradle file.

If you can build your project successfully from the command line, you should trying closing Eclipse and restarting it. This solved my problem.

Kyle Olson
  • 116
  • 6
  • Far out. This worked for me. But why?? Is that an Eclipse bug or ... what? – GreenAsJade Dec 30 '18 at 03:09
  • I can't say for sure. It's presumably some sort of caching issue. I find with nearly every IDE that a combination of rebuild all/close and reopen/clear the cache can occasionally fix a build error. – Kyle Olson Dec 31 '18 at 06:54