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