use of com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageProblemCauseAnnotatorTest method testAnnotate_dependencyInSpringRepository.
@Test
public void testAnnotate_dependencyInSpringRepository() throws IOException, RepositoryException {
DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(ImmutableList.of("https://repo.spring.io/milestone", RepositoryUtility.CENTRAL.getUrl()));
ClassPathBuilder classPathBuilderWithSpring = new ClassPathBuilder(dependencyGraphBuilder);
// io.projectreactor:reactor-core:3.4.0-M2 is in the Spring Milestones repository.
ClassPathResult classPathResult = classPathBuilderWithSpring.resolve(ImmutableList.of(new DefaultArtifact("io.projectreactor:reactor-core:3.4.0-M2"), new DefaultArtifact("org.reactivestreams:reactive-streams:0.4.0")), false, DependencyMediation.MAVEN);
ClassPathEntry reactorCore = classPathResult.getClassPath().get(0);
// A hypothetical problem where org.reactivestreams.Subscriber class is missing because the
// org.reactivestreams:reactive-streams, which contains the class, has a different version.
LinkageProblem problem = new ClassNotFoundProblem(new ClassFile(reactorCore, "reactor.util.Metrics"), new ClassSymbol("org.reactivestreams.Subscriber"));
// This should not throw exception in resolving dependency graph
LinkageProblemCauseAnnotator.annotate(classPathBuilderWithSpring, classPathResult, ImmutableSet.of(problem));
LinkageProblemCause cause = problem.getCause();
assertEquals(DependencyConflict.class, cause.getClass());
DependencyConflict conflict = (DependencyConflict) cause;
assertEquals("org.reactivestreams:reactive-streams:0.4.0", Artifacts.toCoordinates(conflict.getPathToSelectedArtifact().getLeaf()));
// io.projectreactor:reactor-core depends on reactive-streams 1.0.3
assertEquals("org.reactivestreams:reactive-streams:1.0.3", Artifacts.toCoordinates(conflict.getPathToArtifactThruSource().getLeaf()));
}
use of com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerMain method checkArtifacts.
private static Problems checkArtifacts(LinkageCheckerArguments linkageCheckerArguments) throws IOException, RepositoryException, TransformerException, XMLStreamException {
ImmutableList<Artifact> artifacts = linkageCheckerArguments.getArtifacts();
// When a BOM or Maven artifacts are passed as arguments, resolve the dependencies.
DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(linkageCheckerArguments.getMavenRepositoryUrls());
ClassPathBuilder classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder);
ClassPathResult classPathResult = classPathBuilder.resolve(artifacts, false, DependencyMediation.MAVEN);
ImmutableList<ClassPathEntry> inputClassPath = classPathResult.getClassPath();
ImmutableList<ArtifactProblem> artifactProblems = ImmutableList.copyOf(classPathResult.getArtifactProblems());
ImmutableSet<ClassPathEntry> entryPoints = ImmutableSet.copyOf(inputClassPath.subList(0, artifacts.size()));
LinkageChecker linkageChecker = LinkageChecker.create(inputClassPath, entryPoints, linkageCheckerArguments.getInputExclusionFile());
ImmutableSet<LinkageProblem> linkageProblems = findLinkageProblems(linkageChecker, linkageCheckerArguments.getReportOnlyReachable());
LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems);
return new Problems(linkageProblems, artifactProblems, classPathResult);
}
use of com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRule method execute.
@Override
public void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException {
logger = helper.getLog();
try {
MavenProject project = (MavenProject) helper.evaluate("${project}");
MavenSession session = (MavenSession) helper.evaluate("${session}");
MojoExecution execution = (MojoExecution) helper.evaluate("${mojoExecution}");
RepositorySystemSession repositorySystemSession = session.getRepositorySession();
ImmutableList<String> repositoryUrls = project.getRemoteProjectRepositories().stream().map(RemoteRepository::getUrl).collect(toImmutableList());
DependencyGraphBuilder dependencyGraphBuilder = new DependencyGraphBuilder(repositoryUrls);
classPathBuilder = new ClassPathBuilder(dependencyGraphBuilder);
boolean readingDependencyManagementSection = dependencySection == DependencySection.DEPENDENCY_MANAGEMENT;
if (readingDependencyManagementSection && (project.getDependencyManagement() == null || project.getDependencyManagement().getDependencies() == null || project.getDependencyManagement().getDependencies().isEmpty())) {
logger.warn("The rule is set to read dependency management section but it is empty.");
}
String projectType = project.getArtifact().getType();
if (readingDependencyManagementSection) {
if (!"pom".equals(projectType)) {
logger.warn("A BOM should have packaging pom");
return;
}
} else {
if (UNSUPPORTED_NONBOM_PACKAGING.contains(projectType)) {
return;
}
if (!"verify".equals(execution.getLifecyclePhase())) {
throw new EnforcerRuleException("To run the check on the compiled class files, the linkage checker enforcer rule" + " should be bound to the 'verify' phase. Current phase: " + execution.getLifecyclePhase());
}
if (project.getArtifact().getFile() == null) {
// https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/850
return;
}
}
ClassPathResult classPathResult = readingDependencyManagementSection ? findBomClasspath(project, repositorySystemSession) : findProjectClasspath(project, repositorySystemSession, helper);
ImmutableList<ClassPathEntry> classPath = classPathResult.getClassPath();
if (classPath.isEmpty()) {
logger.warn("Class path is empty.");
return;
}
List<ClassPathEntry> entryPoints = entryPoints(project, classPath);
try {
// TODO LinkageChecker.create and LinkageChecker.findLinkageProblems
// should not be two separate public methods since we always call
// findLinkageProblems immediately after create.
Path exclusionFile = this.exclusionFile == null ? null : Paths.get(this.exclusionFile);
LinkageChecker linkageChecker = LinkageChecker.create(classPath, entryPoints, exclusionFile);
ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems();
if (reportOnlyReachable) {
ClassReferenceGraph classReferenceGraph = linkageChecker.getClassReferenceGraph();
linkageProblems = linkageProblems.stream().filter(entry -> classReferenceGraph.isReachable(entry.getSourceClass().getBinaryName())).collect(toImmutableSet());
}
if (classPathResult != null) {
LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems);
}
// Count unique LinkageProblems by their symbols
long errorCount = linkageProblems.stream().map(LinkageProblem::formatSymbolProblem).distinct().count();
String foundError = reportOnlyReachable ? "reachable error" : "error";
if (errorCount > 1) {
foundError += "s";
}
if (errorCount > 0) {
String message = String.format("Linkage Checker rule found %d %s:\n%s", errorCount, foundError, LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult));
if (getLevel() == WARN) {
logger.warn(message);
} else {
logger.error(message);
logger.info("For the details of the linkage errors, see " + "https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/Linkage-Checker-Messages");
throw new EnforcerRuleException("Failed while checking class path. See above error report.");
}
} else {
// arguably shouldn't log anything on success
logger.info("No " + foundError + " found");
}
} catch (IOException ex) {
// Maven's "-e" flag does not work for EnforcerRuleException. Print stack trace here.
logger.warn("Failed to run Linkage Checker:" + ex.getMessage(), ex);
throw new EnforcerRuleException("Failed to run Linkage Checker", ex);
}
} catch (ExpressionEvaluationException ex) {
throw new EnforcerRuleException("Unable to lookup an expression " + ex.getMessage(), ex);
}
}
use of com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder in project cloud-opensource-java by GoogleCloudPlatform.
the class DashboardUnavailableArtifactTest method testDashboardForRepositoryException.
@Test
public void testDashboardForRepositoryException() throws TemplateException {
Configuration configuration = DashboardMain.configureFreemarker();
Artifact validArtifact = new DefaultArtifact("io.grpc:grpc-context:1.15.0");
Artifact nonExistentArtifact = new DefaultArtifact("io.grpc:nonexistent:jar:1.15.0");
DependencyGraphBuilder graphBuilder = new DependencyGraphBuilder();
Map<Artifact, ArtifactInfo> map = new LinkedHashMap<>();
DependencyGraph graph1 = graphBuilder.buildMavenDependencyGraph(new Dependency(validArtifact, "compile"));
DependencyGraph graph2 = graphBuilder.buildMavenDependencyGraph(new Dependency(nonExistentArtifact, "compile"));
map.put(validArtifact, new ArtifactInfo(graph1, graph2));
map.put(nonExistentArtifact, new ArtifactInfo(new RepositoryException("foo")));
ArtifactCache cache = new ArtifactCache();
cache.setInfoMap(map);
List<ArtifactResults> artifactResults = DashboardMain.generateReports(configuration, outputDirectory, cache, ImmutableMap.of(), new ClassPathResult(new AnnotatedClassPath(), ImmutableList.of()), bom);
Assert.assertEquals("The length of the ArtifactResults should match the length of artifacts", 2, artifactResults.size());
Assert.assertEquals("The first artifact result should be valid", true, artifactResults.get(0).getResult(DashboardMain.TEST_NAME_UPPER_BOUND));
ArtifactResults errorArtifactResult = artifactResults.get(1);
Assert.assertNull("The second artifact result should be null", errorArtifactResult.getResult(DashboardMain.TEST_NAME_UPPER_BOUND));
Assert.assertEquals("The error artifact result should contain error message", "foo", errorArtifactResult.getExceptionMessage());
}
Aggregations