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;
}
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();
}
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());
}
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());
}
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());
}
Aggregations