Search in sources :

Example 16 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 17 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException 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);
    }
}
Also used : ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) DependencyNode(org.eclipse.aether.graph.DependencyNode) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) Artifact(org.eclipse.aether.artifact.Artifact)

Example 18 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)

Example 19 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 20 with EnforcerRuleException

use of org.apache.maven.enforcer.rule.api.EnforcerRuleException in project jersey by jersey.

the class FilePatternDoesNotExistRule method execute.

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
    if (files == null) {
        return;
    }
    for (File file : files) {
        final String filePath = file.getPath();
        final String prefix = filePath.substring(0, filePath.indexOf("*"));
        final String dirName = prefix.substring(0, prefix.lastIndexOf(File.separator));
        final String fileItselfPattern = filePath.substring(dirName.length() + 1);
        final File dir = new File(dirName);
        if (!dir.isDirectory()) {
            continue;
        }
        final Set<File> matchedFiles = new TreeSet<>();
        for (File matchedFile : dir.listFiles((FileFilter) new WildcardFileFilter(fileItselfPattern))) {
            matchedFiles.add(matchedFile);
        }
        if (!matchedFiles.isEmpty()) {
            throw new EnforcerRuleException("Files found! " + Arrays.toString(matchedFiles.toArray()));
        }
    }
}
Also used : TreeSet(java.util.TreeSet) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter)

Aggregations

EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)35 ExpressionEvaluationException (org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException)17 File (java.io.File)12 MavenProject (org.apache.maven.project.MavenProject)12 IOException (java.io.IOException)11 Artifact (org.apache.maven.artifact.Artifact)7 Log (org.apache.maven.plugin.logging.Log)6 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)6 Path (java.nio.file.Path)4 Matcher (java.util.regex.Matcher)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 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3