The import org.springframework.data cannot be resolved in gradle

CandleCoder picture CandleCoder · Sep 3, 2015 · Viewed 16.5k times · Source

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.

Answer

Bob Kuhar picture Bob Kuhar · Sep 3, 2015

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.