How can I set in build.gradle the groovy nature of an Eclipse project? - eclipse

After running gradle cleanEclipse Eclipse on the project it loses the Groovy nature. How can I set this nature automatically, or simply to say to the Gradle to leave it alone.
Edit:
According do doc, I can write in the build.gradle:
eclipse {
project {
natures 'some.extra.eclipse.nature', 'some.another.interesting.nature'
}
}
But what is the name of the groovy nature, or how could I get it?
I go to the .project and look:
<natures>
<nature>org.eclipse.jdt.groovy.core.groovyNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>bndtools.core.bndnature</nature>
</natures>
org.eclipse.jdt.groovy.core.groovyNature - that is the nature name
And I am adding apply plugin: "groovy", as #Peter Niederwieser advised (thanks+1)
But
After running gradle cleanEclipse Eclipse I have correct .project file, and the project has "G" on the icon, but in the .classpath two lines are not present:
<classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
That ARE present, when I am setting the Groovy nature by hand.
And it seems, that these lines are important, for in that variant the project shows errors even on compile level - it doesn't see some Groovy methods.

Thinking about this again, Gradle will add a Groovy nature for those projects that have the groovy (or groovy-base) plugin applied. So either add that plugin on the Gradle side, or don't run cleanEclipse after you have added the Groovy nature manually, or switch to the Eclipse Gradle tooling instead of generating Eclipse files.

It seems, that for a real groovy nature setting, I need not only to set
natures.add 'org.eclipse.jdt.groovy.core.groovyNature'
and
apply plugin: "groovy"
, but also to edit the classpath.
eclipse {
classpath {
file {
withXml {
Node node = it.asNode()
node.appendNode('classpathentry',[exported:"true",kind:"con",path:"GROOVY_SUPPORT"])
node.appendNode('classpathentry',[exported:"true",kind:"con",path:"GROOVY_DSL_SUPPORT"])
}
}
}
}
What is interesting, if we turn on the groovy classpath by hand, only the path "GROOVY_SUPPORT" appears in the .classpath file. It is also enough for my project. But when turning by hand the whole Groovy nature, both paths appear. So, I am better including them both, too.

The Gradle-Eclipse plugin (that is gradle tooling for Eclipse, not the Eclipse plugin for gradle). Should be installable on Eclipse 3.6. (I say should be installable because we no longer test on 3.6, but there shouldn't be any reason why it is broken). If you install this plugin, you should be able to import your gradle project into Eclipse without calling gradle Eclipse.
You can install from the update site here:
http://dist.springsource.com/release/TOOLS/gradle
And more information on the project is here:
https://github.com/SpringSource/eclipse-integration-gradle

Related

Buildship keeps overwriting .classpath

I'm using Gradle and Eclipse with the Buildship plugin.
Buildship creates the .classpath file for Eclipse to use. I need one classpath entry (com.gwtplugins.gwt.eclipse.core.GWT_CONTAINER) to appear after the org.eclipse.buildship.core.gradleclasspathcontainer entry, for class-loading reasons.
So the relevant part of my .classpath file should look like this (having the GWT_CONTAINER on the bottom).
<classpath>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer" />
<classpathentry kind="con" path="com.gwtplugins.gwt.eclipse.core.GWT_CONTAINER"/>
</classpath>
Buildship always has the gradleclasspathcontainer on the last position. So I tried to change the sorting like this in my build.gradle (excerpt):
eclipse {
classpath {
file {
beforeMerged { classpath ->
def gwtClasspath = classpath.entries.find { entry -> entry.path == 'com.gwtplugins.gwt.eclipse.core.GWT_CONTAINER' }
classpath.entries.remove gwtClasspath
classpath.entries << gwtClasspath
}
}
}
When using ./gradlew eclipseClasspath, the .classpath file is created correctly. But as soon as Buildship runs, the file is again overwritten with the wrong ordering.
I also tried using whenMerged instead of beforeMerged, but that doesn't change anything.
Here's the output of Gradle when started by Buildship (e.g. by clicking on Gradle -> Refresh on the Eclipse project's properties):
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5/userguide/command_line_interface.html#sec:command_line_warnings
CONFIGURE SUCCESSFUL in 0s
:cleanEclipseWtpComponent
:cleanEclipseWtpFacet
:cleanEclipseWtp
:eclipseWtpComponent
:eclipseWtpFacet
:eclipseWtp
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
It seems Buildship doesn't even execute the eclipseClasspath task, but does create the .classpath file by some other means.
How can I get Buildship to honor my wish to have the classpath sorted my way?
I found the solution on Gradle forums:
Buildship doesn't use the eclipseClasspath task, but reads the configuration and creates .classpath by its own means. The Gradle classpath is appended to the end of the classpath, if it's not yet defined. This happens after executing the whenMerged section. So the solution is to add the Gradle classpath manually:
eclipse {
classpath {
containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
}
}
Perhaps the withXml hook would work differently
eclipse.classpath.file {
withXml { provider ->
def entry = provider.asNode().classpath.classpathentry.find { it.path == 'com.gwtplugins.gwt.eclipse.core.GWT_CONTAINER' }
println "Found $entry"
def parent = entry.parent()
parent.remove(entry)
parent.append(entry)
}
}

How to make Gradle add Lombok to its Project and external dependencies libraries?

I created a Java project in Eclipse Neon. I used Buildship 1.0.21 to import the project as a Gradle project and ran the wrapper and init commands to generate the build, settings and wrapper files.
The project has an empty source folder because I am trying to solve a similar problem on a more complicated project and taking the divide and conquer approach - just add Lombok dependency.
Here is my build.gradle as instructed on the lombok website:
apply plugin: 'java'
dependencies {
compileOnly "org.projectlombok:lombok:1.16.12"
}
and gradle-wrapper.properties if needed:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-bin.zip /* <--- tried other versions too */
and I ran the jar file to install it on my eclipse. If I add the jar manually via "Java Build Path" it works but I want Gradle to handle this for me. When I refresh the project nothing happens - there is no lombok jar under "Projects and External Dependencies" and code relying on lombok will give errors.
There is another project I have which does have it there and I don't know what I did differently but I know it's possible.
I managed to solve this after trial and error. The build.gradle file must add the jcenter repository:
repositories {
jcenter()
}
Why this is the case and why this is not mentioned anywhere I don't know. But for now this is what works.

View Gradle dependency tree in Eclipse

Is it possible to visualize the dependency tree from inside of Eclipse (e.g. the output of gradle dependencies)? Expanding the Gradle Dependencies tree in Eclipse is a flat view and does not show dependencies for other projects (e.g. if I have a dependency compile project(':project2'), none of project2's dependencies are shown).
Based on this it looks like a tree view is not supported?
Basically I'm looking for the equivalent of this in the maven plugin:
At the time of writing, neither Spring Eclipse Integration Gradle nor Buildship provide a Dependency Hierarchy view we know from m2e.
I don't know when this has been implemented but you can do a gradle dependencies either on command-line or via Buildship Gradle Tasks view within Eclipse. This prints a nice dependency tree of your project's dependencies to the console.
Use gradle 'project-report' plugin to generate report in HTML format.
https://docs.gradle.org/current/userguide/project_report_plugin.html
Add plugin in your build.gradle file:
apply plugin: 'project-report'
And generate report using:
> gradle htmlDependencyReport
For me, it was simply one command
in build.gradle add plugin
apply plugin: 'project-report'
and then go to cmd and run following command
./gradlew htmlDependencyReport
This give me a HTML report WOW Html report 💕
Or if you want the report in a txt file, to make search easy use following command
gradlew dependencyReport
That's all my lord.

Gradle dependency sources not appearing in Eclipse for the War Plugin

I am working on a Java web project that uses Gradle (version 2.1) as the build dependency tool. I use Eclipse Luna as my IDE. My OS is Mac 10.9 (Mavericks).
This is my build.gradle file (very basic and stripped down for ease of illustration):
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '3.0'
}
So just one dependency, Guice. I will generate my Eclipse classpath and project files using gradle cleanEclipse eclipse. Then I will import an existing project into my workspace. I like being able to view the source code of my dependencies in my Eclipse projects, so I will open a Guice class, such as com.google.inject.Injector, using CMD-SHIFT-T. And voila, the source code of that file will appear.
But I working on a web project, so I need to be able to build a WAR file instead of a JAR file. Therefore, I am going to apply the Gradle War Plugin by replacing apply plugin: 'java' with apply plugin: 'war'. Then I rerun gradle cleanEclipse eclipse and reopen my project.
Now, instead of seeing the source code when I open up Injector, I will see the bytecode viewer with the Attach Source button. Anyone have any ideas whether it's something I'm forgetting in my build.gradle file or if it could be a bug in Gradle/Eclipse?
Note that I am not using the Gradle Integration for Eclipse Plugin because I wish to pinpoint the root cause of this issue without adding an extra layer of complexity to it. I have also checked other SOF questions about Attach Source issue with Gradle and Eclispe, such as how to tell gradle to download all the source jars and Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?, but to no avail.

debug sbt sources too with eclipse

I'd like to navigate to the sbt sources when I debug my sbt build.
By using the sbt eclipse plugin I can debug e.g. my Build.scala file, which is great! And I can include the sbt plugin sources too by using:
> reload plugins
> eclipse withSources=true
But the sources to sbt itself aren't in the .classpath file generated by sbt-eclipse. I see that the source jars are in the ivy cache, but the sourcepath entry is missing:
<classpathentry kind="lib"
path="/home/lee/.ivy2/cache/org.scala-sbt/main-settings/jars/main-settings-0.13.1-RC2.jar"
/>
I'd like the generated class path to have a sourcepath like the following. (This was edited manually, I'd like to have it generated by sbt-eclipse):
<classpathentry kind="lib"
path="/home/lee/.ivy2/cache/org.scala-sbt/main-settings/jars/main-settings-0.13.1-RC2.jar"
sourcepath="/home/lee/.ivy2/cache/org.scala-sbt/main-settings/srcs/main-settings-0.13.1-RC2-sources.jar"
/>
(Tested with sbt 13.1-RC2 and sbt eclipse 2.4.0)
I see this sbt-dependency-manager plugin, but that looks awkward...
Is there an easier way to view sbt sources along with my sbt build?
I am, as originator of sbt-dependency-manager, must admit that there is not only problem to get sources. You may download sources by hands and manually attach them to your debugger, but Eclipse still unable to use them.
sbt-dependency-manager not only download, but also repackage jars. So in this way you should open 2 bugs :-):
about fetch sources in sbt-eclipse
about sources layout of SBT itself
I tried to avoid this issues and this is only solution that I know. Oh, you may switch to Idea or Ensime, of course - but this is not acceptable for me. Also I don't use sbt-eclipse at all because sbt-dependency-manager makes it useless.