use of com.google.cloud.tools.opensource.dependencies.DependencyGraph in project cloud-opensource-java by GoogleCloudPlatform.
the class DashboardMain method generateArtifactReport.
private static ArtifactResults generateArtifactReport(Configuration configuration, Path output, Artifact artifact, ArtifactInfo artifactInfo, List<DependencyGraph> globalDependencies, ImmutableMap<ClassPathEntry, ImmutableSet<LinkageProblem>> linkageProblemTable, ClassPathResult classPathResult, Bom bom) throws IOException, TemplateException {
String coordinates = Artifacts.toCoordinates(artifact);
File outputFile = output.resolve(coordinates.replace(':', '_') + ".html").toFile();
try (Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
// includes all versions
DependencyGraph graph = artifactInfo.getCompleteDependencies();
List<Update> convergenceIssues = graph.findUpdates();
// picks versions according to Maven rules
DependencyGraph transitiveDependencies = artifactInfo.getTransitiveDependencies();
Map<Artifact, Artifact> upperBoundFailures = findUpperBoundsFailures(graph.getHighestVersionMap(), transitiveDependencies);
Map<Artifact, Artifact> globalUpperBoundFailures = findUpperBoundsFailures(collectLatestVersions(globalDependencies), transitiveDependencies);
long totalLinkageErrorCount = linkageProblemTable.values().stream().flatMap(problemToClasses -> problemToClasses.stream().map(LinkageProblem::getSymbol)).distinct().count();
Template report = configuration.getTemplate("/templates/component.ftl");
Map<String, Object> templateData = new HashMap<>();
DefaultObjectWrapper wrapper = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_28).build();
TemplateHashModel staticModels = wrapper.getStaticModels();
templateData.put("linkageProblem", staticModels.get(LinkageProblem.class.getName()));
templateData.put("artifact", artifact);
templateData.put("updates", convergenceIssues);
templateData.put("upperBoundFailures", upperBoundFailures);
templateData.put("globalUpperBoundFailures", globalUpperBoundFailures);
templateData.put("lastUpdated", LocalDateTime.now());
templateData.put("dependencyGraph", graph);
templateData.put("linkageProblems", linkageProblemTable);
templateData.put("classPathResult", classPathResult);
templateData.put("totalLinkageErrorCount", totalLinkageErrorCount);
templateData.put("coordinates", bom.getCoordinates());
report.process(templateData, out);
ArtifactResults results = new ArtifactResults(artifact);
results.addResult(TEST_NAME_UPPER_BOUND, upperBoundFailures.size());
results.addResult(TEST_NAME_GLOBAL_UPPER_BOUND, globalUpperBoundFailures.size());
results.addResult(TEST_NAME_DEPENDENCY_CONVERGENCE, convergenceIssues.size());
results.addResult(TEST_NAME_LINKAGE_CHECK, (int) totalLinkageErrorCount);
return results;
}
}
use of com.google.cloud.tools.opensource.dependencies.DependencyGraph in project cloud-opensource-java by GoogleCloudPlatform.
the class DashboardMain method loadArtifactInfo.
/**
* This is the only method that queries the Maven repository.
*/
private static ArtifactCache loadArtifactInfo(List<Artifact> artifacts) {
Map<Artifact, ArtifactInfo> infoMap = new LinkedHashMap<>();
List<DependencyGraph> globalDependencies = new ArrayList<>();
for (Artifact artifact : artifacts) {
DependencyGraph completeDependencies = dependencyGraphBuilder.buildVerboseDependencyGraph(artifact);
globalDependencies.add(completeDependencies);
// picks versions according to Maven rules
DependencyGraph transitiveDependencies = dependencyGraphBuilder.buildMavenDependencyGraph(new Dependency(artifact, "compile"));
ArtifactInfo info = new ArtifactInfo(completeDependencies, transitiveDependencies);
infoMap.put(artifact, info);
}
ArtifactCache cache = new ArtifactCache();
cache.setInfoMap(infoMap);
cache.setGlobalDependencies(globalDependencies);
return cache;
}
use of com.google.cloud.tools.opensource.dependencies.DependencyGraph 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.DependencyGraph in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRule method buildClasspathFromException.
/**
* Returns class path built from partial dependency graph of {@code resolutionException}.
*/
private static ClassPathResult buildClasspathFromException(DependencyResolutionException resolutionException) throws EnforcerRuleException {
DependencyResolutionResult result = resolutionException.getResult();
for (Throwable cause = resolutionException.getCause(); cause != null; cause = cause.getCause()) {
if (cause instanceof ArtifactTransferException) {
DependencyNode root = result.getDependencyGraph();
DependencyGraph graph = new DependencyGraph(root);
ArtifactTransferException artifactException = (ArtifactTransferException) cause;
Artifact artifact = artifactException.getArtifact();
String warning = graph.createUnresolvableArtifactProblem(artifact).toString();
logger.warn(warning);
break;
}
}
if (result.getResolvedDependencies().isEmpty()) {
// Nothing is resolved. Probably failed at collection phase before resolve phase.
throw new EnforcerRuleException("Unable to collect dependencies", resolutionException);
} else {
// The exception is acceptable enough to build a class path.
return buildClassPathResult(result);
}
}
use of com.google.cloud.tools.opensource.dependencies.DependencyGraph in project cloud-opensource-java by GoogleCloudPlatform.
the class GradleDependencyMediationTest method testMediation_noDuplicates.
@Test
public void testMediation_noDuplicates() 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(artifactA1, "compile")));
AnnotatedClassPath result = mediation.mediate(graph);
Truth.assertThat(result.getClassPath()).hasSize(1);
// Gradle chooses the highest version
Truth.assertThat(result.getClassPath()).comparingElementsUsing(CLASS_PATH_ENTRY_TO_ARTIFACT).containsExactly(artifactA2);
}
Aggregations