Get name of gradle module, and the name of any other modules it depends on

Viewed 1982

Let's say I have a multi-module gradle project with two gradle modules, :A and :B.

.
├── :A
│   └── build.gradle
├── :B
│   └── build.gradle
├── build.gradle     (plugin applied here)
└── settings.gradle
  • :A has no dependencies
  • :B has a dependency on :A

I want to get the following information.

  • a list of the names of each module in project: :A, :B
  • list of module names that each module is dependant on. For :A, this would be an empty list, and for :B it would be listOf(":A") (single element list)

How can I get this information in the context of a custom gradle plugin that's applied to the root gradle module?

The use case for this is to generate a visual representation of how each module is connected in a multi-module project

1 Answers

Here is a snippet for going through each Configuration and getting the ProjectDependency types from them. It uses Gradle.projectsEvaluated(org.gradle.api.Action) which executes after all the projects are evaluated. It doesn't do anything to figure out transitives or keep a notion of who depends on who, but this can hopefully give you a starting point for how you might achieve what you are looking for.

gradle.projectsEvaluated {
  println('Projects loaded')
  println('*' * 15)
  allprojects.forEach { proj ->
    final List<ProjectDependency> projectDependencies = proj.configurations.collectMany { Configuration configuration ->
      configuration.allDependencies
    }.findAll { Dependency dependency ->
      dependency instanceof ProjectDependency
    }.collect {
      it as ProjectDependency
    }.unique().collect()
    println("Project ${proj.name}")
    println(projectDependencies.collect { "  ${it.name} -> ${it.dependencyProject.path}" }.join(System.lineSeparator()))
    println()
  }
}

I tried it out on the junit-team/junit5 repository and got the following output:

Projects loaded
***************
Project junit5


Project documentation
  junit-jupiter-api -> :junit-jupiter-api
  junit-jupiter-params -> :junit-jupiter-params
  junit-platform-runner -> :junit-platform-runner
  junit-platform-launcher -> :junit-platform-launcher
  junit-platform-console -> :junit-platform-console
  junit-vintage-engine -> :junit-vintage-engine
  junit-jupiter-engine -> :junit-jupiter-engine

Project junit-jupiter-api
  junit-platform-commons -> :junit-platform-commons

Project junit-jupiter-engine
  junit-platform-engine -> :junit-platform-engine
  junit-jupiter-api -> :junit-jupiter-api
  junit-platform-launcher -> :junit-platform-launcher
  junit-platform-runner -> :junit-platform-runner
  junit-platform-engine -> :junit-platform-engine
  junit-platform-console -> :junit-platform-console

Project junit-jupiter-migrationsupport
  junit-jupiter-api -> :junit-jupiter-api
  junit-jupiter-engine -> :junit-jupiter-engine
  junit-platform-launcher -> :junit-platform-launcher
  junit-platform-runner -> :junit-platform-runner
  junit-platform-engine -> :junit-platform-engine
  junit-platform-console -> :junit-platform-console

Project junit-jupiter-params
  junit-jupiter-api -> :junit-jupiter-api
  junit-platform-engine -> :junit-platform-engine
  junit-jupiter-engine -> :junit-jupiter-engine
  junit-platform-launcher -> :junit-platform-launcher
  junit-platform-runner -> :junit-platform-runner
  junit-platform-console -> :junit-platform-console

Project junit-platform-commons


Project junit-platform-console
  junit-platform-launcher -> :junit-platform-launcher

Project junit-platform-console-standalone
  junit-platform-console -> :junit-platform-console
  junit-jupiter-engine -> :junit-jupiter-engine
  junit-jupiter-params -> :junit-jupiter-params
  junit-vintage-engine -> :junit-vintage-engine
  junit-jupiter-api -> :junit-jupiter-api
  junit-jupiter-params -> :junit-jupiter-params

Project junit-platform-engine
  junit-platform-commons -> :junit-platform-commons

Project junit-platform-gradle-plugin
  junit-platform-console -> :junit-platform-console
  junit-platform-launcher -> :junit-platform-launcher
  junit-jupiter-api -> :junit-jupiter-api
  junit-platform-console -> :junit-platform-console
  junit-jupiter-engine -> :junit-jupiter-engine

Project junit-platform-launcher
  junit-platform-engine -> :junit-platform-engine

Project junit-platform-runner
  junit-platform-launcher -> :junit-platform-launcher
  junit-platform-suite-api -> :junit-platform-suite-api

Project junit-platform-suite-api
  junit-platform-commons -> :junit-platform-commons

Project junit-platform-surefire-provider
  junit-platform-launcher -> :junit-platform-launcher
  junit-jupiter-api -> :junit-jupiter-api
  junit-platform-runner -> :junit-platform-runner
  junit-jupiter-engine -> :junit-jupiter-engine
  junit-platform-console -> :junit-platform-console

Project junit-vintage-engine
  junit-platform-engine -> :junit-platform-engine
  junit-platform-launcher -> :junit-platform-launcher
  junit-jupiter-api -> :junit-jupiter-api
  junit-platform-runner -> :junit-platform-runner
  junit-platform-engine -> :junit-platform-engine
  junit-platform-console -> :junit-platform-console
  junit-jupiter-engine -> :junit-jupiter-engine

Project platform-tests
  junit-platform-commons -> :junit-platform-commons
  junit-platform-console -> :junit-platform-console
  junit-platform-engine -> :junit-platform-engine
  junit-platform-launcher -> :junit-platform-launcher
  junit-jupiter-api -> :junit-jupiter-api
  junit-jupiter-params -> :junit-jupiter-params
  junit-platform-runner -> :junit-platform-runner
  junit-platform-engine -> :junit-platform-engine
  junit-jupiter-engine -> :junit-jupiter-engine
  junit-vintage-engine -> :junit-vintage-engine
  junit-jupiter-migrationsupport -> :junit-jupiter-migrationsupport
  junit-platform-gradle-plugin -> :junit-platform-gradle-plugin
  junit-platform-surefire-provider -> :junit-platform-surefire-provider
Related