Search in sources :

Example 1 with UnresolvableArtifactProblem

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

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

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

the class ClassPathBuilderTest method testResolveClassPath_invalidCoordinate.

private void testResolveClassPath_invalidCoordinate(DependencyMediation dependencyMediation) throws RepositoryException {
    Artifact nonExistentArtifact = new DefaultArtifact("io.grpc:nosuchartifact:1.2.3");
    ClassPathResult result = classPathBuilder.resolve(ImmutableList.of(nonExistentArtifact), true, dependencyMediation);
    ImmutableList<UnresolvableArtifactProblem> artifactProblems = result.getArtifactProblems();
    assertThat(artifactProblems).hasSize(1);
    assertEquals("io.grpc:nosuchartifact:jar:1.2.3 was not resolved. Dependency path:" + " io.grpc:nosuchartifact:jar:1.2.3 (compile)", artifactProblems.get(0).toString());
    Truth.assertThat(result.getClassPath()).isEmpty();
}
Also used : UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 4 with UnresolvableArtifactProblem

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

the class ClassPathBuilderTest method testResolve_artifactProblems.

@Test
public void testResolve_artifactProblems() throws InvalidVersionSpecificationException {
    // In the full dependency tree of hibernate-core, xerces-impl:2.6.2 and xml-apis:2.6.2 are not
    // available in Maven Central.
    Artifact hibernateCore = new DefaultArtifact("org.hibernate:hibernate-core:jar:3.5.1-Final");
    ClassPathResult result = classPathBuilder.resolve(ImmutableList.of(hibernateCore), true, DependencyMediation.MAVEN);
    ImmutableList<UnresolvableArtifactProblem> artifactProblems = result.getArtifactProblems();
    List<String> coordinates = artifactProblems.stream().map(x -> x.getArtifact()).map(x -> x.toString()).collect(Collectors.toList());
    assertThat(coordinates).containsExactly("xerces:xerces-impl:jar:2.6.2", "xml-apis:xml-apis:jar:2.6.2");
}
Also used : Bom(com.google.cloud.tools.opensource.dependencies.Bom) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Assert.assertNotNull(org.junit.Assert.assertNotNull) Artifact(org.eclipse.aether.artifact.Artifact) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Correspondence(com.google.common.truth.Correspondence) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) Truth(com.google.common.truth.Truth) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RepositoryException(org.eclipse.aether.RepositoryException) Truth8(com.google.common.truth.Truth8) Optional(java.util.Optional) Assert.fail(org.junit.Assert.fail) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) Assert.assertEquals(org.junit.Assert.assertEquals) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 5 with UnresolvableArtifactProblem

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

the class LinkageChecker method create.

public static LinkageChecker create(Bom bom, Path exclusionFile) throws IOException, InvalidVersionSpecificationException {
    // duplicate code from DashboardMain follows. We need to refactor to extract this.
    ImmutableList<Artifact> managedDependencies = bom.getManagedDependencies();
    ClassPathBuilder classPathBuilder = new ClassPathBuilder();
    ClassPathResult classPathResult = classPathBuilder.resolve(managedDependencies, true, DependencyMediation.MAVEN);
    ImmutableList<ClassPathEntry> classpath = classPathResult.getClassPath();
    ImmutableList<UnresolvableArtifactProblem> artifactProblems = classPathResult.getArtifactProblems();
    if (!artifactProblems.isEmpty()) {
        for (UnresolvableArtifactProblem artifactProblem : artifactProblems) {
            logger.severe(artifactProblem.toString());
        }
        throw new IOException("Could not resolve " + (artifactProblems.size() == 1 ? "1 dependency" : (artifactProblems.size() + " dependencies")) + ". See the message above for details.");
    }
    // When checking a BOM, entry point classes are the ones in the artifacts listed in the BOM
    List<ClassPathEntry> artifactsInBom = classpath.subList(0, managedDependencies.size());
    ImmutableSet<ClassPathEntry> entryPoints = ImmutableSet.copyOf(artifactsInBom);
    return LinkageChecker.create(classpath, entryPoints, exclusionFile);
}
Also used : UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) IOException(java.io.IOException) Artifact(org.eclipse.aether.artifact.Artifact)

Aggregations

UnresolvableArtifactProblem (com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem)5 Artifact (org.eclipse.aether.artifact.Artifact)5 ImmutableList (com.google.common.collect.ImmutableList)3 IOException (java.io.IOException)3 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)2 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)2 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)2 Bom (com.google.cloud.tools.opensource.dependencies.Bom)2 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)2 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 File (java.io.File)2 List (java.util.List)2 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 Dependency (org.eclipse.aether.graph.Dependency)2 DependencyNode (org.eclipse.aether.graph.DependencyNode)2 InvalidVersionSpecificationException (org.eclipse.aether.version.InvalidVersionSpecificationException)2 ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)1