Search in sources :

Example 11 with DependencyGraph

use of com.google.cloud.tools.opensource.dependencies.DependencyGraph in project java-cloud-bom by googleapis.

the class DashboardMain method generateArtifactReport.

private static ArtifactResults generateArtifactReport(Artifact artifact, ArtifactInfo artifactInfo) {
    // includes all versions
    DependencyGraph graph = artifactInfo.getCompleteDependencies();
    List<Update> convergenceIssues = graph.findUpdates();
    // picks versions according to Maven rules
    DependencyGraph transitiveDependencies = artifactInfo.getTransitiveDependencies();
    Map<Artifact, Artifact> upperBoundFailures = findUpperBoundsFailures(graph.getHighestVersionMap(), transitiveDependencies);
    ArtifactResults results = new ArtifactResults(artifact);
    results.addResult(TEST_NAME_UPPER_BOUND, upperBoundFailures.size());
    results.addResult(TEST_NAME_DEPENDENCY_CONVERGENCE, convergenceIssues.size());
    return results;
}
Also used : DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) Update(com.google.cloud.tools.opensource.dependencies.Update) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 12 with DependencyGraph

use of com.google.cloud.tools.opensource.dependencies.DependencyGraph in project java-cloud-bom by googleapis.

the class DashboardMain method loadArtifactInfo.

/**
 * This is the only method that queries the Maven repository.
 */
private static ArtifactCache loadArtifactInfo(List<Artifact> artifacts) {
    Map<Artifact, ArtifactInfo> infoMap = new LinkedHashMap<>();
    List<DependencyGraph> globalDependencies = new ArrayList<>();
    for (Artifact artifact : artifacts) {
        DependencyGraph completeDependencies = dependencyGraphBuilder.buildFullDependencyGraph(ImmutableList.of(artifact));
        globalDependencies.add(completeDependencies);
        // picks versions according to Maven rules
        DependencyGraph transitiveDependencies = dependencyGraphBuilder.buildMavenDependencyGraph(new Dependency(artifact, "compile"));
        ArtifactInfo info = new ArtifactInfo(completeDependencies, transitiveDependencies);
        infoMap.put(artifact, info);
    }
    ArtifactCache cache = new ArtifactCache();
    cache.setInfoMap(infoMap);
    cache.setGlobalDependencies(globalDependencies);
    return cache;
}
Also used : ArrayList(java.util.ArrayList) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) Dependency(org.eclipse.aether.graph.Dependency) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with DependencyGraph

use of com.google.cloud.tools.opensource.dependencies.DependencyGraph 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)

Example 14 with DependencyGraph

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

the class DashboardMain method collectLatestVersions.

private static Map<String, String> collectLatestVersions(List<DependencyGraph> globalDependencies) {
    Map<String, String> latestArtifacts = new TreeMap<>();
    VersionComparator comparator = new VersionComparator();
    if (globalDependencies != null) {
        for (DependencyGraph graph : globalDependencies) {
            Map<String, String> map = graph.getHighestVersionMap();
            for (String key : map.keySet()) {
                String newVersion = map.get(key);
                String oldVersion = latestArtifacts.get(key);
                if (oldVersion == null || comparator.compare(newVersion, oldVersion) > 0) {
                    latestArtifacts.put(key, map.get(key));
                }
            }
        }
    }
    return latestArtifacts;
}
Also used : VersionComparator(com.google.cloud.tools.opensource.dependencies.VersionComparator) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) TreeMap(java.util.TreeMap)

Example 15 with DependencyGraph

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

the class DashboardUnavailableArtifactTest method testDashboardForRepositoryException.

@Test
public void testDashboardForRepositoryException() throws TemplateException {
    Configuration configuration = DashboardMain.configureFreemarker();
    Artifact validArtifact = new DefaultArtifact("io.grpc:grpc-context:1.15.0");
    Artifact nonExistentArtifact = new DefaultArtifact("io.grpc:nonexistent:jar:1.15.0");
    DependencyGraphBuilder graphBuilder = new DependencyGraphBuilder();
    Map<Artifact, ArtifactInfo> map = new LinkedHashMap<>();
    DependencyGraph graph1 = graphBuilder.buildMavenDependencyGraph(new Dependency(validArtifact, "compile"));
    DependencyGraph graph2 = graphBuilder.buildMavenDependencyGraph(new Dependency(nonExistentArtifact, "compile"));
    map.put(validArtifact, new ArtifactInfo(graph1, graph2));
    map.put(nonExistentArtifact, new ArtifactInfo(new RepositoryException("foo")));
    ArtifactCache cache = new ArtifactCache();
    cache.setInfoMap(map);
    List<ArtifactResults> artifactResults = DashboardMain.generateReports(configuration, outputDirectory, cache, ImmutableMap.of(), new ClassPathResult(new AnnotatedClassPath(), ImmutableList.of()), bom);
    Assert.assertEquals("The length of the ArtifactResults should match the length of artifacts", 2, artifactResults.size());
    Assert.assertEquals("The first artifact result should be valid", true, artifactResults.get(0).getResult(DashboardMain.TEST_NAME_UPPER_BOUND));
    ArtifactResults errorArtifactResult = artifactResults.get(1);
    Assert.assertNull("The second artifact result should be null", errorArtifactResult.getResult(DashboardMain.TEST_NAME_UPPER_BOUND));
    Assert.assertEquals("The error artifact result should contain error message", "foo", errorArtifactResult.getExceptionMessage());
}
Also used : Configuration(freemarker.template.Configuration) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) RepositoryException(org.eclipse.aether.RepositoryException) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) LinkedHashMap(java.util.LinkedHashMap) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Aggregations

DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)18 Dependency (org.eclipse.aether.graph.Dependency)12 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)9 Artifact (org.eclipse.aether.artifact.Artifact)9 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)9 Test (org.junit.Test)8 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)4 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)4 LinkedHashMap (java.util.LinkedHashMap)4 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)2 Bom (com.google.cloud.tools.opensource.dependencies.Bom)2 Update (com.google.cloud.tools.opensource.dependencies.Update)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 Configuration (freemarker.template.Configuration)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)2 DependencyNode (org.eclipse.aether.graph.DependencyNode)2 ResolvedArtifact (org.gradle.api.artifacts.ResolvedArtifact)2