Gradle eclipse task doesn't add proper gradle nature
Asked Answered
D

6

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.

Disgusting answered 1/9, 2015 at 21:6 Comment(2)
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.Mirabelle
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?Alarum
T
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'
    }
}
Tenia answered 26/2, 2016 at 14:58 Comment(0)
S
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.

Scrimpy answered 19/10, 2015 at 15:30 Comment(5)
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.Scrimpy
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.Disgusting
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).Scrimpy
I agree with @Scrimpy , 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.Zymase
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.Disgusting
A
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'
        }
    }
}
Alarum answered 8/12, 2015 at 21:54 Comment(2)
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.Hipolitohipp
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.Alarum
D
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.
Dorolice answered 7/12, 2015 at 9:40 Comment(2)
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?Hipolitohipp
Either way, it's been configured. Looks like the problem is that the plugin is glitchy.Disgusting
R
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

Rhoads answered 9/12, 2015 at 12:44 Comment(2)
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)?Disgusting
I only run NIDVSADesktop (root project).Rhoads
M
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

Moa answered 1/11, 2021 at 6:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.