Search code examples
gradlegradle-eclipse

Set EclipseProject.referencedProjects using dependencies in Gradle


I am creating Eclipse project files as shown:

eclipse {
    project {
        natures 'org.eclipse.jdt.core.javanature'
        buildCommand 'org.eclipse.jdt.core.javabuilder'
    }
    classpath {
        downloadSources true
        downloadJavadoc true
    }
}

I have a multi-project gradle build where projects reference each other and 3rd party libs. For projectA, its dependencies are:

dependencies {
    compile project(':projectB')
    compile project(':projectC')

    compile "com.google.guava:guava:${VER_GUAVA}"
}

This works great, except that the generated projects don't reference each other. It builds just fine, but it means that if I refactor something in projectB, references in projectA aren't refactored with it. The fix is apparently to set the referencedProjects variable of the eclipse configuration, but I'd like for this to be automatically populated based on the dependencies. Something like:

eclipse {
    project {
        referencedProjects dependencies.grep(dep is project)
        ...

Anybody have any hints?


Solution

  • This is how I ended up fixing it:

    // add all project dependencies as referencedProjects
    tasks.cleanEclipse.doLast {
        project.configurations.stream()
        .flatMap({config -> config.dependencies.stream()})      // get all deps
        .filter({dep -> dep instanceof ProjectDependency})      // that are Projects
        .forEach({dep -> eclipse.project.referencedProjects.add(dep.name)}) // and add to referencedProjects
    }
    // always create "fresh" projects
    tasks.eclipse.dependsOn(cleanEclipse)
    

    Probably ought to mention that I'm using the wuff plugin because I'm in an OSGI environment, and it does tricks on the dependencies. That might be the reason that I'm not getting this automatically, but the above snippet fixes it pretty easily.