Search in sources :

Example 1 with ClassPathResult

use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.

the class DashboardUnavailableArtifactTest method testDashboardWithRepositoryException.

@Test
public void testDashboardWithRepositoryException() throws IOException, TemplateException, ParsingException {
    Configuration configuration = DashboardMain.configureFreemarker();
    Artifact validArtifact = new DefaultArtifact("io.grpc:grpc-context:1.15.0");
    ArtifactResults validArtifactResult = new ArtifactResults(validArtifact);
    validArtifactResult.addResult(DashboardMain.TEST_NAME_UPPER_BOUND, 0);
    validArtifactResult.addResult(DashboardMain.TEST_NAME_DEPENDENCY_CONVERGENCE, 0);
    validArtifactResult.addResult(DashboardMain.TEST_NAME_GLOBAL_UPPER_BOUND, 0);
    Artifact invalidArtifact = new DefaultArtifact("io.grpc:nonexistent:jar:1.15.0");
    ArtifactResults errorArtifactResult = new ArtifactResults(invalidArtifact);
    errorArtifactResult.setExceptionMessage("Could not find artifact io.grpc:nonexistent:jar:1.15.0 in central" + " (https://repo1.maven.org/maven2/)");
    List<ArtifactResults> table = new ArrayList<>();
    table.add(validArtifactResult);
    table.add(errorArtifactResult);
    DashboardMain.generateDashboard(configuration, outputDirectory, table, ImmutableList.of(), ImmutableMap.of(), new ClassPathResult(new AnnotatedClassPath(), ImmutableList.of()), bom);
    Path generatedHtml = outputDirectory.resolve("artifact_details.html");
    Assert.assertTrue(Files.isRegularFile(generatedHtml));
    Document document = builder.build(generatedHtml.toFile());
    Assert.assertEquals("en-US", document.getRootElement().getAttribute("lang").getValue());
    Nodes tr = document.query("//tr");
    Assert.assertEquals("The size of rows in table should match the number of artifacts + 1 (header)", tr.size(), table.size() + 1);
    Nodes tdForValidArtifact = tr.get(1).query("td");
    Assert.assertEquals(Artifacts.toCoordinates(validArtifact), tdForValidArtifact.get(0).getValue());
    Element firstResult = (Element) (tdForValidArtifact.get(2));
    Truth.assertThat(firstResult.getValue().trim()).isEqualTo("PASS");
    Truth.assertThat(firstResult.getAttributeValue("class")).isEqualTo("pass");
    Nodes tdForErrorArtifact = tr.get(2).query("td");
    Assert.assertEquals(Artifacts.toCoordinates(invalidArtifact), tdForErrorArtifact.get(0).getValue());
    Element secondResult = (Element) (tdForErrorArtifact.get(2));
    Truth.assertThat(secondResult.getValue().trim()).isEqualTo("UNAVAILABLE");
    Truth.assertThat(secondResult.getAttributeValue("class")).isEqualTo("unavailable");
}
Also used : AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) Path(java.nio.file.Path) Configuration(freemarker.template.Configuration) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) Element(nu.xom.Element) ArrayList(java.util.ArrayList) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Document(nu.xom.Document) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Nodes(nu.xom.Nodes) Test(org.junit.Test)

Example 2 with ClassPathResult

use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerRule method findBomClasspath.

/**
 * Builds a class path for {@code bomProject}.
 */
private ClassPathResult findBomClasspath(MavenProject bomProject, RepositorySystemSession repositorySystemSession) throws EnforcerRuleException {
    ArtifactTypeRegistry artifactTypeRegistry = repositorySystemSession.getArtifactTypeRegistry();
    List<org.apache.maven.model.Dependency> managedDependencies = bomProject.getDependencyManagement().getDependencies();
    ImmutableList<Artifact> artifacts = managedDependencies.stream().map(dependency -> RepositoryUtils.toDependency(dependency, artifactTypeRegistry)).map(Dependency::getArtifact).filter(artifact -> !Bom.shouldSkipBomMember(artifact)).collect(toImmutableList());
    try {
        ClassPathResult result = classPathBuilder.resolve(artifacts, false, DependencyMediation.MAVEN);
        ImmutableList<UnresolvableArtifactProblem> artifactProblems = result.getArtifactProblems();
        if (!artifactProblems.isEmpty()) {
            throw new EnforcerRuleException("Failed to collect dependency: " + artifactProblems);
        }
        return result;
    } catch (InvalidVersionSpecificationException ex) {
        throw new EnforcerRuleException("Dependency mediation failed due to invalid version", ex);
    }
}
Also used : DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) ClassReferenceGraph(com.google.cloud.tools.opensource.classpath.ClassReferenceGraph) NonTestDependencySelector(com.google.cloud.tools.opensource.dependencies.NonTestDependencySelector) DependencyMediation(com.google.cloud.tools.opensource.classpath.DependencyMediation) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) LinkageProblemCauseAnnotator(com.google.cloud.tools.opensource.classpath.LinkageProblemCauseAnnotator) DefaultRepositoryCache(org.eclipse.aether.DefaultRepositoryCache) EnforcerRuleHelper(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper) Path(java.nio.file.Path) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MojoExecution(org.apache.maven.plugin.MojoExecution) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) WARN(org.apache.maven.enforcer.rule.api.EnforcerLevel.WARN) AbstractNonCacheableEnforcerRule(org.apache.maven.plugins.enforcer.AbstractNonCacheableEnforcerRule) LinkageChecker(com.google.cloud.tools.opensource.classpath.LinkageChecker) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) List(java.util.List) RepositoryUtils(org.apache.maven.RepositoryUtils) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) Bom(com.google.cloud.tools.opensource.dependencies.Bom) OsProperties(com.google.cloud.tools.opensource.dependencies.OsProperties) Dependency(org.eclipse.aether.graph.Dependency) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) HashMap(java.util.HashMap) LinkageProblem(com.google.cloud.tools.opensource.classpath.LinkageProblem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) FilteringZipDependencySelector(com.google.cloud.tools.opensource.dependencies.FilteringZipDependencySelector) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Nonnull(javax.annotation.Nonnull) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) MavenSession(org.apache.maven.execution.MavenSession) DependencyNode(org.eclipse.aether.graph.DependencyNode) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) IOException(java.io.IOException) Log(org.apache.maven.plugin.logging.Log) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) Paths(java.nio.file.Paths) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Artifact(org.eclipse.aether.artifact.Artifact) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem)

Example 3 with ClassPathResult

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

use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckTask method createClassPathResult.

private ClassPathResult createClassPathResult(ResolvedConfiguration configuration) {
    DependencyGraph dependencyGraph = createDependencyGraph(configuration);
    AnnotatedClassPath annotatedClassPath = new AnnotatedClassPath();
    for (DependencyPath path : dependencyGraph.list()) {
        Artifact artifact = path.getLeaf();
        annotatedClassPath.put(new ClassPathEntry(artifact), path);
    }
    return new ClassPathResult(annotatedClassPath, ImmutableList.of());
}
Also used : AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry)

Example 5 with ClassPathResult

use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckTask method findLinkageErrors.

/**
 * Returns true iff {@code configuration}'s artifacts contain linkage errors.
 */
private boolean findLinkageErrors(Configuration configuration) throws IOException {
    ClassPathResult classPathResult = createClassPathResult(configuration.getResolvedConfiguration());
    ImmutableList.Builder<ClassPathEntry> classPathEntriesBuilder = ImmutableList.builder();
    for (ResolvedArtifact resolvedArtifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) {
        ModuleVersionIdentifier moduleVersionId = resolvedArtifact.getModuleVersion().getId();
        DefaultArtifact artifact = new DefaultArtifact(moduleVersionId.getGroup(), moduleVersionId.getName(), resolvedArtifact.getClassifier(), resolvedArtifact.getExtension(), moduleVersionId.getVersion(), null, resolvedArtifact.getFile());
        classPathEntriesBuilder.add(new ClassPathEntry(artifact));
    }
    ImmutableList<ClassPathEntry> classPath = classPathEntriesBuilder.build();
    if (!classPath.isEmpty()) {
        String exclusionFileName = extension.getExclusionFile();
        Path exclusionFile = exclusionFileName == null ? null : Paths.get(exclusionFileName);
        if (exclusionFile != null && !exclusionFile.isAbsolute()) {
            // Relative path from the project root
            Path projectRoot = getProject().getRootDir().toPath();
            exclusionFile = projectRoot.resolve(exclusionFile).toAbsolutePath();
        }
        // TODO(suztomo): Specify correct entry points if reportOnlyReachable is true.
        LinkageChecker linkageChecker = LinkageChecker.create(classPath, classPath, exclusionFile);
        ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems();
        ClassPathBuilder classPathBuilder = new ClassPathBuilder();
        LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems);
        int errorCount = linkageProblems.size();
        // TODO(suztomo): Show the dependency paths to the problematic artifacts.
        if (errorCount > 0) {
            getLogger().error("Linkage Checker rule found {} error{}:\n{}", errorCount, errorCount > 1 ? "s" : "", LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult));
            ResolutionResult result = configuration.getIncoming().getResolutionResult();
            ResolvedComponentResult root = result.getRoot();
            String dependencyPaths = dependencyPathsOfProblematicJars(root, linkageProblems);
            getLogger().error(dependencyPaths);
            getLogger().info("For the details of the linkage errors, see " + "https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/Linkage-Checker-Messages");
        }
        return errorCount > 0;
    }
    // When the configuration does not have any artifacts, there's no linkage error.
    return false;
}
Also used : AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) Path(java.nio.file.Path) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) LinkageProblem(com.google.cloud.tools.opensource.classpath.LinkageProblem) ImmutableList(com.google.common.collect.ImmutableList) ResolutionResult(org.gradle.api.artifacts.result.ResolutionResult) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) LinkageChecker(com.google.cloud.tools.opensource.classpath.LinkageChecker) ResolvedComponentResult(org.gradle.api.artifacts.result.ResolvedComponentResult) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)13 Artifact (org.eclipse.aether.artifact.Artifact)10 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)9 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)8 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)8 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)7 ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)6 LinkageProblem (com.google.cloud.tools.opensource.classpath.LinkageProblem)6 Path (java.nio.file.Path)6 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)5 LinkageChecker (com.google.cloud.tools.opensource.classpath.LinkageChecker)4 ImmutableList (com.google.common.collect.ImmutableList)4 Bom (com.google.cloud.tools.opensource.dependencies.Bom)3 DependencyGraphBuilder (com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder)3 Configuration (freemarker.template.Configuration)3 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)3 Dependency (org.eclipse.aether.graph.Dependency)3 Test (org.junit.Test)3 ClassReferenceGraph (com.google.cloud.tools.opensource.classpath.ClassReferenceGraph)2 DependencyMediation (com.google.cloud.tools.opensource.classpath.DependencyMediation)2