List all Gradle sub-projects in a Dependency Configuration

I wrote this code that will generate the set of sub-projects defined as dependencies of a project. Are there any cleaner ways to do this?

def getAllProjects( Dependency d ) {
  // ignore maven dependnecies
  if (!(d instanceof ProjectDependency)) { return [] }
  def project = d.dependencyProject
  def config
= d.projectConfiguration
  //get all dependencies of "d"
  def subDeps = config.allDependencies.withType(ProjectDependency)
  // return Set of dependency d's project, and sub-projects of d
  return [project] + subDeps.collectMany { getAllProjects(it) }
}
  task allSubProjects << {
    def projects
=
configurations.compile.allDependencies.collectMany { getAllProjects(it) }
    ....
}