Commit 46e19caa authored by Brian Long's avatar Brian Long
Browse files

added banned exceptions; using for better output

parent 196afb74
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import java.util.regex.Pattern;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.MojoFailureException;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
@@ -87,7 +88,7 @@ public class BanDependencyFilter implements DependencyFilter {
        
        if (this.failFast) {
            // plugin resolution downloads banned dependencies unless we fail now; not later
            throw new RuntimeException("Banned dependency detected: " + node + " => " + parents);
            throw new BannedDependencyException(node, parents);
        } else {
            return true;
        }
+10 −4
Original line number Diff line number Diff line
@@ -99,11 +99,17 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
                this.logger.debug("Evaluating plugin dependencies: {}", plugin);
                
                Artifact artifact = new DefaultArtifact(plugin.getId());
                try {
                    DependencyNode depNodeRoot = this.pluginDepResolver.resolve(plugin, artifact, depFilter, project.getRemotePluginRepositories(), session.getRepositorySession());
                    List<Dependency> bannedDependencies = this.crawlDependencyTree(depNodeRoot, depFilter);
                    if (!bannedDependencies.isEmpty())
                        throw new MavenExecutionException("Banned dependencies were detected in plugin '" + plugin + "': " + bannedDependencies, project.getFile());
                } catch (BannedDependencyException bde) {
                    throw new BannedPluginException(plugin, bde);
                }
            }
        } catch (BannedArtifactException bae) {
            throw new MavenExecutionException(bae.getMessage(), bae);
        } catch (PluginResolutionException pre) {
            throw new MavenExecutionException(pre.getMessage(), pre);
        }
+80 −0
Original line number Diff line number Diff line
package com.inteligr8.maven.ban;

import java.util.Iterator;

import org.codehaus.plexus.util.CollectionUtils;
import org.eclipse.aether.artifact.Artifact;

public class BannedArtifactException extends RuntimeException {

    private static String buildMessage(Iterable<Artifact> artifactAncestralChain) {
        if (artifactAncestralChain == null)
            return null;
        Iterator<Artifact> iterator = artifactAncestralChain.iterator();
        if (!iterator.hasNext())
            return null;

        StringBuilder sb = new StringBuilder();
        sb.append(iterator.next());
        while (iterator.hasNext()) {
            sb.append(" from ");
            sb.append(iterator.next());
        }
        return sb.toString();
    }

    private static String buildMessage(Artifact descendant, Iterable<Artifact> ancestors) {
        StringBuilder sb = new StringBuilder();
        sb.append("Banned artifact: ").append(descendant);
        if (ancestors != null) {
            for (Artifact ancestor : ancestors) {
                sb.append(" from ");
                sb.append(ancestor);
            }
        }
        return sb.toString();
    }
    
    private Artifact artifact;
    private Iterable<Artifact> ancestors;

    public BannedArtifactException(Artifact artifact) {
        this(artifact, (Throwable) null);
    }

    public BannedArtifactException(Artifact artifact, Throwable cause) {
        this(artifact, (Iterable<Artifact>) null, cause);
    }

    public BannedArtifactException(Artifact artifact, Iterable<Artifact> ancestors) {
        this(artifact, ancestors, null);
    }

    public BannedArtifactException(Artifact artifact, Iterable<Artifact> ancestors, Throwable cause) {
        super("Banned artifact: " + buildMessage(artifact, ancestors), cause);
        this.artifact = artifact;
        this.ancestors = ancestors;
    }

    public BannedArtifactException(Iterable<Artifact> artifactAncestralChain) {
        this(artifactAncestralChain, null);
    }

    public BannedArtifactException(Iterable<Artifact> artifactAncestralChain, Throwable cause) {
        super("Banned artifact: " + buildMessage(artifactAncestralChain));
        Iterator<Artifact> iterator = artifactAncestralChain.iterator();
        if (!iterator.hasNext())
            throw new IllegalArgumentException("Artifact ancestral chain must contain at least one artifact");
        this.artifact = iterator.next();
        this.ancestors = CollectionUtils.iteratorToList(iterator);
    }

    public Artifact getArtifact() {
        return this.artifact;
    }

    public Iterable<Artifact> getAncestors() {
        return this.ancestors;
    }
    
}
+39 −0
Original line number Diff line number Diff line
package com.inteligr8.maven.ban;

import java.util.Collection;
import java.util.Collections;

import org.codehaus.plexus.util.CollectionUtils;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.DependencyNode;

public class BannedDependencyException extends BannedArtifactException {

    static Iterable<Artifact> toArtifacts(Collection<DependencyNode> nodes) {
        return CollectionUtils.iteratorToList(nodes.stream().map(DependencyNode::getArtifact).iterator());
    }
    
    private DependencyNode node;
    private Collection<DependencyNode> ancestors;

    public BannedDependencyException(DependencyNode node, Collection<DependencyNode> ancestors) {
        super(node.getArtifact(), toArtifacts(ancestors));
        this.node = node;
        this.ancestors = ancestors;
    }
    
    public BannedDependencyException(DependencyNode node, Collection<DependencyNode> ancestors, Throwable cause) {
        super(node.getArtifact(), toArtifacts(ancestors), cause);
        this.node = node;
        this.ancestors = ancestors;
    }

    public DependencyNode getNode() {
        return this.node;
    }

    public Iterable<DependencyNode> getAncestorNodes() {
        return Collections.unmodifiableCollection(this.ancestors);
    }
    
}
+42 −0
Original line number Diff line number Diff line
package com.inteligr8.maven.ban;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.maven.model.Plugin;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.DependencyNode;

public class BannedPluginException extends BannedArtifactException {

    private static Artifact toArtifact(Plugin plugin) {
        return new DefaultArtifact(plugin.getGroupId(), plugin.getArtifactId(), null, "jar", plugin.getVersion());
    }

    private static Collection<Artifact> toDescendants(DependencyNode descendant, Iterable<DependencyNode> ancestors) {
        List<Artifact> list = new LinkedList<Artifact>();
        list.add(descendant.getArtifact());
        for (DependencyNode ancestor : ancestors)
            list.add(ancestor.getArtifact());
        return list;
    }
    
    private Plugin plugin;

    public BannedPluginException(Plugin plugin, Collection<DependencyNode> descendants) {
        super(toArtifact(plugin), BannedDependencyException.toArtifacts(descendants));
        this.plugin = plugin;
    }
    
    public BannedPluginException(Plugin plugin, BannedDependencyException bde) {
        super(toArtifact(plugin), toDescendants(bde.getNode(), bde.getAncestorNodes()), bde);
        this.plugin = plugin;
    }

    public Plugin getPlugin() {
        return this.plugin;
    }
    
}