Search in sources :

Example 1 with DependencyGraphBuilder

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

the class LinkageProblemCauseAnnotatorTest method testAnnotate_dependencyInSpringRepository.

@Test
public void testAnnotate_dependencyInSpringRepository() throws IOException, RepositoryException {
    DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(ImmutableList.of("https://repo.spring.io/milestone", RepositoryUtility.CENTRAL.getUrl()));
    ClassPathBuilder classPathBuilderWithSpring = new ClassPathBuilder(dependencyGraphBuilder);
    // io.projectreactor:reactor-core:3.4.0-M2 is in the Spring Milestones repository.
    ClassPathResult classPathResult = classPathBuilderWithSpring.resolve(ImmutableList.of(new DefaultArtifact("io.projectreactor:reactor-core:3.4.0-M2"), new DefaultArtifact("org.reactivestreams:reactive-streams:0.4.0")), false, DependencyMediation.MAVEN);
    ClassPathEntry reactorCore = classPathResult.getClassPath().get(0);
    // A hypothetical problem where org.reactivestreams.Subscriber class is missing because the
    // org.reactivestreams:reactive-streams, which contains the class, has a different version.
    LinkageProblem problem = new ClassNotFoundProblem(new ClassFile(reactorCore, "reactor.util.Metrics"), new ClassSymbol("org.reactivestreams.Subscriber"));
    // This should not throw exception in resolving dependency graph
    LinkageProblemCauseAnnotator.annotate(classPathBuilderWithSpring, classPathResult, ImmutableSet.of(problem));
    LinkageProblemCause cause = problem.getCause();
    assertEquals(DependencyConflict.class, cause.getClass());
    DependencyConflict conflict = (DependencyConflict) cause;
    assertEquals("org.reactivestreams:reactive-streams:0.4.0", Artifacts.toCoordinates(conflict.getPathToSelectedArtifact().getLeaf()));
    // io.projectreactor:reactor-core depends on reactive-streams 1.0.3
    assertEquals("org.reactivestreams:reactive-streams:1.0.3", Artifacts.toCoordinates(conflict.getPathToArtifactThruSource().getLeaf()));
}
Also used : DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 2 with DependencyGraphBuilder

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

the class LinkageCheckerMain method checkArtifacts.

private static Problems checkArtifacts(LinkageCheckerArguments linkageCheckerArguments) throws IOException, RepositoryException, TransformerException, XMLStreamException {
    ImmutableList<Artifact> artifacts = linkageCheckerArguments.getArtifacts();
    // When a BOM or Maven artifacts are passed as arguments, resolve the dependencies.
    DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(linkageCheckerArguments.getMavenRepositoryUrls());
    ClassPathBuilder classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder);
    ClassPathResult classPathResult = classPathBuilder.resolve(artifacts, false, DependencyMediation.MAVEN);
    ImmutableList<ClassPathEntry> inputClassPath = classPathResult.getClassPath();
    ImmutableList<ArtifactProblem> artifactProblems = ImmutableList.copyOf(classPathResult.getArtifactProblems());
    ImmutableSet<ClassPathEntry> entryPoints = ImmutableSet.copyOf(inputClassPath.subList(0, artifacts.size()));
    LinkageChecker linkageChecker = LinkageChecker.create(inputClassPath, entryPoints, linkageCheckerArguments.getInputExclusionFile());
    ImmutableSet<LinkageProblem> linkageProblems = findLinkageProblems(linkageChecker, linkageCheckerArguments.getReportOnlyReachable());
    LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems);
    return new Problems(linkageProblems, artifactProblems, classPathResult);
}
Also used : ArtifactProblem(com.google.cloud.tools.opensource.dependencies.ArtifactProblem) Artifact(org.eclipse.aether.artifact.Artifact) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder)

Example 3 with DependencyGraphBuilder

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

the class LinkageCheckerRule method execute.

@Override
public void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException {
    logger = helper.getLog();
    try {
        MavenProject project = (MavenProject) helper.evaluate("${project}");
        MavenSession session = (MavenSession) helper.evaluate("${session}");
        MojoExecution execution = (MojoExecution) helper.evaluate("${mojoExecution}");
        RepositorySystemSession repositorySystemSession = session.getRepositorySession();
        ImmutableList<String> repositoryUrls = project.getRemoteProjectRepositories().stream().map(RemoteRepository::getUrl).collect(toImmutableList());
        DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(repositoryUrls);
        classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder);
        boolean readingDependencyManagementSection = dependencySection == DependencySection.DEPENDENCY_MANAGEMENT;
        if (readingDependencyManagementSection && (project.getDependencyManagement() == null || project.getDependencyManagement().getDependencies() == null || project.getDependencyManagement().getDependencies().isEmpty())) {
            logger.warn("The rule is set to read dependency management section but it is empty.");
        }
        String projectType = project.getArtifact().getType();
        if (readingDependencyManagementSection) {
            if (!"pom".equals(projectType)) {
                logger.warn("A BOM should have packaging pom");
                return;
            }
        } else {
            if (UNSUPPORTED_NONBOM_PACKAGING.contains(projectType)) {
                return;
            }
            if (!"verify".equals(execution.getLifecyclePhase())) {
                throw new EnforcerRuleException("To run the check on the compiled class files, the linkage checker enforcer rule" + " should be bound to the 'verify' phase. Current phase: " + execution.getLifecyclePhase());
            }
            if (project.getArtifact().getFile() == null) {
                // https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/850
                return;
            }
        }
        ClassPathResult classPathResult = readingDependencyManagementSection ? findBomClasspath(project, repositorySystemSession) : findProjectClasspath(project, repositorySystemSession, helper);
        ImmutableList<ClassPathEntry> classPath = classPathResult.getClassPath();
        if (classPath.isEmpty()) {
            logger.warn("Class path is empty.");
            return;
        }
        List<ClassPathEntry> entryPoints = entryPoints(project, classPath);
        try {
            // TODO LinkageChecker.create and LinkageChecker.findLinkageProblems
            // should not be two separate public methods since we always call
            // findLinkageProblems immediately after create.
            Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile);
            LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile);
            ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems();
            if (reportOnlyReachable) {
                ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph();
                linkageProblems = linkageProblems.stream().filter(entry -> classReferenceGraph.isReachable(entry.getSourceClass().getBinaryName())).collect(toImmutableSet());
            }
            if (classPathResult != null) {
                LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems);
            }
            // Count unique LinkageProblems by their symbols
            long errorCount = linkageProblems.stream().map(LinkageProblem::formatSymbolProblem).distinct().count();
            String foundError = reportOnlyReachable ? "reachable error" : "error";
            if (errorCount > 1) {
                foundError += "s";
            }
            if (errorCount > 0) {
                String message = String.format("Linkage Checker rule found %d %s:\n%s", errorCount, foundError, LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult));
                if (getLevel() == WARN) {
                    logger.warn(message);
                } else {
                    logger.error(message);
                    logger.info("For the details of the linkage errors, see " + "https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/Linkage-Checker-Messages");
                    throw new EnforcerRuleException("Failed while checking class path. See above error report.");
                }
            } else {
                // arguably shouldn't log anything on success
                logger.info("No " + foundError + " found");
            }
        } catch (IOException ex) {
            // Maven's "-e" flag does not work for EnforcerRuleException. Print stack trace here.
            logger.warn("Failed to run Linkage Checker:" + ex.getMessage(), ex);
            throw new EnforcerRuleException("Failed to run Linkage Checker", ex);
        }
    } catch (ExpressionEvaluationException ex) {
        throw new EnforcerRuleException("Unable to lookup an expression " + ex.getMessage(), ex);
    }
}
Also used : Path(java.nio.file.Path) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) LinkageProblem(com.google.cloud.tools.opensource.classpath.LinkageProblem) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) IOException(java.io.IOException) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) ClassReferenceGraph(com.google.cloud.tools.opensource.classpath.ClassReferenceGraph) MavenSession(org.apache.maven.execution.MavenSession) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) LinkageChecker(com.google.cloud.tools.opensource.classpath.LinkageChecker)

Example 4 with DependencyGraphBuilder

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

DependencyGraphBuilder (com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder)4 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)2 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)2 Artifact (org.eclipse.aether.artifact.Artifact)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 Test (org.junit.Test)2 ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)1 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)1 ClassReferenceGraph (com.google.cloud.tools.opensource.classpath.ClassReferenceGraph)1 LinkageChecker (com.google.cloud.tools.opensource.classpath.LinkageChecker)1 LinkageProblem (com.google.cloud.tools.opensource.classpath.LinkageProblem)1 ArtifactProblem (com.google.cloud.tools.opensource.dependencies.ArtifactProblem)1 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)1 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)1 Configuration (freemarker.template.Configuration)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)1 MavenSession (org.apache.maven.execution.MavenSession)1