getting started with gradle in eclipse on a mac

I’ve used Gradle before although not a ton. For example, I compared eGradle and Buildship here. It’s been a while though. Which means my laptop setup has changed enough for it to be an adventure. (and not everything I needed to know was in my memory).

Eclipse Oxygen comes with Gradle installed so I thought this would be easy. And if I had never installed Java 9 on my computer, that would have been the case. After all, you just need to run: File > New > Gradle Project

The original problem

I tried that and got this error:

org.gradle.tooling.GradleConnectionException: Could not fetch model of type ‘BuildEnvironment’ using Gradle distribution ‘https://services.gradle.org/distributions/gradle-3.5-bin.zip’.

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected java.lang.Package[] java.lang.ClassLoader.getPackages() accessible: module java.base does not “opens java.lang” to unnamed module @2e59aa

I got a similar error when trying to import an existing Gradle project. I have Gradle installed on the command line and it works fine.

What didn’t work 

I want to be using Java 8. I tried the following things that didn’t help:

  • Change eclipse.ini to use Java 8
  • Remove all Java 9 JDKs from the Eclipse workspace preferences.
  • Adding a system JAVA_HOME environment variable pointing to Java 8
  • This workaround editing the build.gradle file
  • A new workspace (my teammate teases me that my workspace was around when Obama got elected – I often do an upgrade in place)
  • Gradle code for Eclipse
  • Checked my versions against what is known to work. I do have Buildship 2.2+ (2.2.1) and Gradle 4.3+ (gradle 4.5).
  • Re-installing Eclipse and not installing/updating any plugins

Updating the plugin

Then I updated the Buildship plugin in Eclipse. Since I had done this in the past, I had to:

  1. Delete the Buildship update site
  2. Re-add the current update site. (Thanks to this post for the suggestion)
  3. Update plugin
  4. Restart Eclipse

That “helped” in that I got a different error:

Caused by: org.gradle.internal.exceptions.LocationAwareException: Unable to make protected void java.net.URLClassLoader.addURL(java.net.URL) accessible: module java.base does not “opens java.net” to unnamed module @6740e13b

This time it created the project on disk and merely didn’t import it. Importing had the same problem though. There is at least one known error with Java 9 and Buildship. And I’m getting a Java 9 error so clearly, I still have something pointing to Java 9 on system.

What actually worked

Running java -version showed I had Java 9 as my system default. This is because Mac defaults to the latest version. I didn’t have a problem earlier because Eclipse itself was set to use Java 8. (except when I’m playing with Java 9.)

I actually had to disable Java 9 on the Mac so it would recognize Java 8 as the default. This post shares how:

  1. cd /Library/Java/JavaVirtualMachines
  2. cd into my Java 9 directory
  3. cd contents
  4. mv Info.plist Info.plist.disabled
  5. Restart Eclipse

And a quick review of Gradle/Buildship

  1. You can intersperse running command line gradle commands and using Buildship. Since everything is stored in the gradle directories, they remember state from each other.
  2. To run “gradle clean” or “gradle build” in Buildship the first time:
    1. Go to the Gradle Tasks view
    2. Expand project
    3. Expand build
    4. Right click clean (or build)
    5. The Gradle Executions view shows what is still running
    6. The Console view shows the command line output
  3. To run “gradle clean” or “gradle build” in BuildShip the second time:
    1. Buildship automatically creates a run configuration for any builds you run. They have clear names (ex: atlanta-tourism – clean”) so you can easily find the right one. You can sticky common ones to the top since they are normal Eclipse run configurations
  4. To delete the whole gradle cache: rm /Users/xxx/.gradle/caches
  5. To delete a specific artifact rm /Users/nyjeanne/.gradle/caches/modules-2/files-2.1/… (I forgot to include a dependency and seeing everything re-downloaded helped)

 

 

 

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.