How to write properties to 3rd party Eclipse .settings files?


(Ryan Nelson) #1

One of our projects uses GWT, and for the most part Gradle builds the project fine. However, we’ve hit a minor hitch with integration in Eclipse.

In order to launch GWT in dev mode, a certain set of properties needs to be checked under project preferences in Eclipse. (Explanation here if you’re curious). These properties are stored in the .settings directory of the Eclipse project, in a special file called com.google.gdt.eclipse.core.prefs.

Is there any way for me to write settings to this file with the current Gradle tools I have? (I.e., without custom-rolling a plugin).


(Peter Niederwieser) #2

You’ll have to write custom code. It doesn’t have to be a plugin; it could just be an ad-hoc task. Groovy often makes writing such code a breeze.


(Ryan Nelson) #3

Here’s what I came up with:

doLast {
  def props = new Properties()
  file(".settings/com.google.gdt.eclipse.core.prefs").withInputStream {
   stream -> props.load(stream)
  }
  props.setProperty("warSrcDir", "src/main/webapp")
  props.setProperty("warSrcDirIsOutput", "true")
  file(".settings/com.google.gdt.eclipse.core.prefs").withOutputStream {
   stream -> props.store(stream, null)
  }
 }

However, Gradle would not let me add this to the eclipse task. It says there is no doLast() method. I also tried adding it in a separate task, and creating a dependency on that task in eclipse, and Gradle tells me there is also no dependsOn() method.

Is the eclipse task not a standard task?

I was able to get it working by adding it to the eclipseProject task instead.


(Peter Niederwieser) #4

The ‘eclipse’ plugin also introduces a ‘project.eclipse’ model object, which means that you’ll have to use ‘tasks.eclipse’ to get hold of the task.

I’d probably make this its own task, and make ‘tasks.eclipse’ depend on it. This is in line with ‘eclipseProject’, ‘eclipseClasspath’, etc.


(Ryan Nelson) #5

So when I do:

eclipse {
   // ... configuration here ...
}

I’m operating on the model object, rather than the task?


(Peter Niederwieser) #6

Correct. The ‘eclipse’ task is a lifecycle task (like ‘build’, ‘check’, etc.). It’s sole purpose is to provide a convenient way to run all Eclipse-related tasks (like ‘eclipseProject’ and ‘eclipseClasspath’) together. There is nothing to configure on the ‘eclipse’ task itself.