如何获得拓扑排序的Gradle子项目列表?

时间:2013-10-22 18:10:30

标签: groovy gradle

我有一个工具,需要在多模块项目中进行拓扑排序的子项目。 Gradle有办法提供这个吗?

3 个答案:

答案 0 :(得分:3)

定义拓扑排序。您可以通过引用subprojects来获取rootProject的build.gradle文件中的子项目列表。像这样:

subprojects.each { p ->
  println p.path
}

您可以按路径对其进行排序(这是完全限定的名称(即:module:submodule

答案 1 :(得分:1)

以下内容适用于我的目的。

class ProjectCategory {
    static List<Project> topologicallySortedProjectDependencies(Project project) {
        topologicallySortedProjectDependenciesImpl(project).unique().reverse()
    }

    private static List<Project> topologicallySortedProjectDependenciesImpl(Project project) {
        final dependencies = project.configurations.compile.dependencies.findAll { dependency ->
            dependency instanceof ProjectDependency
        } collect { ProjectDependency projectDependency ->
            topologicallySortedProjectDependenciesImpl(projectDependency.dependencyProject)
        } flatten()

        dependencies + project
    }
}

task topologicallySortedProjects << {
    println project(':').toString().capitalize()

    use(ProjectCategory) {
        project.topologicallySortedProjectDependencies().each { projectDependency ->
            println "+--- ${projectDependency.toString().capitalize()}"
        }
    }
}

答案 2 :(得分:0)

以下对我有用:

final class ProjectManager {

    private static printSubProjects(Project project) {
        project.subprojects.each { p ->
            println p.path
        }
    }
}

rootProject {

    task listAllProjects << {
        println project(':').toString()
        ProjectManager.printSubProjects(project)
    }
}
  

cd root_gradle_project

     

gradle listAllProjects