Get dependency graph from files


(Bob Bell) #1

I have a project (actually, a multi-project) where the inputs and outputs are connected through RegularFilePropertys. This works great to sequence my tasks, even in parallel.

I’d really like to be able to generate some sort of dependency graph, so that we can better understand the relationship between the tasks (and projects). However, I don’t see any way to do that. The dependencies task doesn’t report on the dependencies between the tasks, as I’ve specified them. Even if I take pains to use a dependencies closure to also declare my dependencies that way (as FileCollections), the dependencies task doesn’t report anything; it seems that the dependencies task doesn’t consider FileCollections when printing its report.

Basically, I’m asking to consider the consumer and producer example from the official Gradle documentation here: https://docs.gradle.org/current/userguide/lazy_configuration.html#sec:working_with_task_dependencies_in_lazy_properties
In that simple example, what can be done to be able to show the relationship between the consumer and producer tasks?


(Bob Bell) #2

FYI, at the moment the best I have is manually adding some code like the following:

String label(Task task) {
    return task.path.substring(1).replaceAll(':myTask$', '')
}

gradle.taskGraph.with {
    whenReady { 
        mkdir project.buildDir
        File gv = new File(project.buildDir, "deps.gv")
        File png = new File(project.buildDir, "deps.png")
        gv.text = "digraph deps {\n"
        allTasks.each { task -> 
            getDependencies(task).each { dep ->
                gv.text += "  \"${label(task)}\" -> \"${label(dep)}\"\n"
            }
        }
        gv.text += "}\n"
        ("dot -Tpng " + gv.path + " -o " + png.path).execute()
    }
}

(Fred Curts) #3

The built-in dependencies task displays module dependencies, not task dependencies.

There are several third-party plugins for visualizing the task graph, e.g., https://github.com/dorongold/gradle-task-tree.