use of com.google.cloud.tools.opensource.classpath.ClassPathBuilder in project java-cloud-bom by googleapis.
the class BomContentTest method assertUniqueClasses.
/**
* Asserts that the BOM only provides JARs which contains unique class names to the classpath.
*/
private static void assertUniqueClasses(List<Artifact> allArtifacts) throws InvalidVersionSpecificationException, IOException {
StringBuilder errorMessageBuilder = new StringBuilder();
ClassPathBuilder classPathBuilder = new ClassPathBuilder();
ClassPathResult result = classPathBuilder.resolve(allArtifacts, false, DependencyMediation.MAVEN);
// A Map of every class name to its artifact ID.
HashMap<String, String> fullClasspathMap = new HashMap<>();
for (ClassPathEntry classPathEntry : result.getClassPath()) {
Artifact currentArtifact = classPathEntry.getArtifact();
if (!currentArtifact.getGroupId().contains("google") || currentArtifact.getGroupId().contains("com.google.android") || currentArtifact.getGroupId().contains("com.google.cloud.bigtable") || currentArtifact.getArtifactId().startsWith("proto-") || currentArtifact.getArtifactId().equals("protobuf-javalite") || currentArtifact.getArtifactId().equals("appengine-testing")) {
// See: https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2226
continue;
}
String artifactCoordinates = Artifacts.toCoordinates(currentArtifact);
for (String className : classPathEntry.getFileNames()) {
if (className.contains("javax.annotation") || className.contains("$") || className.equals("com.google.cloud.location.LocationsGrpc") || className.endsWith("package-info")) {
// Ignore LocationsGrpc classes which are duplicated in generated grpc libraries.
continue;
}
String previousArtifact = fullClasspathMap.get(className);
if (previousArtifact != null) {
String msg = String.format("Duplicate class %s found in classpath. Found in artifacts %s and %s.\n", className, previousArtifact, artifactCoordinates);
errorMessageBuilder.append(msg);
} else {
fullClasspathMap.put(className, artifactCoordinates);
}
}
}
String error = errorMessageBuilder.toString();
Assert.assertTrue("Failing test due to duplicate classes found on classpath:\n" + error, error.isEmpty());
}
use of com.google.cloud.tools.opensource.classpath.ClassPathBuilder in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckTask method findLinkageErrors.
/**
* Returns true iff {@code configuration}'s artifacts contain linkage errors.
*/
private boolean findLinkageErrors(Configuration configuration) throws IOException {
ClassPathResult classPathResult = createClassPathResult(configuration.getResolvedConfiguration());
ImmutableList.Builder<ClassPathEntry> classPathEntriesBuilder = ImmutableList.builder();
for (ResolvedArtifact resolvedArtifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) {
ModuleVersionIdentifier moduleVersionId = resolvedArtifact.getModuleVersion().getId();
DefaultArtifact artifact = new DefaultArtifact(moduleVersionId.getGroup(), moduleVersionId.getName(), resolvedArtifact.getClassifier(), resolvedArtifact.getExtension(), moduleVersionId.getVersion(), null, resolvedArtifact.getFile());
classPathEntriesBuilder.add(new ClassPathEntry(artifact));
}
ImmutableList<ClassPathEntry> classPath = classPathEntriesBuilder.build();
if (!classPath.isEmpty()) {
String exclusionFileName = extension.getExclusionFile();
Path exclusionFile = exclusionFileName == null ? null : Paths.get(exclusionFileName);
if (exclusionFile != null && !exclusionFile.isAbsolute()) {
// Relative path from the project root
Path projectRoot = getProject().getRootDir().toPath();
exclusionFile = projectRoot.resolve(exclusionFile).toAbsolutePath();
}
// TODO(suztomo): Specify correct entry points if reportOnlyReachable is true.
LinkageChecker linkageChecker = LinkageChecker.create(classPath, classPath, exclusionFile);
ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems();
ClassPathBuilder classPathBuilder = new ClassPathBuilder();
LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems);
int errorCount = linkageProblems.size();
// TODO(suztomo): Show the dependency paths to the problematic artifacts.
if (errorCount > 0) {
getLogger().error("Linkage Checker rule found {} error{}:\n{}", errorCount, errorCount > 1 ? "s" : "", LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult));
ResolutionResult result = configuration.getIncoming().getResolutionResult();
ResolvedComponentResult root = result.getRoot();
String dependencyPaths = dependencyPathsOfProblematicJars(root, linkageProblems);
getLogger().error(dependencyPaths);
getLogger().info("For the details of the linkage errors, see " + "https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/Linkage-Checker-Messages");
}
return errorCount > 0;
}
// When the configuration does not have any artifacts, there's no linkage error.
return false;
}
use of com.google.cloud.tools.opensource.classpath.ClassPathBuilder in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageMonitor method run.
/**
* Returns new problems in the BOM specified by {@code groupId} and {@code artifactId}. This
* method compares the latest release of the BOM and its snapshot version which uses artifacts in
* {@link #localArtifacts}.
*/
private ImmutableSet<LinkageProblem> run(String groupId, String artifactId) throws RepositoryException, IOException, MavenRepositoryException, ModelBuildingException {
String latestBomCoordinates = RepositoryUtility.findLatestCoordinates(repositorySystem, groupId, artifactId);
logger.info("BOM Coordinates: " + latestBomCoordinates);
Bom baseline = Bom.readBom(latestBomCoordinates);
ImmutableSet<LinkageProblem> problemsInBaseline = LinkageChecker.create(baseline, null).findLinkageProblems();
Bom snapshot = copyWithSnapshot(repositorySystem, session, baseline, localArtifacts);
// Comparing coordinates because DefaultArtifact does not override equals
ImmutableList<String> baselineCoordinates = coordinatesList(baseline.getManagedDependencies());
ImmutableList<String> snapshotCoordinates = coordinatesList(snapshot.getManagedDependencies());
if (baselineCoordinates.equals(snapshotCoordinates)) {
logger.info("Snapshot is same as baseline. Not running comparison.");
logger.info("Baseline coordinates: " + Joiner.on(";").join(baselineCoordinates));
return ImmutableSet.of();
}
ImmutableList<Artifact> snapshotManagedDependencies = snapshot.getManagedDependencies();
ClassPathResult classPathResult = (new ClassPathBuilder()).resolve(snapshotManagedDependencies, true, DependencyMediation.MAVEN);
ImmutableList<ClassPathEntry> classpath = classPathResult.getClassPath();
List<ClassPathEntry> entryPointJars = classpath.subList(0, snapshotManagedDependencies.size());
ImmutableSet<LinkageProblem> problemsInSnapshot = LinkageChecker.create(classpath, ImmutableSet.copyOf(entryPointJars), null).findLinkageProblems();
if (problemsInBaseline.equals(problemsInSnapshot)) {
logger.info("Snapshot versions have the same " + problemsInBaseline.size() + " errors as baseline");
return ImmutableSet.of();
}
Set<LinkageProblem> fixedProblems = Sets.difference(problemsInBaseline, problemsInSnapshot);
if (!fixedProblems.isEmpty()) {
logger.info(messageForFixedErrors(fixedProblems));
}
Set<LinkageProblem> newProblems = Sets.difference(problemsInSnapshot, problemsInBaseline);
if (!newProblems.isEmpty()) {
logger.severe(messageForNewErrors(problemsInSnapshot, problemsInBaseline, classPathResult));
}
return ImmutableSet.copyOf(newProblems);
}
use of com.google.cloud.tools.opensource.classpath.ClassPathBuilder 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.classpath.ClassPathBuilder in project cloud-opensource-java by GoogleCloudPlatform.
the class BomContentTest method assertUniqueClasses.
/**
* Asserts that the BOM only provides JARs which contains unique class names to the classpath.
*/
private static void assertUniqueClasses(List<Artifact> allArtifacts) throws InvalidVersionSpecificationException, IOException {
StringBuilder errorMessageBuilder = new StringBuilder();
ClassPathBuilder classPathBuilder = new ClassPathBuilder();
ClassPathResult result = classPathBuilder.resolve(allArtifacts, false, DependencyMediation.MAVEN);
// A Map of every class name to its artifact ID.
HashMap<String, String> fullClasspathMap = new HashMap<>();
for (ClassPathEntry classPathEntry : result.getClassPath()) {
Artifact currentArtifact = classPathEntry.getArtifact();
if (!currentArtifact.getGroupId().contains("google") || currentArtifact.getGroupId().contains("com.google.android") || currentArtifact.getGroupId().contains("com.google.cloud.bigtable") || currentArtifact.getArtifactId().startsWith("proto-") || currentArtifact.getArtifactId().equals("protobuf-javalite") || currentArtifact.getArtifactId().equals("appengine-testing")) {
// See: https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2226
continue;
}
String artifactCoordinates = Artifacts.toCoordinates(currentArtifact);
for (String className : classPathEntry.getFileNames()) {
if (className.contains("javax.annotation") || className.contains("$") || className.equals("com.google.cloud.location.LocationsGrpc") || className.endsWith("package-info")) {
// Ignore LocationsGrpc classes which are duplicated in generated grpc libraries.
continue;
}
String previousArtifact = fullClasspathMap.get(className);
if (previousArtifact != null) {
String msg = String.format("Duplicate class %s found in classpath. Found in artifacts %s and %s.\n", className, previousArtifact, artifactCoordinates);
errorMessageBuilder.append(msg);
} else {
fullClasspathMap.put(className, artifactCoordinates);
}
}
}
String error = errorMessageBuilder.toString();
Assert.assertTrue("Failing test due to duplicate classes found on classpath:\n" + error, error.isEmpty());
}
Aggregations