Is there a way to list task dependencies in Gradle?

后端 未结 10 976
逝去的感伤
逝去的感伤 2020-12-07 10:33

./gradle tasks lists \"some\" of the tasks. Looking at http://gradle.org/docs/current/userguide/java_plugin.html there are hidden ones not listed. Also, othe

相关标签:
10条回答
  • 2020-12-07 11:15

    list the tasks and what tasks they depend on (sort of like maven's depenceny:tree but for tasks)

    for this you can use --dry-run (or -m) option which lists tasks which are executed in order for particular command, but does not execute the command, e.g.

    gradle assemble --dry-run
    

    you can find more here

    0 讨论(0)
  • 2020-12-07 11:15

    You can stick this into your build.gradle:

    gradle.taskGraph.whenReady {taskGraph ->
        println "Found task graph: " + taskGraph
        println "Found " + taskGraph.allTasks.size() + " tasks."
        taskGraph.allTasks.forEach { task ->
            println task
            task.dependsOn.forEach { dep ->
                println "  - " + dep
            }
        }
    }
    

    or this into your build.gradle.kts:

    gradle.taskGraph.whenReady(closureOf<TaskExecutionGraph> {
        println("Found task graph: $this")
        println("Found " + allTasks.size + " tasks.")
        allTasks.forEach { task ->
            println(task)
            task.dependsOn.forEach { dep ->
                println("  - $dep")
            }
        }
    })
    

    Then run your task with gradle:

    ./gradlew build
    

    And you should see this:

    Found task graph: org.gradle.execution.taskgraph.DefaultTaskGraphExecuter@36eb780c
    Found 19 tasks.
    task ':compileJava'
      - task 'compileJava' input files
    task ':compileScala'
      - task 'compileScala' input files
      - compileJava
    task ':processResources'
      - task 'processResources' input files
    task ':classes'
      - org.gradle.api.internal.tasks.DefaultTaskDependency@287a7782
      - task 'classes' input files
      - compileJava
      - dirs
      - compileScala
      - processResources
    task ':jar'
      - task 'jar' input files
    task ':assemble'
      - task 'assemble' input files
      - org.gradle.api.internal.artifacts.DefaultPublishArtifactSet$ArtifactsTaskDependency@5bad9616
    task ':compileTestJava'
        - task 'compileTestJava' input files
    task ':compileTestScala'
      - task 'compileTestScala' input files
      - compileTestJava
    task ':processTestResources'
      - task 'processTestResources' input files
    task ':testClasses'
      - processTestResources
      - task 'testClasses' input files
      - compileTestScala
      - org.gradle.api.internal.tasks.DefaultTaskDependency@42c1fa08
      - compileTestJava
      - dirs
    task ':compileIntegrationTestJava'
      - task 'compileIntegrationTestJava' input files
    task ':compileIntegrationTestScala'
      - task 'compileIntegrationTestScala' input files
      - compileIntegrationTestJava
    task ':processIntegrationTestResources'
      - task 'processIntegrationTestResources' input files
    task ':integrationTestClasses'
      - processIntegrationTestResources
      - compileIntegrationTestJava
      - org.gradle.api.internal.tasks.DefaultTaskDependency@7c8aa0fe
      - compileIntegrationTestScala
      - dirs
      - task 'integrationTestClasses' input files
    task ':composeUp'
      - task 'composeUp' input files
    task ':integrationTest'
      - task ':composeUp'
      - task 'integrationTest' input files
    task ':test'
      - task 'test' input files
    task ':check'
      - task 'check' input files
      - task ':test'
      - task ':integrationTest'
    task ':build'
      - task 'build' input files
      - check
      - assemble
    
    0 讨论(0)
  • 2020-12-07 11:15

    You can also add the following plugin for your local environment build.gradle, https://github.com/dorongold/gradle-task-tree

    0 讨论(0)
  • 2020-12-07 11:23

    you can use the --all flag to get a more detailed listing of the available tasks and the task dependencies

    gradle tasks --all
    

    EDIT: as noted by Radim in the comments, this command does not report dependencies, for gradle 3.3 and newer (see https://docs.gradle.org/3.3/release-notes.html#improved-performance-of-tasks-report).

    0 讨论(0)
  • 2020-12-07 11:23

    Following the answer by cstroe, the following also prints the input and output files of each Gradle task. This is useful since dependencies are sometimes defined by input/output relations. I.e., if task B uses the outputs of task A, cstroe's answer won't show you the dependency. The following is very primitive but does show the list of input and output files for each task:

    gradle.taskGraph.whenReady {taskGraph ->
        println "Found task graph: " + taskGraph
        println "Found " + taskGraph.allTasks.size() + " tasks."
        taskGraph.allTasks.forEach { task ->
            println()
            println("----- " + task + " -----")
            println("depends on tasks: " + task.dependsOn)
            println("inputs: ")
            task.inputs.getFiles().getFiles().collect { f -> println(" - " + f)}
            println("outputs: ")
            task.outputs.getFiles().getFiles().collect { f -> println(" + " + f)}
        }
    }
    
    0 讨论(0)
  • 2020-12-07 11:24

    As your multiproject grows, the solution I marked as correct grows a bit unweildy and hard to read

    gradle tasks --all
    

    Instead, I have moved over to looking at a specific project making it much easier

    gradlew :full-httpproxy:tasks --all
    

    where 'full-httpproxy' is the name of my project(and directory as is typical).

    I am however curious how to list tasks on the master/root project though and have an outstanding question here as well

    How to list all tasks for the master project only in gradle?

    as doing that doesn't seem possible right now.

    0 讨论(0)
提交回复
热议问题