Search in sources :

Example 1 with DependencyGraph

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

the class DashboardMain method generateArtifactReport.

private static ArtifactResults generateArtifactReport(Configuration configuration, Path output, Artifact artifact, ArtifactInfo artifactInfo, List<DependencyGraph> globalDependencies, ImmutableMap<ClassPathEntry, ImmutableSet<LinkageProblem>> linkageProblemTable, ClassPathResult classPathResult, Bom bom) throws IOException, TemplateException {
    String coordinates = Artifacts.toCoordinates(artifact);
    File outputFile = output.resolve(coordinates.replace(':', '_') + ".html").toFile();
    try (Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
        // 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);
        Map<Artifact, Artifact> globalUpperBoundFailures = findUpperBoundsFailures(collectLatestVersions(globalDependencies), transitiveDependencies);
        long totalLinkageErrorCount = linkageProblemTable.values().stream().flatMap(problemToClasses -> problemToClasses.stream().map(LinkageProblem::getSymbol)).distinct().count();
        Template report = configuration.getTemplate("/templates/component.ftl");
        Map<String, Object> templateData = new HashMap<>();
        DefaultObjectWrapper wrapper = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_28).build();
        TemplateHashModel staticModels = wrapper.getStaticModels();
        templateData.put("linkageProblem", staticModels.get(LinkageProblem.class.getName()));
        templateData.put("artifact", artifact);
        templateData.put("updates", convergenceIssues);
        templateData.put("upperBoundFailures", upperBoundFailures);
        templateData.put("globalUpperBoundFailures", globalUpperBoundFailures);
        templateData.put("lastUpdated", LocalDateTime.now());
        templateData.put("dependencyGraph", graph);
        templateData.put("linkageProblems", linkageProblemTable);
        templateData.put("classPathResult", classPathResult);
        templateData.put("totalLinkageErrorCount", totalLinkageErrorCount);
        templateData.put("coordinates", bom.getCoordinates());
        report.process(templateData, out);
        ArtifactResults results = new ArtifactResults(artifact);
        results.addResult(TEST_NAME_UPPER_BOUND, upperBoundFailures.size());
        results.addResult(TEST_NAME_GLOBAL_UPPER_BOUND, globalUpperBoundFailures.size());
        results.addResult(TEST_NAME_DEPENDENCY_CONVERGENCE, convergenceIssues.size());
        results.addResult(TEST_NAME_LINKAGE_CHECK, (int) totalLinkageErrorCount);
        return results;
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) 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) DefaultObjectWrapperBuilder(freemarker.template.DefaultObjectWrapperBuilder) Template(freemarker.template.Template) TemplateHashModel(freemarker.template.TemplateHashModel) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ClassFile(com.google.cloud.tools.opensource.classpath.ClassFile) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with DependencyGraph

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

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.buildVerboseDependencyGraph(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 3 with DependencyGraph

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

the class LinkageCheckerRule method buildClassPathResult.

private static ClassPathResult buildClassPathResult(DependencyResolutionResult result) throws EnforcerRuleException {
    // The root node must have the project's JAR file
    DependencyNode root = result.getDependencyGraph();
    File rootFile = root.getArtifact().getFile();
    if (rootFile == null) {
        throw new EnforcerRuleException("The root project artifact is not associated with a file.");
    }
    List<Dependency> unresolvedDependencies = result.getUnresolvedDependencies();
    Set<Artifact> unresolvedArtifacts = unresolvedDependencies.stream().map(Dependency::getArtifact).collect(toImmutableSet());
    DependencyGraph dependencyGraph = DependencyGraph.from(root);
    AnnotatedClassPath annotatedClassPath = new AnnotatedClassPath();
    ImmutableList.Builder<UnresolvableArtifactProblem> problems = ImmutableList.builder();
    for (DependencyPath path : dependencyGraph.list()) {
        Artifact artifact = path.getLeaf();
        if (unresolvedArtifacts.contains(artifact)) {
            problems.add(new UnresolvableArtifactProblem(artifact));
        } else {
            annotatedClassPath.put(new ClassPathEntry(artifact), path);
        }
    }
    return new ClassPathResult(annotatedClassPath, problems.build());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Artifact(org.eclipse.aether.artifact.Artifact) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) DependencyNode(org.eclipse.aether.graph.DependencyNode) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) File(java.io.File)

Example 4 with DependencyGraph

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

the class LinkageCheckerRule method buildClasspathFromException.

/**
 * Returns class path built from partial dependency graph of {@code resolutionException}.
 */
private static ClassPathResult buildClasspathFromException(DependencyResolutionException resolutionException) throws EnforcerRuleException {
    DependencyResolutionResult result = resolutionException.getResult();
    for (Throwable cause = resolutionException.getCause(); cause != null; cause = cause.getCause()) {
        if (cause instanceof ArtifactTransferException) {
            DependencyNode root = result.getDependencyGraph();
            DependencyGraph graph = new DependencyGraph(root);
            ArtifactTransferException artifactException = (ArtifactTransferException) cause;
            Artifact artifact = artifactException.getArtifact();
            String warning = graph.createUnresolvableArtifactProblem(artifact).toString();
            logger.warn(warning);
            break;
        }
    }
    if (result.getResolvedDependencies().isEmpty()) {
        // Nothing is resolved. Probably failed at collection phase before resolve phase.
        throw new EnforcerRuleException("Unable to collect dependencies", resolutionException);
    } else {
        // The exception is acceptable enough to build a class path.
        return buildClassPathResult(result);
    }
}
Also used : ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) DependencyNode(org.eclipse.aether.graph.DependencyNode) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) Artifact(org.eclipse.aether.artifact.Artifact)

Example 5 with DependencyGraph

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

the class GradleDependencyMediationTest method testMediation_noDuplicates.

@Test
public void testMediation_noDuplicates() throws InvalidVersionSpecificationException {
    DependencyGraph graph = new DependencyGraph(null);
    // The old version comes first in the graph.list
    graph.addPath(new DependencyPath(null).append(new Dependency(artifactA1, "compile")));
    graph.addPath(new DependencyPath(null).append(new Dependency(artifactA2, "compile")));
    // The duplicate shouldn't appear in the class path
    graph.addPath(new DependencyPath(null).append(new Dependency(artifactA1, "compile")));
    AnnotatedClassPath result = mediation.mediate(graph);
    Truth.assertThat(result.getClassPath()).hasSize(1);
    // Gradle chooses the highest version
    Truth.assertThat(result.getClassPath()).comparingElementsUsing(CLASS_PATH_ENTRY_TO_ARTIFACT).containsExactly(artifactA2);
}
Also used : DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Dependency(org.eclipse.aether.graph.Dependency) 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