docs and tidy up

This commit is contained in:
2025-07-09 10:05:28 -04:00
parent ad4b06fc4f
commit bef065e964
7 changed files with 118 additions and 21 deletions

View File

@@ -43,6 +43,37 @@ import org.slf4j.LoggerFactory;
import com.inteligr8.maven.model.ArtifactFilter;
/**
* This class handles the parsing of the configuration specification supported
* by this plugin. It does not implement the banning execution; just the
* collection of configurations to decide what artifacts should be banned.
*
* The specification is as follows:
*
* ```xml
* <configuration>
* <import>
* <file>relative/path/file.xml</file>
* <url>https://host.domain/path/file.xml</url>
* <artifact>domain.host:artifact-id:[1.0,)</artifact>
* </import>
* <includes>
* <artifact>log4j:log4j</artifact>
* <artifact>org.apache.logging:log4j-impl:[,2.16.1)</artifact>
* </includes>
* <excludes>
* <artifact>...</artifact>
* </excludes>
* </configuration>
* ```
*
* The imports are recursively processed under the same specification. The
* included artifacts are the ones that are banned. Any artifact matching both
* includes and excludes will be excluded and therefore NOT banned.
*
* The `groupId` and `artifactId` of the standard shorthand Maven `artifact`
* nomenclature may use regular expressions.
*/
public abstract class AbstractBanConfiguration implements BanConfiguration {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

View File

@@ -31,10 +31,24 @@ import org.eclipse.aether.impl.VersionRangeResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class parses the a configuration file that conforms with the
* specification defined by this plugin.
*/
public class BanConfigurationDownloader extends AbstractBanConfiguration {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* This constructs the downloader based on an accessible URL.
*
* @param session A Maven session object.
* @param artifactResolver A Maven Aether artifact resolver service.
* @param versionRangeResolver A Maven Aether version range resolver service.
* @param url The URL of a ban configuration file.
* @throws IOException The file could not be downloaded for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting or unexpected response.
*/
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, String url) throws IOException, MojoFailureException {
super(session, artifactResolver, versionRangeResolver);
@@ -45,7 +59,17 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
throw new MojoFailureException(xppe.getMessage(), xppe);
}
}
/**
* This constructs the downloader based on a file object.
*
* @param session A Maven session object.
* @param artifactResolver A Maven Aether artifact resolver service.
* @param versionRangeResolver A Maven Aether version range resolver service.
* @param file A file, typically local, though technically may be remote.
* @throws IOException The file could not be read for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting.
*/
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, File file) throws IOException, MojoFailureException {
super(session, artifactResolver, versionRangeResolver);
@@ -57,6 +81,14 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
}
}
/**
* This method loads the configuration from the specified URL.
*
* @param url A URL of a ban configuration file.
* @return A Maven Plexus XML DOM object.
* @throws IOException The file could not be downloaded for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting or unexpected response.
*/
private Xpp3Dom load(URL url) throws IOException, XmlPullParserException {
InputStream istream = url.openStream();
BufferedInputStream bistream = new BufferedInputStream(istream, 16384);
@@ -67,7 +99,15 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
bistream.close();
}
}
/**
* This method loads the configuration from the specified file.
*
* @param file A file, typically local, though technically may be remote.
* @return A Maven Plexus XML DOM object.
* @throws IOException The file could not be read for any number of reasons.
* @throws MojoFailureException The file could not be parsed due to bad formatting.
*/
private Xpp3Dom load(File file) throws IOException, XmlPullParserException {
FileInputStream fistream = new FileInputStream(file);
BufferedInputStream bistream = new BufferedInputStream(fistream, 16384);

View File

@@ -28,6 +28,18 @@ import org.slf4j.LoggerFactory;
import com.inteligr8.maven.model.ArtifactFilter;
/**
* This class implements the banned artifact detection logic.
*
* It will recursively scan every dependency and plugin dependency to find any
* artifacts that reference a banned artifact and its banned versions. If one
* is found, the filter will fail and stop the build immediately. It will
* prevent the banned artifact from being downloaded.
*
* The complicated part is that when multiple versions of the same artifact
* show up in the dependency tree, only one may be selected. So this filter
* will ignore the versions that are not selected.
*/
public class BanDependencyFilter implements DependencyFilter {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

View File

@@ -87,7 +87,9 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
if (config == null)
return;
BanDependencyFilter depFilter = new BanDependencyFilter(config.getIncludeArtifacts(), config.getExcludeArtifacts());
BanDependencyFilter depFilter = new BanDependencyFilter(
config.getIncludeArtifacts(),
config.getExcludeArtifacts());
depFilter.setFailFast(true);
MavenProject project = session.getCurrentProject();
@@ -132,7 +134,7 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
return null;
} else {
try {
return new BanConfigurationParser(session, this.artifactResolver, this.versionRangeResolver, plugin);
return new BanPluginConfigurationParser(session, this.artifactResolver, this.versionRangeResolver, plugin);
} catch (IOException | MojoFailureException e) {
throw new MavenExecutionException(e.getMessage(), project.getFile());
}

View File

@@ -23,9 +23,14 @@ import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.aether.impl.ArtifactResolver;
import org.eclipse.aether.impl.VersionRangeResolver;
public class BanConfigurationParser extends AbstractBanConfiguration {
/**
* This class parses the POM plugin configuration block for this plugin. The
* specification for that configuration block is shared with the one found in
* the ban configuration file.
*/
public class BanPluginConfigurationParser extends AbstractBanConfiguration {
public BanConfigurationParser(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, Plugin plugin) throws IOException, MojoFailureException {
public BanPluginConfigurationParser(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, Plugin plugin) throws IOException, MojoFailureException {
super(session, artifactResolver, versionRangeResolver);
Xpp3Dom rootDom = (Xpp3Dom) plugin.getConfiguration();

View File

@@ -88,7 +88,7 @@ public class PurgeRepoMojo extends AbstractMojo {
private void purge() throws MojoFailureException, IOException {
List<Path> includePaths = new LinkedList<>();
BanConfigurationParser config = this.getConfiguration();
BanPluginConfigurationParser config = this.getConfiguration();
for (ArtifactFilter afilter : config.getIncludeArtifacts()) {
if (afilter.getGroupId() == null) {
@@ -172,12 +172,12 @@ public class PurgeRepoMojo extends AbstractMojo {
}
}
private BanConfigurationParser getConfiguration() throws MojoFailureException, IOException {
private BanPluginConfigurationParser getConfiguration() throws MojoFailureException, IOException {
MavenProject project = this.session.getCurrentProject();
Plugin plugin = project.getPlugin(BanExtension.THIS_PLUGIN_KEY);
if (plugin == null)
throw new MojoFailureException("The plugin is executing but it cannot be found");
return new BanConfigurationParser(this.session, this.artifactResolver, this.versionRangeResolver, plugin);
return new BanPluginConfigurationParser(this.session, this.artifactResolver, this.versionRangeResolver, plugin);
}
private Path resolveGroupPath(ArtifactFilter afilter) {