假设我有一个包含两个gradle模块的多模块gradle项目:A
和:B
。
.
├── :A
│ └── build.gradle
├── :B
│ └── build.gradle
├── build.gradle (plugin applied here)
└── settings.gradle
:A
没有依赖关系:B
依赖:A
我想获得以下信息。
:A
,:B
:A
,这将是一个空列表,对于:B
,它将是listOf(":A")
(单个元素列表)如何在应用于根gradle模块的自定义gradle插件的上下文中获取此信息?
用例是生成多模块项目中每个模块连接方式的直观表示
答案 0 :(得分:4)
以下是浏览每个Configuration
并从中获取ProjectDependency
类型的代码段。它使用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()
}
}
我在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