How to show if dependency is an External (ie, binary) and not a Project dependency

I am trying to modify existing code that lists and examines the dependency graph.

I want to select only External dependencies (ie, binary jars, etc) and NOT Project dependencies.

https://gradle.org/docs/current/userguide/dependency_management.html#sub:project_dependencies

task zx << {

project.allprojects.each { proj ->

proj.configurations.each { conf ->

ResolutionResult rr = conf.incoming.resolutionResult

def requestedList = rr.getAllDependencies()

def resolvedList = rr.getAllModuleVersions()

requestedList.each {

DependencyResult dr = it

ResolvedModuleVersionResult rmvr = it.getFrom()

ModuleVersionSelectionReason mvsr = rmvr.getSelectionReason()

// boolean isExternalDependency =

how???

proj.logger.lifecycle "${proj.name} : dependency ‘${dr}’ "

}

}

} }

Any help is appreciated

There are a few ways you can do this. Currently you’re inspecting all of the Depenencies (edges) of the dependency graph. In most cases you want to inspect the Components (nodes) of the graph.

proj.configurations.each { conf ->
    ResolutionResult rr = conf.incoming.resolutionResult
      // Inspect the components (nodes) of the graph
    rr.getAllComponents().each { ResolvedComponentResult cr ->
        def isExternal = (cr.id instanceof ModuleComponentIdentifier)
    }
      // Inspect the dependencies (edges) of the graph
    rr.getAllDependencies().each { DependencyResult dr ->
        def isExternal = (dr.requested instanceof ModuleComponentSelector)
    }
}

Note that ‘DependencyResult.getFrom()’ points to the origin of the dependency, not the component that is the result of resolving that dependency. So your code for getting the selection reason above is not correct.

See the docs for ResolutionResult for more details.

Yes, the incorrect code is “trial and error” code I should not have posted.

I will work with your suggestion and post the final code as a reply.

Thank you very much.

Actually, the posted code was helpful to describe your problem. I just wanted to point out that using ‘getFrom()’ is probably not going to give you the results you wanted.

I am so sorry, I neglected to say I am running gradle 1.7 which does not support the ResolvedComponentResult nor the getAllComponents() method.

Is there a way in 1.7 to implement the same ‘isExternal’ check (ie, using the getAllDependencies() ) ?