Search in sources :

Example 21 with DependencyPath

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

the class GradleDependencyMediation method mediate.

@Override
public AnnotatedClassPath mediate(DependencyGraph dependencyGraph) throws InvalidVersionSpecificationException {
    AnnotatedClassPath annotatedClassPath = new AnnotatedClassPath();
    List<DependencyPath> dependencyPaths = dependencyGraph.list();
    // Step 1: Gather versions in the dependency graph.
    HashMultimap<String, Version> coordinatesToVersions = HashMultimap.create();
    GenericVersionScheme versionScheme = new GenericVersionScheme();
    for (DependencyPath dependencyPath : dependencyPaths) {
        Artifact artifact = dependencyPath.getLeaf();
        String versionlessCoordinates = Artifacts.makeKey(artifact);
        Version version = versionScheme.parseVersion(artifact.getVersion());
        coordinatesToVersions.put(versionlessCoordinates, version);
    }
    // Step 2: Select the highest version or the version in the enforcedPlatform for each
    // versionless coordinates.
    List<String> selectedCoordinates = new ArrayList<>();
    for (String versionlessCoordinates : coordinatesToVersions.keySet()) {
        if (enforcedPlatform.containsKey(versionlessCoordinates)) {
            String versionInEnforcedPlatform = enforcedPlatform.get(versionlessCoordinates);
            selectedCoordinates.add(versionlessCoordinates + ":" + versionInEnforcedPlatform);
        } else {
            ImmutableList<Version> versions = coordinatesToVersions.get(versionlessCoordinates).stream().sorted().collect(toImmutableList());
            Version highestVersion = versions.get(versions.size() - 1);
            selectedCoordinates.add(versionlessCoordinates + ":" + highestVersion.toString());
        }
    }
    // Step 3: Build annotated class path.
    for (DependencyPath dependencyPath : dependencyPaths) {
        Artifact artifact = dependencyPath.getLeaf();
        if (selectedCoordinates.contains(Artifacts.toCoordinates(artifact)) && artifact.getFile() != null) {
            // If artifact's file is null, it means there was a problem in downloading the artifact.
            // Such failure information is recorded in dependencyGraph's artifactProblems.
            annotatedClassPath.put(new ClassPathEntry(artifact), dependencyPath);
        }
    }
    return annotatedClassPath;
}
Also used : Version(org.eclipse.aether.version.Version) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) ArrayList(java.util.ArrayList) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Artifact(org.eclipse.aether.artifact.Artifact)

Example 22 with DependencyPath

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

the class GradleDependencyMediationTest method testMediation_withEnforcedPlatform.

@Test
public void testMediation_withEnforcedPlatform() 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(artifactB1, "compile")));
    GradleDependencyMediation mediation = GradleDependencyMediation.withEnforcedPlatform(new Bom("g:bom:1.0.0", ImmutableList.of(artifactA1)));
    AnnotatedClassPath result = mediation.mediate(graph);
    Truth.assertThat(result.getClassPath()).comparingElementsUsing(CLASS_PATH_ENTRY_TO_ARTIFACT).containsExactly(artifactA1, artifactB1).inOrder();
}
Also used : Bom(com.google.cloud.tools.opensource.dependencies.Bom) 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)

Example 23 with DependencyPath

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

the class LinkageProblemCauseAnnotatorTest method testAnnotate_autoServiceAnnotationsExclusion.

@Test
public void testAnnotate_autoServiceAnnotationsExclusion() throws IOException, RepositoryException {
    // Auto-value declares exclusion element for auto-service-annotations. Because of this, the
    // dependency graph does not include auto-service-annotations, resulting in a linkage error for
    // AutoServiceProcessor class.
    ClassPathBuilder builder = new ClassPathBuilder();
    ClassPathResult classPathResult = builder.resolve(ImmutableList.of(new DefaultArtifact("com.google.auto.value:auto-value:1.7.3")), false, DependencyMediation.MAVEN);
    Optional<ClassPathEntry> foundAutoService = classPathResult.getClassPath().stream().filter(entry -> entry.getArtifact().getArtifactId().equals("auto-service")).findFirst();
    ClassPathEntry autoServiceEntry = foundAutoService.get();
    LinkageProblem problem = new ClassNotFoundProblem(new ClassFile(autoServiceEntry, "com.google.auto.service.processor.AutoServiceProcessor"), new ClassSymbol("com.google.auto.service.AutoService"));
    LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, ImmutableSet.of(problem));
    LinkageProblemCause cause = problem.getCause();
    assertEquals(ExcludedDependency.class, cause.getClass());
    ExcludedDependency excludedDependency = (ExcludedDependency) cause;
    DependencyPath pathToMissingArtifact = excludedDependency.getPathToMissingArtifact();
    Artifact leaf = pathToMissingArtifact.getLeaf();
    assertEquals("auto-service-annotations", leaf.getArtifactId());
    assertEquals("auto-value", excludedDependency.getExcludingArtifact().getArtifactId());
}
Also used : Artifacts(com.google.cloud.tools.opensource.dependencies.Artifacts) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Assert.assertTrue(org.junit.Assert.assertTrue) Artifact(org.eclipse.aether.artifact.Artifact) IOException(java.io.IOException) Test(org.junit.Test) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) ImmutableList(com.google.common.collect.ImmutableList) RepositoryException(org.eclipse.aether.RepositoryException) Optional(java.util.Optional) RepositoryUtility(com.google.cloud.tools.opensource.dependencies.RepositoryUtility) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) Assert.assertEquals(org.junit.Assert.assertEquals) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 24 with DependencyPath

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

the class LinkageProblemCauseAnnotatorTest method testAnnotate_dom4jOptionalDependency.

@Test
public void testAnnotate_dom4jOptionalDependency() throws IOException, RepositoryException {
    ClassPathBuilder builder = new ClassPathBuilder();
    ClassPathResult classPathResult = builder.resolve(ImmutableList.of(new DefaultArtifact("org.dom4j:dom4j:2.1.3")), false, DependencyMediation.MAVEN);
    // Dom4j declares jaxen dependency as optional. Dom4j's org.dom4j.DocumentHelper references
    // jaxen's org.jaxen.VariableContext. This annotator should tell that this invalid reference
    // is caused by the missing optional dependency.
    ImmutableList<ClassPathEntry> dom4jDependencies = TestHelper.resolve("org.dom4j:dom4j:2.1.3");
    ClassPathEntry dom4jEntry = dom4jDependencies.get(0);
    LinkageProblem problem = new ClassNotFoundProblem(new ClassFile(dom4jEntry, "org.dom4j.DocumentHelper"), new ClassSymbol("org.jaxen.VariableContext"));
    LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, ImmutableSet.of(problem));
    LinkageProblemCause cause = problem.getCause();
    assertEquals(MissingDependency.class, cause.getClass());
    DependencyPath pathToMissingArtifact = ((MissingDependency) cause).getPathToMissingArtifact();
    Artifact leaf = pathToMissingArtifact.getLeaf();
    assertEquals("jaxen", leaf.getArtifactId());
}
Also used : DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 25 with DependencyPath

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

the class ExcludedDependencyTest method testToString.

@Test
public void testToString() {
    Artifact root = new DefaultArtifact("a:b:1");
    Artifact foo = new DefaultArtifact("com.google:foo:1");
    Artifact bar = new DefaultArtifact("com.google:bar:1");
    DependencyPath pathRootFooBar = new DependencyPath(root).append(new Dependency(foo, "test", false)).append(new Dependency(bar, "compile", true));
    ExcludedDependency cause = new ExcludedDependency(pathRootFooBar, foo);
    assertEquals("The valid symbol is in com.google:bar:jar:1 at a:b:jar:1 / " + "com.google:foo:1 (test) / com.google:bar:1 (compile, optional) " + "but it was not selected because com.google:foo:1 excludes com.google:bar.", cause.toString());
}
Also used : DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Dependency(org.eclipse.aether.graph.Dependency) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Aggregations

DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)27 Artifact (org.eclipse.aether.artifact.Artifact)17 Dependency (org.eclipse.aether.graph.Dependency)15 Test (org.junit.Test)15 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)12 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)9 ImmutableList (com.google.common.collect.ImmutableList)5 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)4 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)4 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)2 Artifacts (com.google.cloud.tools.opensource.dependencies.Artifacts)2 DependencyGraphBuilder (com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder)2 RepositoryUtility (com.google.cloud.tools.opensource.dependencies.RepositoryUtility)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 File (java.io.File)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Optional (java.util.Optional)2 RepositoryException (org.eclipse.aether.RepositoryException)2