Search in sources :

Example 6 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException 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);
    }
}
Also used : DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) ClassReferenceGraph(com.google.cloud.tools.opensource.classpath.ClassReferenceGraph) NonTestDependencySelector(com.google.cloud.tools.opensource.dependencies.NonTestDependencySelector) DependencyMediation(com.google.cloud.tools.opensource.classpath.DependencyMediation) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) LinkageProblemCauseAnnotator(com.google.cloud.tools.opensource.classpath.LinkageProblemCauseAnnotator) DefaultRepositoryCache(org.eclipse.aether.DefaultRepositoryCache) EnforcerRuleHelper(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper) Path(java.nio.file.Path) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MojoExecution(org.apache.maven.plugin.MojoExecution) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) WARN(org.apache.maven.enforcer.rule.api.EnforcerLevel.WARN) AbstractNonCacheableEnforcerRule(org.apache.maven.plugins.enforcer.AbstractNonCacheableEnforcerRule) LinkageChecker(com.google.cloud.tools.opensource.classpath.LinkageChecker) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) List(java.util.List) RepositoryUtils(org.apache.maven.RepositoryUtils) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) Bom(com.google.cloud.tools.opensource.dependencies.Bom) OsProperties(com.google.cloud.tools.opensource.dependencies.OsProperties) Dependency(org.eclipse.aether.graph.Dependency) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) HashMap(java.util.HashMap) LinkageProblem(com.google.cloud.tools.opensource.classpath.LinkageProblem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) FilteringZipDependencySelector(com.google.cloud.tools.opensource.dependencies.FilteringZipDependencySelector) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Nonnull(javax.annotation.Nonnull) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) MavenSession(org.apache.maven.execution.MavenSession) DependencyNode(org.eclipse.aether.graph.DependencyNode) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) IOException(java.io.IOException) Log(org.apache.maven.plugin.logging.Log) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) Paths(java.nio.file.Paths) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Artifact(org.eclipse.aether.artifact.Artifact) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem)

Example 7 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerRule method findProjectClasspath.

/**
 * Builds a class path for {@code mavenProject}.
 */
private static ClassPathResult findProjectClasspath(MavenProject mavenProject, RepositorySystemSession session, EnforcerRuleHelper helper) throws EnforcerRuleException {
    try {
        ProjectDependenciesResolver projectDependenciesResolver = helper.getComponent(ProjectDependenciesResolver.class);
        DefaultRepositorySystemSession fullDependencyResolutionSession = new DefaultRepositorySystemSession(session);
        // Clear artifact cache. Certain artifacts in the cache have dependencies without
        // ${os.detected.classifier} interpolated. They are instantiated before 'verify' phase:
        // https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/925
        fullDependencyResolutionSession.setCache(new DefaultRepositoryCache());
        // For netty-handler referencing its dependencies with ${os.detected.classifier}
        // allowing duplicate entries
        Map<String, String> properties = new HashMap<>();
        properties.putAll(fullDependencyResolutionSession.getSystemProperties());
        properties.putAll(OsProperties.detectOsProperties());
        fullDependencyResolutionSession.setSystemProperties(properties);
        fullDependencyResolutionSession.setDependencySelector(new AndDependencySelector(new NonTestDependencySelector(), new ExclusionDependencySelector(), new OptionalDependencySelector(), new FilteringZipDependencySelector()));
        DependencyResolutionRequest dependencyResolutionRequest = new DefaultDependencyResolutionRequest(mavenProject, fullDependencyResolutionSession);
        DependencyResolutionResult resolutionResult = projectDependenciesResolver.resolve(dependencyResolutionRequest);
        return buildClassPathResult(resolutionResult);
    } catch (ComponentLookupException e) {
        throw new EnforcerRuleException("Unable to lookup a component " + e.getMessage(), e);
    } catch (DependencyResolutionException e) {
        return buildClasspathFromException(e);
    }
}
Also used : DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) HashMap(java.util.HashMap) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) DefaultRepositoryCache(org.eclipse.aether.DefaultRepositoryCache) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) FilteringZipDependencySelector(com.google.cloud.tools.opensource.dependencies.FilteringZipDependencySelector) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) NonTestDependencySelector(com.google.cloud.tools.opensource.dependencies.NonTestDependencySelector) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector)

Example 8 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException 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());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Artifact(org.eclipse.aether.artifact.Artifact) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) DependencyNode(org.eclipse.aether.graph.DependencyNode) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) File(java.io.File)

Example 9 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerRuleTest method testArtifactTransferError.

@Test
public void testArtifactTransferError() throws RepositoryException, DependencyResolutionException {
    DependencyNode graph = createResolvedDependencyGraph("org.apache.maven:maven-core:jar:3.5.2");
    DependencyResolutionResult resolutionResult = mock(DependencyResolutionResult.class);
    when(resolutionResult.getDependencyGraph()).thenReturn(graph);
    DependencyResolutionException exception = createDummyResolutionException(new DefaultArtifact("aopalliance:aopalliance:1.0"), resolutionResult);
    when(mockProjectDependenciesResolver.resolve(any())).thenThrow(exception);
    try {
        rule.execute(mockRuleHelper);
        fail("The rule should throw EnforcerRuleException upon dependency resolution exception");
    } catch (EnforcerRuleException expected) {
        verify(mockLog).warn("aopalliance:aopalliance:jar:1.0 was not resolved. " + "Dependency path: a:b:jar:0.1 > " + "org.apache.maven:maven-core:jar:3.5.2 (compile) > " + "com.google.inject:guice:jar:no_aop:4.0 (compile) > " + "aopalliance:aopalliance:jar:1.0 (compile)");
    }
}
Also used : DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 10 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerRuleTest method testExecute_shouldFailForBadProjectWithBundlePackaging.

@Test
public void testExecute_shouldFailForBadProjectWithBundlePackaging() throws RepositoryException {
    try {
        // This artifact is known to contain classes missing dependencies
        setupMockDependencyResolution("com.google.appengine:appengine-api-1.0-sdk:1.9.64");
        org.apache.maven.artifact.DefaultArtifact rootArtifact = new org.apache.maven.artifact.DefaultArtifact("com.google.cloud", "linkage-checker-rule-test", "0.0.1", "compile", // Maven Bundle Plugin uses "bundle" packaging.
        "bundle", null, new DefaultArtifactHandler());
        rootArtifact.setFile(new File("dummy.jar"));
        when(mockProject.getArtifact()).thenReturn(rootArtifact);
        rule.execute(mockRuleHelper);
        Assert.fail("The rule should raise an EnforcerRuleException for artifacts missing dependencies");
    } catch (EnforcerRuleException ex) {
        // Java 11 removed javax.activation package. Therefore the number of expected errors differs
        // between Java 8 and Java 11.
        // https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/1856
        int expectedErrorCount = System.getProperty("java.version").startsWith("1.8.") ? 112 : 117;
        // pass
        verify(mockLog).error(ArgumentMatchers.startsWith("Linkage Checker rule found " + expectedErrorCount + " errors:"));
        assertEquals("Failed while checking class path. See above error report.", ex.getMessage());
    }
}
Also used : DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Aggregations

EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)17 File (java.io.File)6 ExpressionEvaluationException (org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException)6 MavenProject (org.apache.maven.project.MavenProject)5 IOException (java.io.IOException)4 DependencyResolutionResult (org.apache.maven.project.DependencyResolutionResult)4 DependencyNode (org.eclipse.aether.graph.DependencyNode)4 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)3 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)3 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)3 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)3 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)3 Path (java.nio.file.Path)3 DependencyResolutionException (org.apache.maven.project.DependencyResolutionException)3 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)3 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)3 Artifact (org.eclipse.aether.artifact.Artifact)3 ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)2 ClassReferenceGraph (com.google.cloud.tools.opensource.classpath.ClassReferenceGraph)2 LinkageChecker (com.google.cloud.tools.opensource.classpath.LinkageChecker)2