diff --git a/README.md b/README.md index 2b45ff8..96dc15a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Artifact Ban Maven Plugin -This is a maven plugin that allows for developers and organizations to ban Maven artifacts. We are keenly aware of the capability in the `maven-enforcer-plugin`. Instead of simply generating an error when a banned artifact is referenced, this plugin prevents the artifact from being downloaded as well. This is crucial within certain organizations with strict security scans that crawl the Maven cache. +This is a maven plugin that allows developers and organizations to ban Maven artifacts. We are keenly aware of the capability in the `maven-enforcer-plugin`. Instead of simply generating an error when a banned artifact is referenced, this plugin prevents the artifact from being downloaded as well. This is crucial within certain organizations with strict security scans that crawl the Maven cache. ## Extension @@ -33,6 +33,8 @@ When using as an extension, it will enforce a ban on the configured dependencies The `extensions` elements is critical. Without it, the plugin does nothing as far as banning artifacts/dependencies. With it, the plugin is able to not only detect banned artifacts, but do it before they are downloaded. This keeps libraries from even reaching your local Maven repository cache. +A module project will inherit the `extensions` setting from its parent project. A project with the extension (whether declared or inherited) will inherit the plugin configurations of its ancestral projects. + ### Configuration | Element | Description | @@ -45,7 +47,7 @@ The `extensions` elements is critical. Without it, the plugin does nothing as f When specifying `artifact`, you can use any of the following formats. The example is of this library; just replace the values to match the artifacts you want to ban. -- Exact artifact/version: `com.inteligr8:ban-maven-plugin:1.0.0` +- Exact artifact/version: `com.inteligr8:ban-maven-plugin:[1.0.0]` - Exact artifact; all versions: `com.inteligr8:ban-maven-plugin` - Exact artifact; version range: `com.inteligr8:ban-maven-plugin:[,1.4.0)` - All artifacts/versions in group: `org.springframework:` @@ -72,15 +74,15 @@ It supports the use of regular expressions with `groupIdRegex` and `artifactIdRe ``` -If no `includes` are provided, then no artifacts will be banned. An *included* artifact is a banned artifact. An *excluded* artifact is not banned. It is the opposite of what you may think. If no `excludes` are provided, then no banned artifacts are granted an exception. +If no `includes` are provided, then no artifacts will be banned. An *included* artifact is a banned artifact. An *excluded* artifact is not banned. It may be the opposite of what you think. If no `excludes` are provided, then no banned artifacts are granted an exception. The `artifact` element supports the descriptive `groupId`/`artifactId`/`version` elements or the abbreviated colon-based notation. When using the colon-based notation, the group ID and artifact ID may be treated as `groupIdRegex` and `artifactIdRegex` (see below). If you only use acceptable `groupId` and `artifactId` characters (letters/numbers/dashes/underscores/dots), it will not. But if you include any other characters, like `\.` or `*`, then it will be treated as regex. How it is treated will impact the functionality of `purge-repo` goal, if you are using it. If `groupId` or `artifactId` or `version` are not provided, they are ignored in the matching process. So it will match all applicable artifacts and the constraint will be only for what was specified. This means that `:` will ban every artifact and all their versions. -If `groupId` and `groupIdRegex` are both provided, only `groupId` is used. The same is true for `artifactId` and `artifactIdRegex`. The `*Regex` element values use standard Java regular expression parsing. If using regular expressions, remember to escape the dots (`\.`) in group IDs. If you do use `groupIdRegex` or use regular expressions in the colon-notation, the matching artifacts will not be purged using the `purge-repo` goal. So if you intend to use that goal, group ID regular expression matching needs be avoided. +If `groupId` and `groupIdRegex` are both provided, only `groupId` is used. The same is true for `artifactId` and `artifactIdRegex`. The `*Regex` element values use standard Java regular expression parsing. If using regular expressions, remember to escape the dots (`\.`) in group IDs. If you do use `groupIdRegex` or use regular expressions in the group part of the colon-notation, the matching artifacts will not be purged using the `purge-repo` goal. So if you intend to use that goal, group ID regular expression matching needs be avoided. -The `version` element supports the standard Maven specification. You can match a specific version like `1.0.0`. Or you can match all versions before `1.2.17` like `[,1.2.17)`. You can match all future versions after `1.2.17` (inclusive) with `[1.2.17,)`. +The `version` element supports the standard [Maven version specification](https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html). You can match a specific version like `[1.0.0]`. Or you can match all versions before `1.2.17` like `[,1.2.17)`. You can match all future versions after `1.2.17` (inclusive) with `[1.2.17,)`. The bracket-less `1.0.0` is not the same as `[1.0.0]`. There is nothing stopping you from specifying two `artifact` elements with the exact same values. So you can ban multiple version ranges of the same artifact by using multiple `artifact` elements. @@ -142,7 +144,7 @@ This goal will purge the local Maven repository of banned artifacts. The most c mvn -Dban.file=ban-config.xml com.inteligr8:ban-maven-plugin:1.4.1:purge-repo clean ``` -This goal does **NOT support** `groupIdRegex` or blank `groupId` specifications. So any of those will be ignored not be purged/removed (if in `includes`). +This goal does **NOT support** `groupIdRegex` or blank `groupId` specifications. So any of those will be ignored and not be purged/removed (if in `includes`). #### Configuration @@ -160,7 +162,7 @@ The following additional elements/properties are supported: | -------- | ------------------- | ------- | ----------- | | `skip` | `ban.skip` | `false` | `true` to skip the purge. | | `dryRun` | `ban.dryRun` | `false` | `true` to not actually delete any files or directories. | -| `eager` | `ban.eager | `false` | `true` to delete non-artifact (e.g. `pom` and `_remote.repositories`) files. | +| `eager` | `ban.eager` | `false` | `true` to delete non-artifact (e.g. `pom` and `_remote.repositories`) files. | ## Usage diff --git a/pom.xml b/pom.xml index 98e9c75..10a8d8f 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.inteligr8 ban-maven-plugin - 1.4-SNAPSHOT + 1.5-SNAPSHOT maven-plugin Ban Artifacts Maven Plugin @@ -45,24 +45,26 @@ 1.8 3.9.11 + + + + + + commons-io + commons-io + 2.21.0 + + + - - org.apache.commons - commons-lang3 - 3.19.0 - - - commons-io - commons-io - 2.20.0 - org.apache.maven.shared file-management 3.1.0 + org.apache.maven maven-plugin-api @@ -178,28 +180,18 @@ ${basedir}/src/it ${project.build.directory}/it ${env.MAVEN_HOME} - true - true + 4 ${basedir} - - - run-its - - install - run - - - - run-it-1 + run-it run-it @@ -227,6 +219,30 @@ + + run-its + + + !run-it + + + + + + maven-invoker-plugin + + + run-its + + install + run + + + + + + + central-publish diff --git a/src/it/ban-log4j-all/invoker.properties b/src/it/ban-log4j-all/invoker.properties new file mode 100644 index 0000000..324b5fb --- /dev/null +++ b/src/it/ban-log4j-all/invoker.properties @@ -0,0 +1 @@ +invoker.buildResult=failure \ No newline at end of file diff --git a/src/it/ban-log4j-all/pom.xml b/src/it/ban-log4j-all/pom.xml index 2eff7a8..e410312 100644 --- a/src/it/ban-log4j-all/pom.xml +++ b/src/it/ban-log4j-all/pom.xml @@ -5,12 +5,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.inteligr8 - ban-maven-plugin-log4j-all + @pom.groupId@ + @pom.artifactId@-log4j-all @pom.version@ jar - Log4j Ban Plugin Tests + Ban Plugin Test: All Log4J @@ -23,12 +23,8 @@ - maven-compiler-plugin - 3.11.0 - - - ${project.groupId} - ban-maven-plugin + @pom.groupId@ + @pom.artifactId@ @pom.version@ true diff --git a/src/it/ban-log4j-old/pom.xml b/src/it/ban-log4j-old/pom.xml index d11cf20..c33cf78 100644 --- a/src/it/ban-log4j-old/pom.xml +++ b/src/it/ban-log4j-old/pom.xml @@ -5,12 +5,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.inteligr8 - ban-maven-plugin-log4j-old + @pom.groupId@ + @pom.artifactId@-log4j-old @pom.version@ jar - Log4j Ban Plugin Tests + Ban Plugin Test: Old Log4J @@ -23,8 +23,8 @@ - ${project.groupId} - ban-maven-plugin + @pom.groupId@ + @pom.artifactId@ @pom.version@ true diff --git a/src/it/ban-log4j-purge/pom.xml b/src/it/ban-log4j-purge/pom.xml index 5814e4d..ed65d43 100644 --- a/src/it/ban-log4j-purge/pom.xml +++ b/src/it/ban-log4j-purge/pom.xml @@ -5,12 +5,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.inteligr8 - ban-maven-plugin-log4j-old + @pom.groupId@ + @pom.artifactId@-log4j-purge @pom.version@ jar - Log4j Ban Plugin Tests + Ban Plugin Test: Purge Old Log4J @@ -23,12 +23,8 @@ - maven-compiler-plugin - 3.11.0 - - - ${project.groupId} - ban-maven-plugin + @pom.groupId@ + @pom.artifactId@ @pom.version@ true @@ -41,6 +37,7 @@ log4j:log.+:[1.2.17,) + true diff --git a/src/it/ban-module-ext/invoker.properties b/src/it/ban-module-ext/invoker.properties new file mode 100644 index 0000000..324b5fb --- /dev/null +++ b/src/it/ban-module-ext/invoker.properties @@ -0,0 +1 @@ +invoker.buildResult=failure \ No newline at end of file diff --git a/src/it/ban-module-ext/module/pom.xml b/src/it/ban-module-ext/module/pom.xml new file mode 100644 index 0000000..ee880fc --- /dev/null +++ b/src/it/ban-module-ext/module/pom.xml @@ -0,0 +1,31 @@ + + + + 4.0.0 + + + @pom.groupId@ + @pom.artifactId@-parent + @pom.version@ + ../ + + + @pom.artifactId@-module + jar + + Ban Plugin Test: Module + + + + + @pom.groupId@ + @pom.artifactId@ + @pom.version@ + true + + + + + diff --git a/src/it/ban-module-ext/pom.xml b/src/it/ban-module-ext/pom.xml new file mode 100644 index 0000000..5ec7edd --- /dev/null +++ b/src/it/ban-module-ext/pom.xml @@ -0,0 +1,42 @@ + + + + 4.0.0 + + @pom.groupId@ + @pom.artifactId@-parent + @pom.version@ + pom + + Ban Plugin Test: Parent + + + + log4j + log4j + 1.2.17 + + + + + + + @pom.groupId@ + @pom.artifactId@ + @pom.version@ + + + log4j:log.* + + + + + + + + module + + + diff --git a/src/it/ban-noext/pom.xml b/src/it/ban-noext/pom.xml new file mode 100644 index 0000000..5256461 --- /dev/null +++ b/src/it/ban-noext/pom.xml @@ -0,0 +1,25 @@ + + + + 4.0.0 + + @pom.groupId@ + @pom.artifactId@-noext + @pom.version@ + jar + + Ban Plugin Test: Not Extension + + + + + @pom.groupId@ + @pom.artifactId@ + @pom.version@ + + + + + diff --git a/src/it/ban-none/pom.xml b/src/it/ban-none/pom.xml index cc0fa6b..e10335c 100644 --- a/src/it/ban-none/pom.xml +++ b/src/it/ban-none/pom.xml @@ -5,22 +5,18 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.inteligr8 - ban-maven-plugin-unsued + @pom.groupId@ + @pom.artifactId@-none @pom.version@ jar - Log4j Ban Plugin Tests + Ban Plugin Test: Nothing - maven-compiler-plugin - 3.11.0 - - - ${project.groupId} - ban-maven-plugin + @pom.groupId@ + @pom.artifactId@ @pom.version@ true diff --git a/src/it/ban-parent-ext/invoker.properties b/src/it/ban-parent-ext/invoker.properties new file mode 100644 index 0000000..324b5fb --- /dev/null +++ b/src/it/ban-parent-ext/invoker.properties @@ -0,0 +1 @@ +invoker.buildResult=failure \ No newline at end of file diff --git a/src/it/ban-parent-ext/module/pom.xml b/src/it/ban-parent-ext/module/pom.xml new file mode 100644 index 0000000..d3b13b6 --- /dev/null +++ b/src/it/ban-parent-ext/module/pom.xml @@ -0,0 +1,28 @@ + + + + 4.0.0 + + + @pom.groupId@ + @pom.artifactId@-parent + @pom.version@ + ../ + + + @pom.artifactId@-module + jar + + Ban Plugin Test: Module + + + + log4j + log4j + 1.2.17 + + + + diff --git a/src/it/ban-parent-ext/pom.xml b/src/it/ban-parent-ext/pom.xml new file mode 100644 index 0000000..158f2ef --- /dev/null +++ b/src/it/ban-parent-ext/pom.xml @@ -0,0 +1,35 @@ + + + + 4.0.0 + + @pom.groupId@ + @pom.artifactId@-parent + @pom.version@ + pom + + Ban Plugin Test: Parent + + + + + @pom.groupId@ + @pom.artifactId@ + @pom.version@ + true + + + log4j:log.* + + + + + + + + module + + + diff --git a/src/it/ban-unused/pom.xml b/src/it/ban-unused/pom.xml index 705686e..d4fa19d 100644 --- a/src/it/ban-unused/pom.xml +++ b/src/it/ban-unused/pom.xml @@ -5,19 +5,15 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.inteligr8 - ban-maven-plugin-unsued + @pom.groupId@ + @pom.artifactId@-unsued @pom.version@ jar - Log4j Ban Plugin Tests + Ban Plugin Test: Unused - - maven-compiler-plugin - 3.11.0 - ${project.groupId} ban-maven-plugin diff --git a/src/main/java/com/inteligr8/maven/ban/AbstractBanConfiguration.java b/src/main/java/com/inteligr8/maven/ban/AbstractBanConfiguration.java index 6ff3a05..bce3e32 100644 --- a/src/main/java/com/inteligr8/maven/ban/AbstractBanConfiguration.java +++ b/src/main/java/com/inteligr8/maven/ban/AbstractBanConfiguration.java @@ -22,11 +22,11 @@ import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.lang3.StringUtils; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; @@ -94,34 +94,28 @@ public abstract class AbstractBanConfiguration implements BanConfiguration { this.versionRangeResolver = versionRangeResolver; } - public void init(Xpp3Dom rootDom) throws IOException, MojoFailureException { - this.init(rootDom, null); + public void amendProperties(Properties userProperties) throws IOException, MojoFailureException { + if (userProperties.containsKey("ban.file")) + this.processFileImport(this.trimToNull(userProperties.getProperty("ban.file"))); + if (userProperties.containsKey("ban.url")) + this.processUrlImport(this.trimToNull(userProperties.getProperty("ban.url"))); + if (userProperties.containsKey("ban.artifact")) + this.processArtifactImport(this.trimToNull(userProperties.getProperty("ban.artifact"))); } - public void init(Xpp3Dom rootDom, Properties userProperties) throws IOException, MojoFailureException { - if (userProperties != null) { - if (userProperties.containsKey("ban.file")) - this.processFileImport(StringUtils.trimToNull(userProperties.getProperty("ban.file"))); - if (userProperties.containsKey("ban.url")) - this.processUrlImport(StringUtils.trimToNull(userProperties.getProperty("ban.url"))); - if (userProperties.containsKey("ban.artifact")) - this.processArtifactImport(StringUtils.trimToNull(userProperties.getProperty("ban.artifact"))); - } - - if (rootDom != null) { - Xpp3Dom importDom = rootDom.getChild("import"); - if (importDom != null) - this.processImports(importDom); - this.processIncludesExcludes(rootDom); - } + public void amendConfigurationElement(Xpp3Dom rootDom) throws IOException, MojoFailureException { + Xpp3Dom importDom = rootDom.getChild("import"); + if (importDom != null) + this.processImports(importDom); + this.processIncludesExcludes(rootDom); } private void processImports(Xpp3Dom importDom) throws IOException, MojoFailureException { for (Xpp3Dom child : importDom.getChildren()) { if (child.getName().equals("file")) { - this.processFileImport(StringUtils.trimToNull(child.getValue())); + this.processFileImport(this.trimToNull(child.getValue())); } else if (child.getName().equals("url")) { - this.processUrlImport(StringUtils.trimToNull(child.getValue())); + this.processUrlImport(this.trimToNull(child.getValue())); } else if (child.getName().equals("artifact")) { this.processArtifactImport(child.getValue()); } else { @@ -136,7 +130,7 @@ public abstract class AbstractBanConfiguration implements BanConfiguration { } private BanConfigurationDownloader getUrlDownloader(String url) throws IOException, MojoFailureException { - url = StringUtils.trimToNull(url); + url = this.trimToNull(url); return new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, url); } @@ -221,9 +215,6 @@ public abstract class AbstractBanConfiguration implements BanConfiguration { Xpp3Dom excludesDom = rootDom.getChild("excludes"); if (excludesDom != null) this.excludeArtifacts.addAll(this.parseArtifacts(excludesDom)); - - this.logger.debug("Include artifacts: {}", this.includeArtifacts); - this.logger.debug("Exclude artifacts: {}", this.excludeArtifacts); } public List getIncludeArtifacts() { @@ -248,18 +239,18 @@ public abstract class AbstractBanConfiguration implements BanConfiguration { } if (this.notRegexPattern.matcher(matcher.group(1)).matches()) { - filter.setGroupId(StringUtils.trimToNull(matcher.group(1))); + filter.setGroupId(this.trimToNull(matcher.group(1))); } else { - filter.setGroupIdRegex(StringUtils.trimToNull(matcher.group(1))); + filter.setGroupIdRegex(this.trimToNull(matcher.group(1))); } if (this.notRegexPattern.matcher(matcher.group(2)).matches()) { - filter.setArtifactId(StringUtils.trimToNull(matcher.group(2))); + filter.setArtifactId(this.trimToNull(matcher.group(2))); } else { - filter.setArtifactIdRegex(StringUtils.trimToNull(matcher.group(2))); + filter.setArtifactIdRegex(this.trimToNull(matcher.group(2))); } - versionSpec = StringUtils.trimToNull(matcher.group(4)); + versionSpec = this.trimToNull(matcher.group(4)); } else { filter.setGroupId(this.getChildValue(artifactDom, "groupId")); filter.setGroupIdRegex(this.getChildValue(artifactDom, "groupIdRegex")); @@ -287,7 +278,21 @@ public abstract class AbstractBanConfiguration implements BanConfiguration { private String getChildValue(Xpp3Dom dom, String child) { Xpp3Dom childDom = dom.getChild(child); - return childDom == null ? null : StringUtils.trimToNull(childDom.getValue()); + return childDom == null ? null : this.trimToNull(childDom.getValue()); + } + + /** + * Re-implemented here just to avoid unnecessary dependencies, since this + * plugin is about banning dependencies. + * + * @param value A string value; may be null + * @return A trim, but if empty, return `null`; `null` returns `null`. + */ + protected String trimToNull(String value) { + if (value == null) + return null; + value = StringUtils.trim(value); + return value.isEmpty() ? null : value; } } diff --git a/src/main/java/com/inteligr8/maven/ban/BanConfigurationDownloader.java b/src/main/java/com/inteligr8/maven/ban/BanConfigurationDownloader.java index bb5a540..fa8e7bc 100644 --- a/src/main/java/com/inteligr8/maven/ban/BanConfigurationDownloader.java +++ b/src/main/java/com/inteligr8/maven/ban/BanConfigurationDownloader.java @@ -54,10 +54,13 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration { try { Xpp3Dom rootDom = this.load(new URL(url)); - this.init(rootDom); + this.amendConfigurationElement(rootDom); } catch (XmlPullParserException xppe) { throw new MojoFailureException(xppe.getMessage(), xppe); } + + this.logger.debug("Include artifacts: {}", this.includeArtifacts); + this.logger.debug("Exclude artifacts: {}", this.excludeArtifacts); } /** @@ -75,10 +78,13 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration { try { Xpp3Dom rootDom = this.load(file); - this.init(rootDom); + this.amendConfigurationElement(rootDom); } catch (XmlPullParserException xppe) { throw new MojoFailureException(xppe.getMessage(), xppe); } + + this.logger.debug("Include artifacts: {}", this.includeArtifacts); + this.logger.debug("Exclude artifacts: {}", this.excludeArtifacts); } /** diff --git a/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java b/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java index 8fdc7fd..8a83cac 100644 --- a/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java +++ b/src/main/java/com/inteligr8/maven/ban/BanDependencyFilter.java @@ -20,7 +20,6 @@ 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; diff --git a/src/main/java/com/inteligr8/maven/ban/BanExtension.java b/src/main/java/com/inteligr8/maven/ban/BanExtension.java index f7d1128..86f86de 100644 --- a/src/main/java/com/inteligr8/maven/ban/BanExtension.java +++ b/src/main/java/com/inteligr8/maven/ban/BanExtension.java @@ -17,6 +17,7 @@ package com.inteligr8.maven.ban; import java.io.IOException; import java.util.LinkedList; import java.util.List; +import java.util.Stack; import javax.inject.Inject; import javax.inject.Named; @@ -81,23 +82,46 @@ public class BanExtension extends AbstractMavenLifecycleParticipant { @Parameter(name = "excludes") private List excludes; + /** + * This does not execute unless the plugin is declared an extension at some + * layer in the reactor. That could be the current project, an ancestor, + * or a module. It will only execute once and in the context where + * `session.getCurrentProject()` is the POM `mvn` was executed on. + */ @Override public void afterProjectsRead(MavenSession session) throws MavenExecutionException { - BanConfiguration config = this.getConfiguration(session); - if (config == null) - return; + List extensionProjects = new LinkedList<>(); + + MavenProject project = session.getCurrentProject(); + Plugin plugin = this.getExtensionPlugin(project); + if (plugin != null) + extensionProjects.add(project); + + for (MavenProject childProject : session.getProjectDependencyGraph().getDownstreamProjects(project, true)) { + plugin = this.getExtensionPlugin(childProject); + if (plugin != null) + extensionProjects.add(childProject); + } + + this.logger.debug("Discovered {} projects with the '{}' plugin as an extension: {}", extensionProjects.size(), THIS_PLUGIN_KEY, extensionProjects); + + for (MavenProject extensionProject : extensionProjects) { + this.handleProject(session, extensionProject); + } + } + + protected void handleProject(MavenSession session, MavenProject project) throws MavenExecutionException { + BanConfiguration config = this.getConfiguration(session, project); BanDependencyFilter depFilter = new BanDependencyFilter( config.getIncludeArtifacts(), config.getExcludeArtifacts()); //depFilter.setFailFast(true); boolean fail = false; - - MavenProject project = session.getCurrentProject(); try { for (Plugin plugin : project.getBuildPlugins()) { - this.logger.debug("Evaluating plugin dependencies: {}", plugin); + this.logger.debug("Evaluating plugin dependencies: {}: {}", project, plugin); Artifact artifact = new DefaultArtifact(plugin.getId()); DependencyNode depNodeRoot = this.pluginDepResolver.resolve(plugin, artifact, depFilter, project.getRemotePluginRepositories(), session.getRepositorySession()); @@ -132,21 +156,39 @@ public class BanExtension extends AbstractMavenLifecycleParticipant { throw new MavenExecutionException("Banned dependencies were detected; see warnings: ", project.getFile()); } - private BanConfiguration getConfiguration(MavenSession session) throws MavenExecutionException { - MavenProject project = session.getCurrentProject(); + private Plugin getExtensionPlugin(MavenProject project) { Plugin plugin = project.getPlugin(THIS_PLUGIN_KEY); - if (plugin == null) - throw new MavenExecutionException("The plugin is executing but it cannot be found", project.getFile()); - - if (!plugin.isExtensions()) { - this.logger.warn("The '{}' plugin must be defined with 'true'; ignoring plugin", plugin.getId()); + if (plugin == null) { + this.logger.debug("Although a module declares the '{}' plugin, the ancestral project '{}' does not", THIS_PLUGIN_KEY, project.getId()); + return null; + } else if (!plugin.isExtensions()) { + this.logger.debug("Although an ancester or module declares the '{}' plugin as an extension, this project '{}' does not", plugin.getId(), project.getId()); return null; } else { - try { - return new BanPluginConfigurationParser(session, this.artifactResolver, this.versionRangeResolver, plugin); - } catch (IOException | MojoFailureException e) { - throw new MavenExecutionException(e.getMessage(), project.getFile()); + return plugin; + } + } + + private BanConfiguration getConfiguration(MavenSession session, MavenProject project) throws MavenExecutionException { + Plugin plugin = project.getPlugin(THIS_PLUGIN_KEY); + this.logger.debug("Found plugin definition in project: {}", project.getId()); + Stack pluginDefinitions = new Stack<>(); + pluginDefinitions.push(plugin); + + MavenProject parent = project.getParent(); + while (parent != null) { + plugin = parent.getPlugin(THIS_PLUGIN_KEY); + if (plugin != null) { + this.logger.debug("Found plugin definition in project: {}", parent.getId()); + pluginDefinitions.push(plugin); } + parent = parent.getParent(); + } + + try { + return new BanPluginConfigurationParser(session, this.artifactResolver, this.versionRangeResolver, pluginDefinitions); + } catch (IOException | MojoFailureException e) { + throw new MavenExecutionException(e.getMessage(), project.getFile()); } } diff --git a/src/main/java/com/inteligr8/maven/ban/BanPluginConfigurationParser.java b/src/main/java/com/inteligr8/maven/ban/BanPluginConfigurationParser.java index 7033941..bbc0200 100644 --- a/src/main/java/com/inteligr8/maven/ban/BanPluginConfigurationParser.java +++ b/src/main/java/com/inteligr8/maven/ban/BanPluginConfigurationParser.java @@ -15,6 +15,7 @@ package com.inteligr8.maven.ban; import java.io.IOException; +import java.util.Stack; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; @@ -22,6 +23,8 @@ import org.apache.maven.plugin.MojoFailureException; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.eclipse.aether.impl.ArtifactResolver; import org.eclipse.aether.impl.VersionRangeResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class parses the POM plugin configuration block for this plugin. The @@ -30,11 +33,34 @@ import org.eclipse.aether.impl.VersionRangeResolver; */ public class BanPluginConfigurationParser extends AbstractBanConfiguration { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + public BanPluginConfigurationParser(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, Plugin plugin) throws IOException, MojoFailureException { super(session, artifactResolver, versionRangeResolver); - Xpp3Dom rootDom = plugin == null ? null : (Xpp3Dom) plugin.getConfiguration(); - this.init(rootDom, session.getUserProperties()); + if (session.getUserProperties() != null) + this.amendProperties(session.getUserProperties()); + if (plugin != null) { + Xpp3Dom rootDom = (Xpp3Dom) plugin.getConfiguration(); + if (rootDom != null) + this.amendConfigurationElement(rootDom); + } + } + + public BanPluginConfigurationParser(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, Stack plugins) throws IOException, MojoFailureException { + super(session, artifactResolver, versionRangeResolver); + + if (session.getUserProperties() != null) + this.amendProperties(session.getUserProperties()); + while (!plugins.isEmpty()) { + Plugin plugin = plugins.pop(); + Xpp3Dom rootDom = (Xpp3Dom) plugin.getConfiguration(); + if (rootDom != null) + this.amendConfigurationElement(rootDom); + } + + this.logger.debug("Include artifacts: {}", this.includeArtifacts); + this.logger.debug("Exclude artifacts: {}", this.excludeArtifacts); } } diff --git a/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java b/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java index 311ff60..ab21bc6 100644 --- a/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java +++ b/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java @@ -84,7 +84,7 @@ public class PurgeRepoMojo extends AbstractMojo { if (this.skip) { this.getLog().debug("Skipping purge of banned artifacts"); } else { - this.getLog().info("Purging banned artifacts from local repository: " + this.session.getLocalRepository().getBasedir()); + this.getLog().info("Purging banned artifacts from local repository: " + this.session.getRepositorySession().getLocalRepository().getBasedir()); try { this.purge(); @@ -282,7 +282,7 @@ public class PurgeRepoMojo extends AbstractMojo { } private Path getRepositoryPath() { - return new File(this.session.getLocalRepository().getBasedir()).toPath(); + return this.session.getRepositorySession().getLocalRepository().getBasedir().toPath(); }