5

We are having a frustrating problem with our Eclipse workspace. Here is a high-level example of what is happening:

ProjectA
-- src/main/java/...
-- build.gradle

ProjectB
-- src/main/java/...
-- build.gradle

After running gradle eclipse and importing the projects into Eclipse, we occasionally will get 'MyType cannot be resolved to a type' even though the following are true:

  • Our projects have the Gradle library container on their classpath
  • The projects have the correct workspace projects within their Gradle container
  • Things like CTRL + Click work from types that are showing up with red underline (i.e. compile error) meaning Eclipse knows exactly how to get to those types, but the compiler can't find them
  • Command-line Gradle builds work as expected

This is happening throughout the team and we cannot figure out why Eclipse keeps going out of sync and cannot compile from time to time. What usually fixes the problem is a combination of:

  • Project > Clean in Eclipse
  • Gradle cleanEclipse or gradle eclipse
  • Reimporting the projects
  • etc

Other notes:

  • Using Gradle version 2.2.1
  • Happens in both Eclipse Luna and Eclipse Mars
  • Using JDK 8

Has anyone else had these types of compile issues in Eclipse with Gradle projects?

thedude19
  • 2,643
  • 5
  • 34
  • 43
  • Also, show use your `build.gradles`. – Jared Burrows Jun 30 '15 at 14:25
  • 2
    @Jared Burrows, is that supposed to be a constructive comment? I'm not sure why I should use Android Studio when I am not developing for Android. Also, I don't use IntelliJ because the plugins I need are in Eclipse. – thedude19 Jun 30 '15 at 14:46
  • Can you get away with just refreshing the project (F5) in Eclipse? – Robert Jun 30 '15 at 15:08
  • @Robert, unfortunately no. Refreshing does not seem to change anything. – thedude19 Jun 30 '15 at 15:13
  • @thedude19 It is more than constructive. You are using the `Gradle` and the build structure for `Gradle`, why not use an `IDE` that supports it naturally? What plugins do you need in `Eclipse` that you do not have or cannot get in `Intellij`? – Jared Burrows Jun 30 '15 at 15:29
  • Do you have the Gradle preference enabled in Eclipse to resolve project dependencies to the local worksace (described at http://stackoverflow.com/questions/26282985/is-it-possible-for-gradle-projects-in-eclipse-sts-to-resolve-dependencies-to-oth)? – E-Riz Jul 06 '15 at 22:39
  • Guessing what can cause this issue is like finding a mare's nest. You should check the generated `.classpath` files by `gradle` and find whether they have been generated right or wrong. For example: camel lib is missing in eclipse dependencies for a project. So you should find `camel-jar` entry in `.classpath`. If they are missing you should be looking towards your build.gradle (check `gradle dependencies`), If they are there, compare them to others jar entries and try to understand how eclipse gets. – deathangel908 Jul 07 '15 at 17:52

1 Answers1

7

One source of such problems can be letting Gradle generate the Eclipse project metadata/setup files (what you get by running gradle eclipse). I know that the Maven and Gradle teams really want their tool (Maven or Gradle) to generate that stuff, but they notoriously do a less-than-ideal job of it. For example, the last time I used Gradle it configured the .classpath to use a hard-coded JRE library path instead of the preferred Execution Environment. That kind of poor job of generating files makes for developer headaches.

Instead, the Eclipse recommendation is to manually configure your projects (for the most part), only letting Gradle manage its Classpath Container, then checking in the Eclipse .project, .classpath, and .settings files/folders in to your SCM (svn, git, etc.). That way, the process of checking out a project into a workspace is automatic and does not require running gradle eclipse or mvn eclipse:eclipse all the time.

That's the way that Eclipse projects were originally designed and intended to be managed, and it works very well. I've worked with setups like that on very large projects (100+ separate projects in Eclipse and dozens of developers). It also cuts down on the number of steps to go from zero to running app.

This is one of those philosophical differences between Maven/Gradle and Eclipse; when it comes down to it, IMO, the IDE should manage it's stuff and the build/dependency tool should just stay out of the way.

Try manually getting things just right in your project configuration and then checking in those files - see if it doesn't relieve the headaches your team faces in this area.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Thanks for the awesome answer. I'm actually working on experimenting with this today. – thedude19 Jul 07 '15 at 17:38
  • Implemented the changes for my team. It is working perfectly. Thank you! – thedude19 Jul 08 '15 at 17:34
  • Running mvn eclipse:eclipse from the command line, then right clicking on the projects in Eclipse -> Refresh worked for me. Thumbs up – Bradley D May 14 '18 at 16:46
  • The eclipse:eclipse task is retired and obsoleted, for good reason. I would not use it under any circumstances. Plus it's not likely to work at all with newer versions of Eclipse since eclipse:eclipse hasn't been maintained in quite a while. – E-Riz May 14 '18 at 20:47