2017-09-22 237 views
3

假设我有一个带有两个gradle模块的多模块gradle项目,分别为:A:B获取gradle模块的名称以及它依赖的任何其他模块的名称

. 
├── :A 
│   └── build.gradle 
├── :B 
│   └── build.gradle 
├── build.gradle  (plugin applied here) 
└── settings.gradle 
  • :A:A

我想获得以下信息没有依赖

  • :B具有相关性。

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

    我怎样才能在套用至根gradle这个模块自定义gradle这个插件的背景下,这个信息?

    的应用案例,这是生成的每个模块是如何连接在一个多模块项目

  • +0

    你的意思是'A'和'B'和多模块项目或多模块项目本身的项目? – Opal

    +0

    @Opal多模块项目(标准安卓项目设置)我相信 – ZakTaccardi

    +0

    您能简单地勾画一下结构以及相关的'* .gradle'文件吗?简单的ASCII树就足够了。 – Opal

    回答

    3

    这里的可视化表示是通过每个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 
    
    +0

    这很不错,但是请记住gradl'es内部API用于此代码。 – Opal

    +0

    @Opal我不认为我使用任何内部API(据我所知)。 – mkobit

    +1

    你是对的!我认为'ProjectDependency'来自内部API。凉! :) – Opal