Determining external vs sub-project dependencies

Hi,

I wanted to determine a list of dependencies split into external dependencies and sub-project dependencies.
So far I have found a few ways to get the dependencies, but cannot figure a way to split between external and sub-project.

    task diag << {
        def configuration = project.configurations.runtime
        println("1: ${resolve1(configuration).collect { it.toString() }.sort()}")
        println("2: ${resolve2(configuration).collect { it.toString() }.sort()}")
        println("3: ${resolve3(configuration).collect { it.toString() }.sort()}")
    }

private Iterable<ModuleVersionIdentifier> resolve1(Configuration config) {
    config.incoming.resolutionResult.allDependencies
        .findAll { DependencyResult result -> result instanceof ResolvedDependencyResult }
        .collect { ResolvedDependencyResult result -> result.selected.id } as Set
}

private Iterable<ModuleVersionIdentifier> resolve2(Configuration config) {
    config.incoming.resolutionResult.allComponents
        .collect { ResolvedComponentResult component -> component.moduleVersion } as Set
}

private Iterable<ModuleVersionIdentifier> resolve3(Configuration config) {
    recurseDeps(config.resolvedConfiguration.firstLevelModuleDependencies)
}

private Iterable<ModuleVersionIdentifier> recurseDeps(Iterable<ResolvedDependency> deps) {
    def result = new HashSet()
    deps.each { ResolvedDependency dep ->
        result.add(dep.module.id)
        result.addAll(recurseDeps(dep.children))
    }
    result
}

Could somebody please point me to the best way to determine which deps are external and which are sub-project?

Thanks,
Ross

1 Like

I have managed to get a bit further… this seems to work.

project.configurations.runtime.incoming.dependencies.each { Dependency dep ->
if (dep instanceof ExternalDependency) {
    ExternalDependency externalDependency = dep as ExternalDependency
    println("DEP EXTERNAL ${externalDependency.group}:${externalDependency.name}:${externalDependency.version}")

} else if (dep instanceof ProjectDependency) {
    ProjectDependency projectDependency = dep as ProjectDependency
    println("DEP PROJECT ${projectDependency}")

} else {
    println("DEP UNKNOWN ${dep}")
}

but does not include transitive dependencies. I also cannot figure how to convert a project dependency to a set of files.

What I am trying to do is - for each Java sub-project in a project:

  • get the list of maven descriptors (including transitive dependencies) that a sub-project requires; and
  • get the list of project jars that a sub-project requires (excluding anything fetched via maven repos).

Any assistance would be appreciated.
Thanks,
Ross

Finally figured something that works for what I wanted:

project.configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact artifact ->
def id = artifact.id.componentIdentifier
if (id instanceof ProjectComponentIdentifier) {
    println("ARTIFACT FILE - ${artifact.file}")
} else if (id instanceof ModuleComponentIdentifier) {
    println("ARTIFACT MODULE ${id.group}:${id.module}:${id.version}")
} else {
    println("ARTIFACT UNKNOWN ${artifact}")
}

Hopefully this is a reasonable way to get the relevant info.

Ross

1 Like