12

I have the following project structure:

  • root-gradle (build.gradle)
    • project-group1 (no build file)
      • project1 (build.gradle)
      • project2 (build.gradle)
      • ...
    • project-group2 (no build file)
      • ...

So happens that I have to recreate Eclipse projects often. I run command:

gradle cleanEclipse eclipse

After a number of runs "eclipse" task stops working as expected. Namely, it does not add gradle nature to the projects anymore and does not recognize

sourceCompatibility = 1.6

anymore attempting to build everything with 1.8 version of Java.

I added the following to the root build.gradle:

allprojects {
    sourceCompatibility = 1.6
    eclipse.project {
        natures 'org.springsource.ide.eclipse.gradle.core.nature'
    }
    // more stuff here ...
}

This helped with the root project, but had no effect on any other project. I added the same thing to subprojects with the same unsatisfactory result. I have to say than even after the nature had been added to the root project and Gradle plugin options became available for the root again I still don't see the "G" icon.

So it looks like I have 2 problems with the gradle setup.

  1. Disappearance of the gradle nature. After a few runs Eclipse stops recognizing gradle projects. I wouldn't even face the second problem if that worked properly.

  2. Some problem with my gradle build files or projects layout as my settings don't seem to take effect on subprojects.

  3. Missing "G" icon for the project with restored Gradle nature.

Cadoiz
  • 1,446
  • 21
  • 31
ATrubka
  • 3,982
  • 5
  • 33
  • 52
  • 1
    I only refresh from Eclipse, but customize using the gradle eclipse plugin. I have run into issues where `afterEvaluate` resolved races so you could try that to see if you need to delay the change until the end of the configuration phase. – Ben Manes Sep 01 '15 at 23:44
  • 1
    I'm with @BenManes on this one. It is possible that your allprojects block is being run before a list of subprojects is assembled, resulting in the behavior you're seeing. Have you tried using afterEvaluate? – RaGe Dec 08 '15 at 21:47

6 Answers6

9

Using the Buildship: Eclipse Plug-ins for Gradle I had to use natures string org.eclipse.buildship.core.gradleprojectnature

For Example:

allprojects {
    eclipse.project {
        natures 'org.eclipse.buildship.core.gradleprojectnature'
    }
}
Cadoiz
  • 1,446
  • 21
  • 31
Alex Q
  • 3,080
  • 2
  • 27
  • 29
4

Install Gradle plug-in for Eclipse

Then use Eclipse menu: File -> Import... to import the Gradle project. It will add the Gradle Nature. And 2 extra Eclipse views, one for quickly running Gradle tasks. One for Gradle output of those executed tasks.

TIP: Before importing an existing Gradle project, create a New Gradle project, to see if the Gradle plug-in is working as expected.

New Gradle Project

  • Use the Eclispe menu: File -> New -> Other...
  • Select the wizard: Gradle -> Gradle Project
  • Enter the project name
  • Press the button Finish

This should set up a minimal Gradle project.

Verhagen
  • 3,885
  • 26
  • 36
  • 1
    Then you should __not__ use the `gradle cleanEclipse eclipse` commands. As they generate, the same / similar files as the _Gradle for Eclipse_ plugin does. Also remove the `eclipse.project { ... }` part fomr the Gradle build file. As it is __not__ used by the _Gradle for Eclipse_ plugin. And only can confuse other developers of the project. – Verhagen Oct 20 '15 at 05:20
  • I understand that the cleanEclipse eclipse is the last resort. In some cases though there's no other command that helps with library version changes. Section eclipse.project is actually used, but it's not applied to subprojects. You can insert it in every gradle file, then the nature gets added as expected. – ATrubka Oct 20 '15 at 19:47
  • When you stick with the `gradle cleanEclipse eclipse`, it is not surprising that Eclipse gets out of sink, with new generated files, like `.project` and `.classapth` are created. If you need this way-of-working, I would advice, to close Eclipse or at least close within Eclipse all related projects. Then run in a command promt, the `gradle cleanEclipse eclipse`. And then open Eclipse again (or open the before closed projects). – Verhagen Oct 20 '15 at 20:40
  • I agree with @Verhagen , Gradle's eclipse tasks are not the best and really fight against Eclipse itself. Even before Buildship, using STS's Gradle IDE plugins, they did a better job of setting up the Eclipse project than Gradle itself. Makes sense, doesn't it: Gradle is a build and dependency management tool, better to leave IDE stuff to the IDE tools. – E-Riz Dec 03 '15 at 14:00
  • Unfortunately, unhealthy obsession with build and dependency management tools has forced people to use it for everything even if it's not fit for it. – ATrubka Dec 07 '15 at 20:28
3

If there is an evaluation phase race condition, you can eliminate it by using the afterEvaluate qualifier. I would try:

allprojects {
    afterEvaluate {
        eclipse.project {
            natures 'org.springsource.ide.eclipse.gradle.core.nature'
        }
    }
}
RaGe
  • 22,696
  • 11
  • 72
  • 104
  • While the nature is added, Eclipse still doesn't recognize the projects as Gradle projects. I end up having to disable and then reenable the Gradle nature, then enable dependency management for them to build. – nickb Dec 09 '15 at 14:19
  • Hmm, I guess eclipse does some recognition only during a project add, and there isn't (currently) a straightforward way to force eclipse to re-do it from the build tool. – RaGe Dec 10 '15 at 10:13
1

I think you must set gradle to your system environment. Steps:

  1. Install Gradle from gradle website.
  2. Go to eclipse preferences set Home path in gradle EnIDE like C:\gradle-2.3 or whatever directory you have installed.
  3. Also make sure to add path to your system environment variable.
Cadoiz
  • 1,446
  • 21
  • 31
user1973596
  • 41
  • 1
  • 5
  • This would force Gradle versions to be the same across projects, and restrict the ability to let the IDE use the defaults defined by the Gradle wrapper, no? – nickb Dec 07 '15 at 19:20
  • Either way, it's been configured. Looks like the problem is that the plugin is glitchy. – ATrubka Dec 07 '15 at 20:24
0

Screenshot of the root dir

in the settings.gradle >>

 include 'Device', 'Secugen', 'Morpho', 'NIDVSADesktop'

here NIDVSADesktop is my root project. in the root project gradle

dependencies {
  compile project(':Secugen') 
  compile project(':Morpho')

}

here Secugen and Morpho are my 2 sub projects

Bazer Con
  • 105
  • 4
Mahfuz Ahmed
  • 721
  • 9
  • 23
  • How does Morpho gradle build find its parent? From what I know it only searches in "master" sibling directory or parent directories. What happens when you run Morpho build only (not root build)? – ATrubka Dec 09 '15 at 22:05
  • I only run NIDVSADesktop (root project). – Mahfuz Ahmed Oct 03 '21 at 02:22
0

Remove Gradle Nature close ecllips then go to project folder structure and run the command "./gradlew clean build -x test cleanEclipse eclipse" next open eclipse and just refresh the project