From 46e19caa0843cc08fbe007852d691925d9407eea Mon Sep 17 00:00:00 2001 From: "Brian M. Long" Date: Thu, 5 Mar 2026 14:41:56 -0500 Subject: [PATCH] added banned exceptions; using for better output --- .../maven/ban/BanDependencyFilter.java | 3 +- .../com/inteligr8/maven/ban/BanExtension.java | 14 +++- .../maven/ban/BannedArtifactException.java | 80 +++++++++++++++++++ .../maven/ban/BannedDependencyException.java | 39 +++++++++ .../maven/ban/BannedPluginException.java | 42 ++++++++++ 5 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/inteligr8/maven/ban/BannedArtifactException.java create mode 100644 src/main/java/com/inteligr8/maven/ban/BannedDependencyException.java create mode 100644 src/main/java/com/inteligr8/maven/ban/BannedPluginException.java diff --git a/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java b/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java index 3818ef0..a39c3e4 100644 --- a/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java +++ b/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java @@ -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; } diff --git a/src/main/java/com/inteligr8/maven/ban/BanExtension.java b/src/main/java/com/inteligr8/maven/ban/BanExtension.java index 32dbed3..8b30524 100644 --- a/src/main/java/com/inteligr8/maven/ban/BanExtension.java +++ b/src/main/java/com/inteligr8/maven/ban/BanExtension.java @@ -99,11 +99,17 @@ public class BanExtension extends AbstractMavenLifecycleParticipant { this.logger.debug("Evaluating plugin dependencies: {}", plugin); Artifact artifact = new DefaultArtifact(plugin.getId()); - DependencyNode depNodeRoot = this.pluginDepResolver.resolve(plugin, artifact, depFilter, project.getRemotePluginRepositories(), session.getRepositorySession()); - List bannedDependencies = this.crawlDependencyTree(depNodeRoot, depFilter); - if (!bannedDependencies.isEmpty()) - throw new MavenExecutionException("Banned dependencies were detected in plugin '" + plugin + "': " + bannedDependencies, project.getFile()); + try { + DependencyNode depNodeRoot = this.pluginDepResolver.resolve(plugin, artifact, depFilter, project.getRemotePluginRepositories(), session.getRepositorySession()); + List 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); } diff --git a/src/main/java/com/inteligr8/maven/ban/BannedArtifactException.java b/src/main/java/com/inteligr8/maven/ban/BannedArtifactException.java new file mode 100644 index 0000000..15b1af4 --- /dev/null +++ b/src/main/java/com/inteligr8/maven/ban/BannedArtifactException.java @@ -0,0 +1,80 @@ +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 artifactAncestralChain) { + if (artifactAncestralChain == null) + return null; + Iterator 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 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 ancestors; + + public BannedArtifactException(Artifact artifact) { + this(artifact, (Throwable) null); + } + + public BannedArtifactException(Artifact artifact, Throwable cause) { + this(artifact, (Iterable) null, cause); + } + + public BannedArtifactException(Artifact artifact, Iterable ancestors) { + this(artifact, ancestors, null); + } + + public BannedArtifactException(Artifact artifact, Iterable ancestors, Throwable cause) { + super("Banned artifact: " + buildMessage(artifact, ancestors), cause); + this.artifact = artifact; + this.ancestors = ancestors; + } + + public BannedArtifactException(Iterable artifactAncestralChain) { + this(artifactAncestralChain, null); + } + + public BannedArtifactException(Iterable artifactAncestralChain, Throwable cause) { + super("Banned artifact: " + buildMessage(artifactAncestralChain)); + Iterator 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 getAncestors() { + return this.ancestors; + } + +} diff --git a/src/main/java/com/inteligr8/maven/ban/BannedDependencyException.java b/src/main/java/com/inteligr8/maven/ban/BannedDependencyException.java new file mode 100644 index 0000000..10edf42 --- /dev/null +++ b/src/main/java/com/inteligr8/maven/ban/BannedDependencyException.java @@ -0,0 +1,39 @@ +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 toArtifacts(Collection nodes) { + return CollectionUtils.iteratorToList(nodes.stream().map(DependencyNode::getArtifact).iterator()); + } + + private DependencyNode node; + private Collection ancestors; + + public BannedDependencyException(DependencyNode node, Collection ancestors) { + super(node.getArtifact(), toArtifacts(ancestors)); + this.node = node; + this.ancestors = ancestors; + } + + public BannedDependencyException(DependencyNode node, Collection ancestors, Throwable cause) { + super(node.getArtifact(), toArtifacts(ancestors), cause); + this.node = node; + this.ancestors = ancestors; + } + + public DependencyNode getNode() { + return this.node; + } + + public Iterable getAncestorNodes() { + return Collections.unmodifiableCollection(this.ancestors); + } + +} diff --git a/src/main/java/com/inteligr8/maven/ban/BannedPluginException.java b/src/main/java/com/inteligr8/maven/ban/BannedPluginException.java new file mode 100644 index 0000000..0c2abd4 --- /dev/null +++ b/src/main/java/com/inteligr8/maven/ban/BannedPluginException.java @@ -0,0 +1,42 @@ +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 toDescendants(DependencyNode descendant, Iterable ancestors) { + List list = new LinkedList(); + list.add(descendant.getArtifact()); + for (DependencyNode ancestor : ancestors) + list.add(ancestor.getArtifact()); + return list; + } + + private Plugin plugin; + + public BannedPluginException(Plugin plugin, Collection 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; + } + +}