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

获取gradle模块的名称,以及它所依赖的任何其他模块的名称,gradle,android-gradle-plugin,gradle-plugin,Gradle,Android Gradle Plugin,Gradle Plugin,假设我有一个多模块的gradle项目,有两个gradle模块,:a和:B . ├── :A │   └── build.gradle ├── :B │   └── build.gradle ├── build.gradle (plugin applied here) └── settings.gradle :A没有依赖项 :B依赖于:a 我想得到以下信息 项目中每个模块的名称列表::a,:B 每个模块依赖的模块名称列表。对于:A,这将是一个空列表,而对于:B则是列表(“:A”)(

假设我有一个多模块的gradle项目,有两个gradle模块,
:a
:B

.
├── :A
│   └── build.gradle
├── :B
│   └── build.gradle
├── build.gradle     (plugin applied here)
└── settings.gradle
  • :A
    没有依赖项
  • :B
    依赖于
    :a
我想得到以下信息

  • 项目中每个模块的名称列表:
    :a
    :B
  • 每个模块依赖的模块名称列表。对于
    :A
    ,这将是一个空列表,而对于
    :B
    则是
    列表(“:A”)
    (单元素列表)
如何在应用于根gradle模块的自定义gradle插件的上下文中获取此信息


此用例用于生成多模块项目中每个模块的连接方式的可视化表示

这里有一个片段,用于遍历每个
配置
,并从中获取
项目依赖关系
类型。它使用在评估所有项目后执行的。搞清楚过渡词或保持谁依赖于谁的观念是没有任何作用的,但这有希望给你一个起点,让你知道如何实现你想要的

gradle.projectsevaluate{
println('已加载的项目')
println('*'*15)
allprojects.forEach{proj->
最终列表projectDependencies=proj.configurations.collectMany{Configuration-Configuration->
配置.allDependencies
}.findAll{依赖关系->
ProjectDependency的依赖实例
}.收集{
它作为项目依赖关系
}.unique().collect()
println(“项目${proj.name}”)
println(projectDependencies.collect{“${it.name}->${it.dependencyProject.path}}}.join(System.lineSeparator()))
println()
}
}
我在存储库中试用了它,得到了以下输出:

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

你是说
A
B
以及多模块项目中的项目还是多模块项目本身?@Opal多模块项目(标准安卓项目设置)我相信你能简单地勾勒一下结构以及相关的
*.gradle
文件吗?简单的ASCII树就足够了。添加,请参阅edit@Opal哈哈,好的捕获-修复!这很好,但是请记住,这段代码中使用了gradl的内部API。@Opal我认为我没有使用任何内部API(据我所知)。你说得对!我认为
ProjectDependency
来自内部API。酷!:)我的项目的依赖项(Android)是
DefaultDependencySet
,而不是
ProjectDependency
。隐马尔可夫模型。。