获取 gradle 模块的名称,以及它所依赖的任何其他模块的名称

Zak*_*rdi 5 gradle android-gradle-plugin gradle-plugin

假设我有一个多模块 gradle 项目,其中包含两个 gradle 模块:A:B.

.
??? :A
?   ??? build.gradle
??? :B
?   ??? build.gradle
??? build.gradle     (plugin applied here)
??? settings.gradle
Run Code Online (Sandbox Code Playgroud)
  • :A 没有依赖
  • :B 有依赖 :A

我想获得以下信息。

  • 项目中每个模块的名称列表::A,:B
  • 每个模块所依赖的模块名称列表。对于:A,这将是一个空列表,因为:B它将是listOf(":A")(单元素列表)

如何在应用于根 gradle 模块的自定义 gradle 插件的上下文中获取此信息?

用例是生成每个模块如何在多模块项目中连接的可视化表示

mko*_*bit 6

下面是一个片段,用于遍历每个ConfigurationProjectDependency从中获取类型。它使用Gradle.projectsEvaluated(org.gradle.api.Action)在评估所有项目后执行的操作。它不会做任何事情来找出传递性或保留谁依赖于谁的概念,但这有望为您提供一个起点,帮助您实现您正在寻找的目标。

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()
  }
}
Run Code Online (Sandbox Code Playgroud)

我在junit-team/junit5存储库上进行了尝试,并得到了以下输出:

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
Run Code Online (Sandbox Code Playgroud)