Search in sources :

Example 6 with ClassPathBuilder

use of com.google.cloud.tools.opensource.classpath.ClassPathBuilder in project cloud-opensource-java by GoogleCloudPlatform.

the class BomContentTest method findNoDowngradeViolation.

/**
 * Returns messages describing the violation of the no-downgrade rule by {@code artifact} against
 * the BOM containing {@code bomArtifacts}. An empty list if there is no violations.
 */
private static ImmutableList<String> findNoDowngradeViolation(Map<String, Artifact> bomArtifacts, Artifact artifact) throws InvalidVersionSpecificationException {
    ImmutableList.Builder<String> violations = ImmutableList.builder();
    ClassPathBuilder classPathBuilder = new ClassPathBuilder();
    ClassPathResult result = classPathBuilder.resolve(ImmutableList.of(artifact), false, DependencyMediation.MAVEN);
    for (ClassPathEntry entry : result.getClassPath()) {
        Artifact transitiveDependency = entry.getArtifact();
        String key = Artifacts.makeKey(transitiveDependency);
        Artifact bomArtifact = bomArtifacts.get(key);
        if (bomArtifact == null) {
            // transitiveDependency is not part of the BOM
            continue;
        }
        Version versionInBom = versionScheme.parseVersion(bomArtifact.getVersion());
        Version versionInTransitiveDependency = versionScheme.parseVersion(transitiveDependency.getVersion());
        if (versionInTransitiveDependency.compareTo(versionInBom) <= 0) {
            // the no-downgrade rule.
            continue;
        }
        // A violation of the no-downgrade rule is found.
        violations.add(artifact + " has a transitive dependency " + transitiveDependency + ". This is higher version than " + bomArtifact + " in the BOM");
    }
    return violations.build();
}
Also used : Version(org.eclipse.aether.version.Version) ImmutableList(com.google.common.collect.ImmutableList) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) Artifact(org.eclipse.aether.artifact.Artifact)

Example 7 with ClassPathBuilder

use of com.google.cloud.tools.opensource.classpath.ClassPathBuilder in project java-cloud-bom by googleapis.

the class BomContentTest method findNoDowngradeViolation.

/**
 * Returns messages describing the violation of the no-downgrade rule by {@code artifact} against
 * the BOM containing {@code bomArtifacts}. An empty list if there is no violations.
 */
private static ImmutableList<String> findNoDowngradeViolation(Map<String, Artifact> bomArtifacts, Artifact artifact) throws InvalidVersionSpecificationException {
    ImmutableList.Builder<String> violations = ImmutableList.builder();
    ClassPathBuilder classPathBuilder = new ClassPathBuilder();
    ClassPathResult result = classPathBuilder.resolve(ImmutableList.of(artifact), false, DependencyMediation.MAVEN);
    for (ClassPathEntry entry : result.getClassPath()) {
        Artifact transitiveDependency = entry.getArtifact();
        String key = Artifacts.makeKey(transitiveDependency);
        Artifact bomArtifact = bomArtifacts.get(key);
        if (bomArtifact == null) {
            // transitiveDependency is not part of the BOM
            continue;
        }
        Version versionInBom = versionScheme.parseVersion(bomArtifact.getVersion());
        Version versionInTransitiveDependency = versionScheme.parseVersion(transitiveDependency.getVersion());
        if (versionInTransitiveDependency.compareTo(versionInBom) <= 0) {
            // the no-downgrade rule.
            continue;
        }
        // Filter by scopes that are invisible to library users
        ImmutableList<DependencyPath> dependencyPaths = result.getDependencyPaths(entry);
        Verify.verify(!dependencyPaths.isEmpty(), "The class path entry should have at least one dependency path from the root");
        boolean dependencyVisibleToUsers = false;
        for (DependencyPath dependencyPath : dependencyPaths) {
            int length = dependencyPath.size();
            // As the root element is an empty node, the last element is at "length - 2".
            Dependency dependency = dependencyPath.getDependency(length - 2);
            if (dependencyScopesVisibleToUsers.contains(dependency.getScope())) {
                dependencyVisibleToUsers = true;
                break;
            }
        }
        if (!dependencyVisibleToUsers) {
            // provided scope.
            continue;
        }
        // A violation of the no-downgrade rule is found.
        violations.add(artifact + " has a transitive dependency " + transitiveDependency + ". This is higher version than " + bomArtifact + " in the BOM. Example dependency path: " + dependencyPaths.get(0));
    }
    return violations.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Dependency(org.eclipse.aether.graph.Dependency) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) Artifact(org.eclipse.aether.artifact.Artifact) Version(org.eclipse.aether.version.Version)

Aggregations

ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)7 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)7 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)7 Artifact (org.eclipse.aether.artifact.Artifact)5 LinkageProblem (com.google.cloud.tools.opensource.classpath.LinkageProblem)3 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)3 ImmutableList (com.google.common.collect.ImmutableList)3 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)2 LinkageChecker (com.google.cloud.tools.opensource.classpath.LinkageChecker)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 Version (org.eclipse.aether.version.Version)2 ClassReferenceGraph (com.google.cloud.tools.opensource.classpath.ClassReferenceGraph)1 Bom (com.google.cloud.tools.opensource.dependencies.Bom)1 DependencyGraphBuilder (com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder)1 IOException (java.io.IOException)1 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)1 MavenSession (org.apache.maven.execution.MavenSession)1 MojoExecution (org.apache.maven.plugin.MojoExecution)1