Thursday, September 16, 2010

Customizing Eclipse files with Gradle

Currently I'm employed at XebiaLabs where we're building our Deployit product. In the team, we recently switched from Maven2 to Gradle to build Deployit, so that we could do some funky stuff with integration testing our flash UI. However some of my team-members are now missing certain magic features from m2eclipse...

When you use m2eclipse, it will not only detect all the real modules of your build. But also any other projects which are in your Eclipse workspace. We for instance have some 3rd party plugins for Deployit, which reside outside of our normal build and are in a plugins tree somewhere else. However when we run:

$ gradle eclipse

On our projects, it won't resolve these dependencies as they are not part of the multi-module build. Sounds like a nice problem to tackle with Gradle, as it is possible to customize just about anything.

After browsing through the documentation a bit, I started out with something like:

subprojects {
apply plugin: 'eclipse'
...

eclipseClasspath {
...
}}

Which was suggested in the documentation of the Gradle Eclipse plugin. However that gave me a MethodMissingException for "eclipseClasspath". So I applied the following trick to get it all to work:


subprojects {
apply plugin: 'eclipse'
...

afterEvaluate { eachProject ->
eachProject.tasks.withType(org.gradle.plugins.eclipse.EclipseClasspath).each { task ->
task.whenConfigured { classpath ->
groupDependencies = classpath.entries.findAll { entry -> entry.path.contains(project.group)}
classpath.entries.removeAll(groupDependencies)
groupDependencies.each { dep ->
projectName = "/" + dep.sourcePath.split("/")[2]
if (classpath.entries.findAll({ entry -> entry.path.equals(projectName) }).isEmpty()) {
newProject = new org.gradle.plugins.eclipse.model.ProjectDependency(projectName, true, null, new HashSet())
classpath.entries.add(newProject)
}}}}}}


What this does is scan for all dependencies which have the same group as our current project, and replace them with ProjectDependencies if that hasn't been done before.

If anyone knows how to do this without doing the afterEvaluate trick and looking up the tasks, I'd be happy to see a comment. For now it seems to work, and my colleagues are somewhat happier with Gradle again ;-)...

1 comment:

  1. Works like a dream! Do be aware that this assumes that you have imported the projects with the same group ID into the same Eclipse workspace.

    ReplyDelete