Getting all project dependencies


(Ahsan Rabbani) #1

Is there an easy way to get a list of all projects that a certain project depends on (including transitive project dependencies)? So if I have project A which depends on project B which depends on project C, I want to get a list containing project B and C when getting the list for project A.

Basically I’m trying to add all dependent projects’ library dependencies to the web facet of project A since the eclipse-wtp plugin doesn’t seem to do this automatically (which it should I think).


(Ahsan Rabbani) #2

I wrote a short recursive function to get what I want. Is there a better way to do this though?

def getAllDependentProjects(project) {
~
 def projectDependencies = project.configurations.runtime.getAllDependencies().withType(ProjectDependency)
    def dependentProjects = projectDependencies*.dependencyProject
    if (dependentProjects.size > 0) {
         dependentProjects.each { dependentProjects += getAllDependentProjects(it) }
    }
    return dependentProjects.unique()
}

This seems to work fine during the execution phase. However, it doesn’t seem to return all dependent projects during the configuration phase. Is that expected, and if so, how can I get them during the configuration phase?


(Peter Niederwieser) #3

I don’t know of a fundamentally easier way. Any configuration information may change until the end of the configuration phase, so you have to defer the work (at least) until then. There are several techniques for doing so, one of them being ‘gradle.projectsEvaluated { … }’.

PS: I’d be surprised if the ‘eclipse-wtp’ plugin didn’t handle dependencies correctly, as we got a lot of input/feedback back when we implemented this. Make sure to apply it to all projects (including root project).


(Ahsan Rabbani) #4

Ah, I wasn’t aware of that - applying the plugin to all projects did the trick. Thanks.


(Eric Francis ) #5

Peter,

I found this article: http://gradle.1045684.n5.nabble.com/dependsOnProjects-always-empty-td4362896.html

I noticed that the above article and this forum post both make use of the code:

def projectDependencies = project.configurations.runtime.getAllDependencies().withType(ProjectDependency)
    def dependentProjects = projectDependencies*.dependencyProject

Im getting errors:

Caused by: groovy.lang.MissingPropertyException: No such property: ProjectDependency

and:

Caused by: groovy.lang.MissingPropertyException: Could not find property 'dependencyProject'

Is the code above supported in gradle 1.6?


(Peter Niederwieser) #6

A ‘MissingPropertyException’ for what appears to be a class name indicates that you are missing an import (e.g. ‘org.gradle.api.artifacts.ProjectDependency’).