Search in sources :

Example 1 with PathToNode

use of com.google.cloud.tools.opensource.dependencies.PathToNode in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckTask method createDependencyGraph.

private DependencyGraph createDependencyGraph(ResolvedConfiguration configuration) {
    // Why this method is not part of the DependencyGraph? Because the dependencies module
    // which the DependencyGraph belongs to is a Maven project, and Gradle does not provide good
    // Maven artifacts to develop code with Gradle-related classes.
    // For the details, see https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/1556
    // The root Gradle project is not available in `configuration`.
    DependencyGraph graph = new DependencyGraph(null);
    ArrayDeque<PathToNode<ResolvedDependency>> queue = new ArrayDeque<>();
    DependencyPath root = new DependencyPath(null);
    for (ResolvedDependency firstLevelDependency : configuration.getFirstLevelModuleDependencies()) {
        queue.add(new PathToNode<>(firstLevelDependency, root));
    }
    Set<ResolvedDependency> visited = new HashSet<>();
    while (!queue.isEmpty()) {
        PathToNode<ResolvedDependency> item = queue.poll();
        ResolvedDependency node = item.getNode();
        // Never null
        DependencyPath parentPath = item.getParentPath();
        // If there are multiple artifacts (with different classifiers) in this node, then the path is
        // the same, because these artifacts share the same dependencies with the same pom.xml.
        DependencyPath path = null;
        Set<ResolvedArtifact> moduleArtifacts = node.getModuleArtifacts();
        if (moduleArtifacts.isEmpty()) {
            // Unlike Maven's dependency tree, Gradle's dependency tree may include nodes that do not
            // have associated artifacts. BOMs, such as com.fasterxml.jackson:jackson-bom:2.12.3, fall
            // in this category. For the detailed observation, see the issue below:
            // https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2174#issuecomment-897174898
            getLogger().warn("The dependency node " + node.getName() + " does not have any artifact. Skipping.");
            path = parentPath.append(new Dependency(artifactWithPomFrom(node), "compile"));
        } else {
            // For artifacts with classifiers, there can be multiple resolved artifacts for one node
            for (ResolvedArtifact artifact : moduleArtifacts) {
                path = parentPath.append(dependencyFrom(node, artifact));
                graph.addPath(path);
            }
        }
        for (ResolvedDependency child : node.getChildren()) {
            if (visited.add(child)) {
                queue.add(new PathToNode<>(child, path));
            }
        }
    }
    return graph;
}
Also used : ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) PathToNode(com.google.cloud.tools.opensource.dependencies.PathToNode) ResolvedDependency(org.gradle.api.artifacts.ResolvedDependency) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Dependency(org.eclipse.aether.graph.Dependency) ResolvedDependency(org.gradle.api.artifacts.ResolvedDependency) ArrayDeque(java.util.ArrayDeque) HashSet(java.util.HashSet)

Aggregations

DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)1 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)1 PathToNode (com.google.cloud.tools.opensource.dependencies.PathToNode)1 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 Dependency (org.eclipse.aether.graph.Dependency)1 ResolvedArtifact (org.gradle.api.artifacts.ResolvedArtifact)1 ResolvedDependency (org.gradle.api.artifacts.ResolvedDependency)1