Running Gradle inside Eclipse? - eclipse

There appears to be an Eclipse plugin for Gradle, but no Gradle plugin for Eclipse...
Simply, I'd like to add a build.gradle to my Eclipse project, write its contents (including defining its dependencies), and then run it from inside Eclipse, the same way I can run Ant scripts from inside Eclipse.
When it runs, I'd expect the plugin to pull down all dependencies and make them available to my project's classpath in Eclipse.
If no such plugin exists, then I ask: what's the best way to develop in Eclipse, but keep your builds managed by Gradle? If I decide I need a new xyz.jar as a dependency for my code, how do I add it as a dependency in such a way that both Gradle and Eclipse will recognize it (and not throw compiler errors)?

Either use the IDE project generation approach (gradle eclipse), or use the Eclipse Gradle Integration. In both cases, you'll want to apply plugin: "eclipse".

The Gradle plugin for Eclipse is part of the Spring IDE. It understands the dependencies specified in the build script and makes those available in the .classpath.

Related

Use Gradle plugin without adding to build.gradle?

In Maven you can do mvn eclipse:eclipse to run the eclipse goal of the maven-eclipse-plugin. Is there a similar way to do this in Gradle? I'd like to run the plugin for my IDE of choice but I want my build script to remain "IDE-agnostic".
I don't want apply plugin 'eclipse' in my build.gradle file but want it to generate the Eclipse files it needs. This way users of IntelliJ won't need to swap it.
Rather than rely on the build tool to configure your IDE (who ever thought that was a good idea is beyond me), set up the IDE to understand and integrate with the build tool. Eclipse is specifically designed to share configuration in such a way that each person who checks out a project from source control gets the project configured correctly with a minimum of manual intervention. Gradle support is much newer and somewhat less mature than Ant or Maven, but it works. See the Buildship project, which allows you to point Eclipse at your existing Gradle-based project and import it as a properly configured Eclipse project, no need for hacks like mvn eclipse:eclipse (which is now deprecated/retired, by the way)

How to script maven commands in Eclipse

I have a maven project in Eclipse with a bunch of local project dependencies.
Building with the Maven assembly plugin or shade plugin is a pain because I first have to build and install all of the local project dependencies before I can build my main project.
Does m2e provide a way to script a sequence of arbitrary maven commands? I know I can script everything from the command line, I just don't know where m2e's instance of maven is installed, and I'd like to avoid installing a separate instance.
Have you considered creating a multi-module Maven project that "contains" the dependencies? That would allow you to build just the container and Maven automatically takes care of building the component modules. Eclipse's m2e supports multi-module builds so there wouldn't be anything special to do for Eclipse.

How do I make my Gradle project automatically built in Eclipse?

I am trying to use Gradle for my Java project. However, it does not automatically build like when I use Maven. Which plugins and settings should I use?
You could use the Gradle Eclipse plugin: http://gradle.org/docs/current/userguide/eclipse_plugin.html
If you are starting your project, maybe you could try STS: http://spring.io/tools/sts

Is it possible for Gradle projects in Eclipse/STS to resolve dependencies to other Gradle projects in the same workspace?

A Gradle project in my workspace (call it Downstream) needs to depend on another Gradle project (call it Upstream). Outside of Eclipse, of course Upstream would need to be build and installed before Downstream (so that Gradle can resolve it). In Eclipse, since both projects are in the workspace together, I'd like to have Gradle look in the workspace first and make the dependency between the projects, not from the repo.
m2e (Maven integration for Eclipse) does this (the option is called Resolve dependencies from Workspace). Gradle Eclipse plugin has an option called Remap Jars to maven projects but that seems to do this for pom-driven m2e projects, not other Gradle projects.
Is there any way to get Gradle to resolve dependencies to the local workspace (when they're present) instead of the repo?
It turns out this is was an outstanding feature request for Gradle IDE. It has been delivered for the 3.6.3 release of Gradle IDE.
You do have to enable it, though:
The feature has to be enabled in the Gradle preferences page. Access
via "Window >> Preferences >> Gradle".
If you are not seeing the "remap jars to Gradle projects" there then
maybe double check that version of the Gradle plugins is indeed 3.6.3.
Maybe something went wrong during the upgrade and you are still using
an older version.
Source
As far as I know these is no such functionality in current STS plugin. Given that there is the re-mapping support to replace dependencies with reference to m2e project I think it should be possible to add it.
Modifying the generated classpath using XML hooks in EclipseClasspath model descrideb in http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.eclipse.model.EclipseClasspath.html will probably not help because this is done in Gradle process where you do not have information about existing Eclipse projects from your current workspace.

How eclipse maven and ant work together?

I made simple maven project and I opened it with Eclipse. I have installed maven plugin for Eclipse. I'm interested in following:
How Eclipse compiles code when I hit save on my source code (does it use configuration from ant or maven or something else)?
When I run tests from JUnit plugin for Eclipse those Eclipse calls mvn test (I suppose not, but what is then happening exactly)?
Is it possible that maven does the build successfully but Eclipse is
showing errors in code?
The Maven Integration for Eclipse makes it easier to edit POM files, allows you to execute maven builds from within Eclipse and to help with dependency management. It doesn't actually compile your code (unless of course you execute a maven build from within Eclipse). The main help is with the dependency management and writing the .classpath file of your project within Eclipse.
To try and answer your questions:
Eclipse uses its standard mechanism to compile code. With a standard eclipse for java developers your project will have a Java Project nature and Eclipse will then use the Java Development Tools - JDT to compile the code. (Internally this uses an incremental builder to build the code http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2FresAdv_builders.htm). What source files it will compile and where it will place the resultant .class files is configured in your project's Java Build Path (which I am guessing the maven plugin may well configure for you)
JUnit support is part of the Java Development Tools as well.
It is possible that maven will successfully build a project outside of Eclipse, but that the same project will show errors within Eclipse. This is usually down to classpath errors (dependencies defined in the project's POM not being added to the classpath in Eclipse). If you are using the maven plugin with eclipse this probably shouldn't happen. If you are not using the maven plugin within eclipse you can execute maven eclipse:eclipse to have maven update the Eclipse .classpath file of the project which should then fix any of these problems.