gradle in eclipse – egradle vs buildship

TLDR: use BuildShip rather than EGradle.

While everyone on our team uses command line git (rather than egit), most actual coding in Java happens within Eclipse. We ran Ant in Eclipse for deploying robot code and Ant at the command line for other things. I’m not sure if we will use Eclipse to run Gradle, but writing up for the team just in case!

For more on what the Gradle files mean see the main Gradle SmartDashboard post

EGradle

The first thing I did was try out the EGradle plugin. It worked, but wasn’t as smooth as BuildShip. (See below for using BuildShip).

Install EGradle

  1. Help
  2. Eclipse Marketplace
  3. EGradle
  4. Accept license and install
  5. Restart Eclipse

Modify the project you cloned from github

  1. Clone SmartDashboard if you haven’t already
  2. Edit the build.gradle file to add the eclipse profile.  You just have to add the one line and save the file:
    project(':fakeRobot') {
      apply plugin: 'java'
      apply plugin: 'application'
      apply plugin: 'eclipse'

Import the project you cloned from github

  1. File
  2. Import
  3. EGradle > Import gradle root project with all subprojects
  4. Set Gradle root project to the repository you cloned from github

Build

  1. Right click the build.gradle file
  2. Run as > EGradle

EGradle limitations

  1. You had to edit the build.gradle for Eclipse to recognize it.
  2. EGradle doesn’t provide a way to see the build directory so you have to go to the file system. (and yes I tried changing the filters; it just didn’t work)

Trying that again with BuildShip

BuildShip is the official Eclipse.org plugin for Gradle. Wish I knew that before I started!

Install BuildShip

  1. Help
  2. Eclipse Marketplace
  3. BuildShip Gradle Integration
  4. Accept license and install
  5. Restart Eclipse

Import the project you cloned from github

  1. File
  2. Import
  3. Gradle >Existing Gradle project
  4. Set project root directory to the repository you cloned from github (Note that you don’t need to edit the build.gradle file like you had to with EGradle). It also uses the proper project name.

Build

There are multiple ways to do this. I’m showing just one way that maximizes understanding of what is going on.

  1. The Gradle Tasks view is open automatically for this project. (If not, click the project)
  2. Click SmartDashboard to expand
  3. Click build to expand
  4. Right click build (with a green icon to the left) and choose “Run Gradle Tasks”
  5. This automatically opens the Gradle Executions view which shows you what Gradle did in a nice tree. You can click on the Console view to see the actual command line output.
  6. Configure the filters so you can see the Gradle build folder
  7.  F5 to refresh – now you see the build folder.

 

creating a groovy project with gradle in eclipse

Last month, I went to a talk on gradle.  Today I decided to give it a shot.  My goal was to create a simple groovy project with gradle.  I did it in less than 30 minutes so getting started was fast.

Setup

I already had the Groovy Eclipse plugin.  I then installed the Gradle plugin from the Eclipse marketplace.  Yes, this could be done at the command line.  I’m used to M2Eclipse IDE integration so wanted the same for Gradle.  This step went as smoothly as any other plugin.

Creating a new gradle project

Just like Maven, the first step is to create a new Gradle project.  Since Groovy Quickstart wasn’t in the list, I choose Java quickstart.   The create request appeared to hang, being at 0% for over five minutes.  This was the first (and really only) problem.  I killed Eclipse and started over.  There was no point in doing that.  It just takes long. Apparently, this is a known issue. I tried again and after 5 minutes Gradle did download dependencies from the maven repository.

Making the Java project a Groovy project

Java quickstart does exactly what it sounds like.  It creates a project using the “Maven way” directory structure for Java.  To adapt this to a Groovy project, I:

  1. hand edited build.gradle to add
    apply plugin: 'groovy'
  2. hand edited build.gradle dependencies section to add
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.7.10'

    (I actually missed this step on the first try and got the error “error “you must assign a Groovy library to the ‘groovy’ configuration”.  The code was documented here.)

  3. created src/main/groovy and src/test/groovy directories
  4. gradle > refresh source folders.  This is like Maven where you need to refresh dependencies and the like to sync the Eclipse workspace.
  5. gradle > build > click build (compile and test)

Impressions of the gradle plugin

  1. I’ve mentioned a few times that it is very similar to the Maven plugin.  This is great as the motions feel very familiar and only the part that is new is gradle itself.  (Well that and refreshing my groovy knowledge – it’s been a while.)
  2. You can run your GroovyTestCase classes through Eclipse without Gradle (via run as junit test)
  3. My first build (with one class and one test class) including some downloading the internet took 1 minute and 2 seconds.  My second build took literally two seconds.
  4. I like the “up to date check” so only some targets get run.
  5. I like that you get an Eclipse pop-up if any unit tests fail.
 
This blog post also motivated me to start using my github account to make it easy to show the code.  In particular, the build.gradle file or the whole project. (This class doesn't require any programming so I think it is ok to put this online.  If Coursera complains, I will take it down.)