Hi all. I am trying to create a custom task that c...
# gradle
a
Hi all. I am trying to create a custom task that check dependency graph, verify that some dependency are present and fail build if they are. I have a
dependency
task that prints all the dependencies, but I am not able to assemble that pieces to a working solution.
Copy code
tasks.register("checkForAndroidxDependencies") {
    description = "verify that no androidx dependencies are present"
    group = "verification"

    dependsOn("androidDependencies") // how do I get the output of the androidDependencies?
   
    doLast {
        exec {
            commandLine = listOf("grep", "androidDependencies") // Are there any way to execute the "grep" on the androidDependencies?
        }
    }
}
Any help will be much appreciated.
v
You cannot access the print output of another task. Also you shouldn't. If you want to check the dependency tree, use the proper API: https://docs.gradle.org/current/userguide/userguide_single.html#sec:programmatic_api
a
@Vampire Thank you for reply. After spending time debugging, I was not able to find a convenient way to analyze full dependency graph (including those that my 3-rd party libraries depends on). I ended up implementing custom Plugin. Something like:
Copy code
target.tasks.create<DependencyValidatorTask>("abc") {
            dependsOn("androidDependencies")
            project.configurations.all {
                resolutionStrategy.eachDependency {
                    if (this.requested.group.contains(excludeGroup)) {
                        throw IllegalArgumentException("Sorry, $excludeGroup libraries are not welcomed here.")
                    }
                }
            }
        }
But it looks like there are some limitations. I have to create this task in Plugin::apply block. otherwise If I add this block as a standalone task I am failing to run it.
Copy code
Cannot change resolution strategy of dependency configuration ':mymodule:debugAndroidTestCompileClasspath' after it has been resolved.
I understand thats because
resolutionStrategy.eachDependency
is a "mutation" api. Wonder if anybody can hint me what am I doing wrong/how can I access complete resolved dependency graph?
v
That confiugration doesn't look very sensible anyway. You are doing in the configuration phase of the
abc
task the
project.configurations.all
action. That doesn't sound nearly like anything you should want to do. Why debugging and why no convenient way? I linked you to the docs where It is documented how to access the full dependency graph.