Commit 2bbd70c3 authored by Brian Long's avatar Brian Long
Browse files

v1.5.x; added parent/module support

parent fd5e91fe
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line

# 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
</artifact>
```

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 `<includes><artifact>:</artifact></includes>` 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

+39 −23
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@

	<groupId>com.inteligr8</groupId>
	<artifactId>ban-maven-plugin</artifactId>
	<version>1.4-SNAPSHOT</version>
	<version>1.5-SNAPSHOT</version>
	<packaging>maven-plugin</packaging>

	<name>Ban Artifacts Maven Plugin</name>
@@ -46,23 +46,25 @@
		<maven.version>3.9.11</maven.version>
	</properties>
	
	<dependencyManagement>
		<dependencies>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.19.0</version>
		</dependency>
			<!-- v2.11 included by file-management v3.1 --> 
			<dependency>
				<groupId>commons-io</groupId>
				<artifactId>commons-io</artifactId>
			<version>2.20.0</version>
				<version>2.21.0</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.apache.maven.shared</groupId>
			<artifactId>file-management</artifactId>
			<!-- v3.2+ appears to require maven v4+ -->
			<version>3.1.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
@@ -178,12 +180,27 @@
					<projectsDirectory>${basedir}/src/it</projectsDirectory>
					<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
					<mavenHome>${env.MAVEN_HOME}</mavenHome>
					<debug>true</debug>
					<ignoreFailures>true</ignoreFailures>
					<parallelThreads>4</parallelThreads>
					<properties>
						<project.main.basedir>${basedir}</project.main.basedir>
					</properties>
				</configuration>
			</plugin>
		</plugins>
	</build>
	
	<profiles>
		<profile>
			<id>run-it</id>
			<activation>
				<property>
					<name>run-it</name>
				</property>
			</activation>
			<build>
				<plugins>
					<plugin>
						<artifactId>maven-invoker-plugin</artifactId>
						<executions>
							<execution>
								<id>run-its</id>
@@ -191,18 +208,22 @@
									<goal>install</goal>
									<goal>run</goal>
								</goals>
								<configuration>
									<pomIncludes>
										<pomInclude>${run-it}/pom.xml</pomInclude>
									</pomIncludes>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
	
	<profiles>
		</profile>
		<profile>
			<id>run-it-1</id>
			<id>run-its</id>
			<activation>
				<property>
					<name>run-it</name>
					<name>!run-it</name>
				</property>
			</activation>
			<build>
@@ -216,11 +237,6 @@
									<goal>install</goal>
									<goal>run</goal>
								</goals>
								<configuration>
									<pomIncludes>
										<pomInclude>${run-it}/pom.xml</pomInclude>
									</pomIncludes>
								</configuration>
							</execution>
						</executions>
					</plugin>
+1 −0
Original line number Diff line number Diff line
invoker.buildResult=failure
 No newline at end of file
+5 −9
Original line number Diff line number Diff line
@@ -5,12 +5,12 @@
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.inteligr8</groupId>
	<artifactId>ban-maven-plugin-log4j-all</artifactId>
	<groupId>@pom.groupId@</groupId>
	<artifactId>@pom.artifactId@-log4j-all</artifactId>
	<version>@pom.version@</version>
	<packaging>jar</packaging>

	<name>Log4j Ban Plugin Tests</name>
	<name>Ban Plugin Test: All Log4J</name>
	
	<dependencies>
		<dependency>
@@ -23,12 +23,8 @@
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.11.0</version>
			</plugin>
			<plugin>
				<groupId>${project.groupId}</groupId>
				<artifactId>ban-maven-plugin</artifactId>
				<groupId>@pom.groupId@</groupId>
				<artifactId>@pom.artifactId@</artifactId>
				<version>@pom.version@</version>
				<extensions>true</extensions>
				<configuration>
+5 −5
Original line number Diff line number Diff line
@@ -5,12 +5,12 @@
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.inteligr8</groupId>
	<artifactId>ban-maven-plugin-log4j-old</artifactId>
	<groupId>@pom.groupId@</groupId>
	<artifactId>@pom.artifactId@-log4j-old</artifactId>
	<version>@pom.version@</version>
	<packaging>jar</packaging>

	<name>Log4j Ban Plugin Tests</name>
	<name>Ban Plugin Test: Old Log4J</name>
	
	<dependencies>
		<dependency>
@@ -23,8 +23,8 @@
	<build>
		<plugins>
			<plugin>
				<groupId>${project.groupId}</groupId>
				<artifactId>ban-maven-plugin</artifactId>
				<groupId>@pom.groupId@</groupId>
				<artifactId>@pom.artifactId@</artifactId>
				<version>@pom.version@</version>
				<extensions>true</extensions>
				<configuration>
Loading