Is there a way to list all gradle dependencies programmatically?

后端 未结 3 1092
无人共我
无人共我 2021-01-31 11:05

I know that doing:

gradle dependencies

Lists the full dependency tree. Now, I\'m looking for a way to manipulate that dependencies tree program

3条回答
  •  逝去的感伤
    2021-01-31 11:31

    You could create a task within your Gradle file to iterate over all dependencies and generate the JSON as you see fit. Here is an example task that will pretty print the JSON for you:

    task printDependencies << {
        def json = '"dependencies": ['
    
        configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { artifact ->
            def id = artifact.moduleVersion.id
           // println "group: ${id.group}, name: ${id.name}, version: ${id.version}"
            json += JsonOutput.toJson(id)
        }
    
        json += "]"
        json = JsonOutput.prettyPrint(json)
        println json
    }
    

    With sample output:

    "dependencies": [
        {
            "group": "com.fasterxml.jackson.core",
            "version": "2.6.5",
            "name": "jackson-core",
            "module": {
                "group": "com.fasterxml.jackson.core",
                "name": "jackson-core"
            }
        }{
            "group": "org.springframework",
            "version": "4.2.5.RELEASE",
            "name": "spring-aop",
            "module": {
                "group": "org.springframework",
                "name": "spring-aop"
            }
        }
    ]
    

提交回复
热议问题