Compare commits
45 Commits
Author | SHA1 | Date | |
---|---|---|---|
1577bbf2d8 | |||
988326a8d5 | |||
ad4b06fc4f | |||
94825b9417 | |||
80068ef209 | |||
a7480c3d85 | |||
034b319172 | |||
65009df10b | |||
8df2d5bd03 | |||
0edac4fc37 | |||
f837e5d1c0 | |||
78e34d85e9 | |||
6e2a701c0d | |||
08498d4224 | |||
1c7997d85c | |||
9116d4bfd9 | |||
9874ed9543 | |||
eeb7a84698 | |||
ca8913e558 | |||
4c1c4d412d | |||
fc5e0c6bd1 | |||
2a1523012b | |||
d41d73fa1c | |||
a9aa47d412 | |||
47fd07247d | |||
cfb13f3170 | |||
75f8175a1c | |||
f2c93eec50 | |||
58eb0ec013 | |||
dec181aa71 | |||
51f5d6d0a3 | |||
24d69c3715 | |||
fe10e53fb4 | |||
b6c7323203 | |||
9644bbe154 | |||
9a0b7207c0 | |||
118b40f3f9 | |||
db3061a3a6 | |||
fee4032208 | |||
f991975923 | |||
e4f14a81da | |||
b4426761df | |||
f749926f92 | |||
8c3ce9f069 | |||
3c73bcb83d |
118
README.md
118
README.md
@@ -5,6 +5,10 @@ This is a maven plugin that allows for developers and organizations to ban Maven
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
### Prevent Banned Artifacts
|
||||||
|
|
||||||
|
Here is a pseudo-code example of all the options this plugin provides.
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
<project>
|
<project>
|
||||||
...
|
...
|
||||||
@@ -18,6 +22,11 @@ This is a maven plugin that allows for developers and organizations to ban Maven
|
|||||||
<version>...</version>
|
<version>...</version>
|
||||||
<extensions>true</extensions>
|
<extensions>true</extensions>
|
||||||
<configuration>
|
<configuration>
|
||||||
|
<import>
|
||||||
|
<file>project-file.xml</file>
|
||||||
|
<url>https://host:port/path/file.xml</url>
|
||||||
|
<artifact>groupId:artifactId:version</artifact>
|
||||||
|
</import>
|
||||||
<includes>
|
<includes>
|
||||||
<artifact>
|
<artifact>
|
||||||
<groupId>...<groupId>
|
<groupId>...<groupId>
|
||||||
@@ -38,7 +47,6 @@ This is a maven plugin that allows for developers and organizations to ban Maven
|
|||||||
....
|
....
|
||||||
</artifact>
|
</artifact>
|
||||||
</excludes>
|
</excludes>
|
||||||
<import>https://domain:port/path/file</import>
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
...
|
...
|
||||||
@@ -49,16 +57,114 @@ This is a maven plugin that allows for developers and organizations to ban Maven
|
|||||||
</project>
|
</project>
|
||||||
```
|
```
|
||||||
|
|
||||||
The `extensions` elements is critical. Without it, the plugin does nothing. With it, the plugin is able to detected ban artifacts before they are downloaded.
|
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 works with both dependencies and plugins. This keeps libraries from even reaching your local Maven repository cache.
|
||||||
|
|
||||||
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.
|
### Purge Banned Artifacts
|
||||||
|
|
||||||
If `groupId` is not provided, it is ignored in the matching process. So it will match all artifact group IDs and the constraint will be for `artifactId` and `version` only. The same is true for `artifactId` and `version`. This means that `<includes><artifact></artifact></includes>` will ban every artifact.
|
Here is an example of the non-extension use case for the plugin. You could use the same plugin for both preventing banned artifacts and cleaning up previously downloaded ones. Just set `extensions` to `true` in those cases, as highlighted in the previous section.
|
||||||
|
|
||||||
If `groupId` and `groupIdRegex` are both provided, only `groupId` is used. The same is true for `artifactId` and `artifactIdRegex`. The `*Regex` values use standard Java regular expressions. If using regular expressions, remember to escape the dots (`\.`) in group IDs.
|
```xml
|
||||||
|
<project>
|
||||||
|
...
|
||||||
|
<build>
|
||||||
|
...
|
||||||
|
<plugins>
|
||||||
|
...
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.inteligr8</groupId>
|
||||||
|
<artifactId>ban-maven-plugin</artifactId>
|
||||||
|
<version>...</version>
|
||||||
|
<configuration>
|
||||||
|
...
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>clean</id>
|
||||||
|
<phase>clean</phase>
|
||||||
|
<goals><goal>purge-repo</goal></goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
...
|
||||||
|
</plugins>
|
||||||
|
...
|
||||||
|
</build>
|
||||||
|
...
|
||||||
|
</project>
|
||||||
|
```
|
||||||
|
|
||||||
|
The `purge-repo` goal will remove all banned artifacts from your local Maven cache. It does not support `groupIdRegex` or blank `groupId` specifications. So any of those will not be purged/removed.
|
||||||
|
|
||||||
|
For instance, you can use the following and expect it to work for preventing and purging banned dependencies and plugins:
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<include>
|
||||||
|
<artifact>
|
||||||
|
<groupId>...<groupId>
|
||||||
|
<artifactId>...<artifactId>
|
||||||
|
<version>...</version>
|
||||||
|
</artifact>
|
||||||
|
<artifact>com.inteligr8:ban-maven-plugin:[,1.0.0)</artifact>
|
||||||
|
<artifact>log4j:log4j</artifact>
|
||||||
|
</include>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
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 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,)`.
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
If you *include* all versions by omitting the `version` element, you can still *exclude* (unban) certain versions, like `[1.2.17,)`.
|
If you *include* all versions by omitting the `version` element, you can still *exclude* (un-ban) certain versions, like `[1.2.17,)`.
|
||||||
|
|
||||||
|
Order does not matter. All include specifications are processed, followed by all exclude specifications.
|
||||||
|
|
||||||
|
## Import
|
||||||
|
|
||||||
|
The `import` file, URL, and artifact are to reference XML files that conform to the same `configuration` element as described here. In fact, the root elmenet of that XML should be `configuration`. It will only support the `includes` and `excludes` elements. so you cannot do recursive imports.
|
||||||
|
|
||||||
|
You can create a Maven `pom` packaging type project that deploys a configuration XML to your Maven repository. Then use an `import` to allow you to change banned dependencies without making changes to each individual project. Just like with the `version` notation in the `includes` and `excludes` elements, your `import` `artifact` element supports a version range. This way the latest banned dependencies can be side-loaded into all projects. This means previously functioning builds may eventually start failing. That is by design in this scenario.
|
||||||
|
|
||||||
|
The `import` elements supports multiple `url` or `artifact` declarations. All imported and directly specified include specifications are processed before all exclude specifications. You cannot change an include when importing, but you can add new ones, that may cover more versions; and you can exclude versions that may have been included by the import.
|
||||||
|
|
||||||
|
The `excludes` element is a way to provide project-by-project exceptions to imported banned artifacts where warranted.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
The recommended use of this plugin is for its use across whole organizations. First, you will want a simple Maven project that is referenced by all other Maven projects. That simple project will declare the banned artifacts and potentially purge existing ones. See the `examples/ban-config` project for a full example.
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<configuration>
|
||||||
|
<includes>
|
||||||
|
<!-- CVE-2019-17571 -->
|
||||||
|
<artifact>org.apache.logging.log4j::[,2.17.1)</artifact>
|
||||||
|
<artifact>log4j:log4j</artifact>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
```
|
||||||
|
|
||||||
|
Deploying that project will result in the publication of the `ban-config.xml` to your Maven repository. That is where it can be picked up by all other projects so they can enforce the ban. If you do not have a local Maven repository, then you will have to upload the `ban-config.xml` to some other URL-accessible location by some other means.
|
||||||
|
|
||||||
|
Once you have that in place, you will want to add the following to every single Maven project that should be governed by the aforementioned `ban-config`. See the `examples/governed-artifact` project for a full example.
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.inteligr8</groupId>
|
||||||
|
<artifactId>ban-maven-plugin</artifactId>
|
||||||
|
<version>...</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<import>
|
||||||
|
<artifact>com.inteligr8:ban-config:[2025.03,)</artifact>
|
||||||
|
</import>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
```
|
||||||
|
8
examples/ban-config/ban-config.xml
Normal file
8
examples/ban-config/ban-config.xml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<configuration>
|
||||||
|
<includes>
|
||||||
|
<!-- CVE-2019-17571 -->
|
||||||
|
<artifact>org.apache.logging.log4j::[,2.17.1)</artifact>
|
||||||
|
<artifact>log4j:log4j</artifact>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
85
examples/ban-config/pom.xml
Normal file
85
examples/ban-config/pom.xml
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
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-config</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<!-- A monthly cadence is reasonable at most organizations -->
|
||||||
|
<version>2025.03</version>
|
||||||
|
|
||||||
|
<name>Banned Artifact Configuration</name>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>.</directory>
|
||||||
|
<includes>
|
||||||
|
<include>*-config.xml</include>
|
||||||
|
</includes>
|
||||||
|
<targetPath>${project.build.directory}</targetPath>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.inteligr8</groupId>
|
||||||
|
<artifactId>ban-maven-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>purge-maven-repo</id>
|
||||||
|
<phase>clean</phase>
|
||||||
|
<goals><goal>purge-repo</goal></goals>
|
||||||
|
<configuration>
|
||||||
|
<import>
|
||||||
|
<file>ban-config.xml</file>
|
||||||
|
</import>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>filter</id>
|
||||||
|
<goals><goal>resources</goal></goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-install-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skip>true</skip>
|
||||||
|
<file>${project.build.directory}/ban-config.xml</file>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>install-xml</id>
|
||||||
|
<phase>install</phase>
|
||||||
|
<goals><goal>install-file</goal></goals>
|
||||||
|
<configuration>
|
||||||
|
<groupId>${project.groupId}</groupId>
|
||||||
|
<artifactId>${project.artifactId}</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<packaging>xml</packaging>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<file>ban-config.xml</file>
|
||||||
|
<pomFile>pom.xml</pomFile>
|
||||||
|
<packaging>xml</packaging>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
31
examples/governed-artifact/pom.xml
Normal file
31
examples/governed-artifact/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
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>governed-artifact</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>Any Governed Artifact</name>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.inteligr8</groupId>
|
||||||
|
<artifactId>ban-maven-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<import>
|
||||||
|
<artifact>com.inteligr8:ban-config:[2025.03,)</artifact>
|
||||||
|
</import>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
32
pom.xml
32
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
<groupId>com.inteligr8</groupId>
|
<groupId>com.inteligr8</groupId>
|
||||||
<artifactId>ban-maven-plugin</artifactId>
|
<artifactId>ban-maven-plugin</artifactId>
|
||||||
<version>1.1.1</version>
|
<version>1.4.1</version>
|
||||||
<packaging>maven-plugin</packaging>
|
<packaging>maven-plugin</packaging>
|
||||||
|
|
||||||
<name>Ban Dependencies Maven Plugin</name>
|
<name>Ban Dependencies Maven Plugin</name>
|
||||||
@@ -43,14 +43,14 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<maven.version>3.9.0</maven.version>
|
<maven.version>3.9.9</maven.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>3.4</version>
|
<version>3.17.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.maven.shared</groupId>
|
<groupId>org.apache.maven.shared</groupId>
|
||||||
@@ -66,7 +66,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.maven.plugin-tools</groupId>
|
<groupId>org.apache.maven.plugin-tools</groupId>
|
||||||
<artifactId>maven-plugin-annotations</artifactId>
|
<artifactId>maven-plugin-annotations</artifactId>
|
||||||
<version>3.7.1</version>
|
<version>3.15.1</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -90,22 +90,36 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.12</version>
|
<version>4.13.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-plugin-plugin</artifactId>
|
||||||
|
<version>3.15.1</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-invoker-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-plugin-plugin</artifactId>
|
<artifactId>maven-plugin-plugin</artifactId>
|
||||||
<version>3.7.1</version>
|
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>default-descriptor</id>
|
<id>default-descriptor</id>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>descriptor</goal>
|
<goal>descriptor</goal>
|
||||||
</goals>
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<goalPrefix>ban</goalPrefix>
|
||||||
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
<execution>
|
<execution>
|
||||||
<id>help-descriptor</id>
|
<id>help-descriptor</id>
|
||||||
@@ -131,7 +145,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.plexus</groupId>
|
<groupId>org.codehaus.plexus</groupId>
|
||||||
<artifactId>plexus-component-metadata</artifactId>
|
<artifactId>plexus-component-metadata</artifactId>
|
||||||
<version>2.1.1</version>
|
<version>2.2.0</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
@@ -142,7 +156,6 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-invoker-plugin</artifactId>
|
<artifactId>maven-invoker-plugin</artifactId>
|
||||||
<version>3.4.0</version>
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<projectsDirectory>${basedir}/src/it</projectsDirectory>
|
<projectsDirectory>${basedir}/src/it</projectsDirectory>
|
||||||
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
|
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
|
||||||
@@ -178,7 +191,6 @@
|
|||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-invoker-plugin</artifactId>
|
<artifactId>maven-invoker-plugin</artifactId>
|
||||||
<version>3.2.2</version>
|
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>run-its</id>
|
<id>run-its</id>
|
||||||
@@ -237,7 +249,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.sonatype.plugins</groupId>
|
<groupId>org.sonatype.plugins</groupId>
|
||||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||||
<version>1.6.13</version>
|
<version>1.7.0</version>
|
||||||
<extensions>true</extensions>
|
<extensions>true</extensions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<serverId>ossrh</serverId>
|
<serverId>ossrh</serverId>
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.inteligr8.maven.ban;
|
package com.inteligr8.maven.ban;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -23,34 +24,127 @@ import java.util.regex.Pattern;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
|
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
|
||||||
import org.apache.maven.artifact.versioning.VersionRange;
|
import org.apache.maven.artifact.versioning.VersionRange;
|
||||||
|
import org.apache.maven.execution.MavenSession;
|
||||||
import org.apache.maven.plugin.MojoFailureException;
|
import org.apache.maven.plugin.MojoFailureException;
|
||||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||||
|
import org.eclipse.aether.artifact.Artifact;
|
||||||
|
import org.eclipse.aether.artifact.DefaultArtifact;
|
||||||
|
import org.eclipse.aether.impl.ArtifactResolver;
|
||||||
|
import org.eclipse.aether.impl.VersionRangeResolver;
|
||||||
|
import org.eclipse.aether.resolution.ArtifactRequest;
|
||||||
|
import org.eclipse.aether.resolution.ArtifactResolutionException;
|
||||||
|
import org.eclipse.aether.resolution.ArtifactResult;
|
||||||
|
import org.eclipse.aether.resolution.VersionRangeRequest;
|
||||||
|
import org.eclipse.aether.resolution.VersionRangeResolutionException;
|
||||||
|
import org.eclipse.aether.resolution.VersionRangeResult;
|
||||||
|
import org.eclipse.aether.version.Version;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.inteligr8.maven.model.ArtifactFilter;
|
import com.inteligr8.maven.model.ArtifactFilter;
|
||||||
|
|
||||||
public class AbstractBanConfiguration implements BanConfiguration {
|
public abstract class AbstractBanConfiguration implements BanConfiguration {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
private final Pattern artifactPattern = Pattern.compile("^([^:]+):([^:]+)(:([^:]+))?$");
|
private final Pattern artifactPattern = Pattern.compile("^([^:]*):([^:]*)(:([^:]+))?$");
|
||||||
private final Pattern notRegexPattern = Pattern.compile("^[A-Za-z0-9_\\.]*$");
|
private final Pattern notRegexPattern = Pattern.compile("^[A-Za-z0-9_\\-\\.]*$");
|
||||||
|
|
||||||
private final List<ArtifactFilter> includeArtifacts = new LinkedList<>();
|
protected final List<ArtifactFilter> includeArtifacts = new LinkedList<>();
|
||||||
private final List<ArtifactFilter> excludeArtifacts = new LinkedList<>();
|
protected final List<ArtifactFilter> excludeArtifacts = new LinkedList<>();
|
||||||
|
|
||||||
|
private final ArtifactResolver artifactResolver;
|
||||||
|
private final VersionRangeResolver versionRangeResolver;
|
||||||
|
private final MavenSession session;
|
||||||
|
|
||||||
|
public AbstractBanConfiguration(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver) {
|
||||||
|
this.session = session;
|
||||||
|
this.artifactResolver = artifactResolver;
|
||||||
|
this.versionRangeResolver = versionRangeResolver;
|
||||||
|
}
|
||||||
|
|
||||||
public void init(Xpp3Dom rootDom) throws IOException, MojoFailureException {
|
public void init(Xpp3Dom rootDom) throws IOException, MojoFailureException {
|
||||||
if (rootDom == null)
|
if (rootDom == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Xpp3Dom importDom = rootDom.getChild("import");
|
Xpp3Dom importDom = rootDom.getChild("import");
|
||||||
if (importDom != null) {
|
if (importDom != null)
|
||||||
String url = StringUtils.trimToNull(importDom.getValue());
|
this.processImports(importDom);
|
||||||
BanConfigurationDownloader downloader = new BanConfigurationDownloader(url);
|
this.processIncludesExcludes(rootDom);
|
||||||
this.includeArtifacts.addAll(downloader.getIncludeArtifacts());
|
}
|
||||||
this.excludeArtifacts.addAll(downloader.getExcludeArtifacts());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private void processImports(Xpp3Dom importDom) throws IOException, MojoFailureException {
|
||||||
|
for (Xpp3Dom child : importDom.getChildren()) {
|
||||||
|
BanConfigurationDownloader downloader = null;
|
||||||
|
if (child.getName().equals("file")) {
|
||||||
|
File file = new File(StringUtils.trimToNull(child.getValue()));
|
||||||
|
downloader = new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, file);
|
||||||
|
} else if (child.getName().equals("url")) {
|
||||||
|
String url = StringUtils.trimToNull(child.getValue());
|
||||||
|
downloader = new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, url);
|
||||||
|
} else if (child.getName().equals("artifact")) {
|
||||||
|
Artifact artifact = new DefaultArtifact(child.getValue());
|
||||||
|
if (!"xml".equals(artifact.getExtension()))
|
||||||
|
artifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), "xml", artifact.getVersion());
|
||||||
|
|
||||||
|
Version latestVersion = this.findLatestVersion(artifact, child.getValue());
|
||||||
|
Artifact latestArtifact = this.findLatestArtifact(artifact, child.getValue());
|
||||||
|
if (latestArtifact == null && latestVersion != null) {
|
||||||
|
this.logger.debug("A latest version was found, but could not resolve the artifact using the range; trying to resolve the artifact with the specific version: {}: {}", latestVersion, child.getValue());
|
||||||
|
artifact = artifact.setVersion(latestVersion.toString());
|
||||||
|
latestArtifact = this.findLatestArtifact(artifact, child.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (latestArtifact != null && latestArtifact.getFile() != null) {
|
||||||
|
this.logger.debug("The latest artifact was found: {}", latestArtifact);
|
||||||
|
File file = latestArtifact.getFile();
|
||||||
|
downloader = new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, file);
|
||||||
|
} else if (artifact != null) {
|
||||||
|
File file = artifact.getFile();
|
||||||
|
downloader = new BanConfigurationDownloader(this.session, this.artifactResolver, this.versionRangeResolver, file);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.logger.debug("Unrecognized configuration element ignored: {}: {}", child.getName(), child.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (downloader != null) {
|
||||||
|
this.includeArtifacts.addAll(downloader.getIncludeArtifacts());
|
||||||
|
this.excludeArtifacts.addAll(downloader.getExcludeArtifacts());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Version findLatestVersion(Artifact artifact, String logId) {
|
||||||
|
this.logger.trace("Inspecting the local and remote repositories to select the version to import: {}", logId);
|
||||||
|
VersionRangeRequest vrrequest = new VersionRangeRequest(artifact, this.session.getCurrentProject().getRemoteProjectRepositories(), null);
|
||||||
|
try {
|
||||||
|
VersionRangeResult vrresult = this.versionRangeResolver.resolveVersionRange(this.session.getRepositorySession(), vrrequest);
|
||||||
|
if (vrresult.getVersions().isEmpty()) {
|
||||||
|
this.logger.info("The artifact version range could not be resolved: {}", logId);
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
this.logger.debug("The artifact version discovered: {}: {}", vrresult.getHighestVersion(), logId);
|
||||||
|
return vrresult.getHighestVersion();
|
||||||
|
}
|
||||||
|
} catch (VersionRangeResolutionException vrre) {
|
||||||
|
this.logger.error("The artifact version range could not be resolved; skipping: {}", logId);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Artifact findLatestArtifact(Artifact artifact, String logId) {
|
||||||
|
this.logger.trace("Inspecting the local/remote repositories to select the artifact to import: {}", logId);
|
||||||
|
ArtifactRequest arequest = new ArtifactRequest(artifact, this.session.getCurrentProject().getRemoteProjectRepositories(), null);
|
||||||
|
try {
|
||||||
|
ArtifactResult aresult = this.artifactResolver.resolveArtifact(this.session.getRepositorySession(), arequest);
|
||||||
|
this.logger.debug("This artifact version discovered: {}: {}", aresult.getArtifact().getVersion(), logId);
|
||||||
|
return aresult.getArtifact();
|
||||||
|
} catch (ArtifactResolutionException are) {
|
||||||
|
this.logger.warn("The artifact could not be resolved; skipping: {}", artifact);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processIncludesExcludes(Xpp3Dom rootDom) {
|
||||||
Xpp3Dom includesDom = rootDom.getChild("includes");
|
Xpp3Dom includesDom = rootDom.getChild("includes");
|
||||||
if (includesDom != null)
|
if (includesDom != null)
|
||||||
this.includeArtifacts.addAll(this.parseArtifacts(includesDom));
|
this.includeArtifacts.addAll(this.parseArtifacts(includesDom));
|
||||||
|
@@ -15,14 +15,19 @@
|
|||||||
package com.inteligr8.maven.ban;
|
package com.inteligr8.maven.ban;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.apache.maven.execution.MavenSession;
|
||||||
import org.apache.maven.plugin.MojoFailureException;
|
import org.apache.maven.plugin.MojoFailureException;
|
||||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||||
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
|
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
|
||||||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
||||||
|
import org.eclipse.aether.impl.ArtifactResolver;
|
||||||
|
import org.eclipse.aether.impl.VersionRangeResolver;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -30,7 +35,9 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public BanConfigurationDownloader(String url) throws IOException, MojoFailureException {
|
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, String url) throws IOException, MojoFailureException {
|
||||||
|
super(session, artifactResolver, versionRangeResolver);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Xpp3Dom rootDom = this.load(new URL(url));
|
Xpp3Dom rootDom = this.load(new URL(url));
|
||||||
this.init(rootDom);
|
this.init(rootDom);
|
||||||
@@ -39,6 +46,17 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, File file) throws IOException, MojoFailureException {
|
||||||
|
super(session, artifactResolver, versionRangeResolver);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Xpp3Dom rootDom = this.load(file);
|
||||||
|
this.init(rootDom);
|
||||||
|
} catch (XmlPullParserException xppe) {
|
||||||
|
throw new MojoFailureException(xppe.getMessage(), xppe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Xpp3Dom load(URL url) throws IOException, XmlPullParserException {
|
private Xpp3Dom load(URL url) throws IOException, XmlPullParserException {
|
||||||
InputStream istream = url.openStream();
|
InputStream istream = url.openStream();
|
||||||
BufferedInputStream bistream = new BufferedInputStream(istream, 16384);
|
BufferedInputStream bistream = new BufferedInputStream(istream, 16384);
|
||||||
@@ -48,7 +66,17 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
|
|||||||
} finally {
|
} finally {
|
||||||
bistream.close();
|
bistream.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Xpp3Dom load(File file) throws IOException, XmlPullParserException {
|
||||||
|
FileInputStream fistream = new FileInputStream(file);
|
||||||
|
BufferedInputStream bistream = new BufferedInputStream(fistream, 16384);
|
||||||
|
try {
|
||||||
|
this.logger.debug("Downloading configuration: {}", file);
|
||||||
|
return Xpp3DomBuilder.build(bistream, "utf-8");
|
||||||
|
} finally {
|
||||||
|
bistream.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -16,13 +16,18 @@ package com.inteligr8.maven.ban;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.maven.execution.MavenSession;
|
||||||
import org.apache.maven.model.Plugin;
|
import org.apache.maven.model.Plugin;
|
||||||
import org.apache.maven.plugin.MojoFailureException;
|
import org.apache.maven.plugin.MojoFailureException;
|
||||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||||
|
import org.eclipse.aether.impl.ArtifactResolver;
|
||||||
|
import org.eclipse.aether.impl.VersionRangeResolver;
|
||||||
|
|
||||||
public class BanConfigurationParser extends AbstractBanConfiguration {
|
public class BanConfigurationParser extends AbstractBanConfiguration {
|
||||||
|
|
||||||
public BanConfigurationParser(Plugin plugin) throws IOException, MojoFailureException {
|
public BanConfigurationParser(MavenSession session, ArtifactResolver artifactResolver, VersionRangeResolver versionRangeResolver, Plugin plugin) throws IOException, MojoFailureException {
|
||||||
|
super(session, artifactResolver, versionRangeResolver);
|
||||||
|
|
||||||
Xpp3Dom rootDom = (Xpp3Dom) plugin.getConfiguration();
|
Xpp3Dom rootDom = (Xpp3Dom) plugin.getConfiguration();
|
||||||
this.init(rootDom);
|
this.init(rootDom);
|
||||||
}
|
}
|
||||||
|
@@ -29,6 +29,7 @@ import org.apache.maven.model.Plugin;
|
|||||||
import org.apache.maven.plugin.MojoFailureException;
|
import org.apache.maven.plugin.MojoFailureException;
|
||||||
import org.apache.maven.plugin.PluginResolutionException;
|
import org.apache.maven.plugin.PluginResolutionException;
|
||||||
import org.apache.maven.plugin.internal.PluginDependenciesResolver;
|
import org.apache.maven.plugin.internal.PluginDependenciesResolver;
|
||||||
|
import org.apache.maven.plugins.annotations.Parameter;
|
||||||
import org.apache.maven.project.DefaultDependencyResolutionRequest;
|
import org.apache.maven.project.DefaultDependencyResolutionRequest;
|
||||||
import org.apache.maven.project.DependencyResolutionException;
|
import org.apache.maven.project.DependencyResolutionException;
|
||||||
import org.apache.maven.project.DependencyResolutionResult;
|
import org.apache.maven.project.DependencyResolutionResult;
|
||||||
@@ -39,9 +40,13 @@ import org.eclipse.aether.artifact.DefaultArtifact;
|
|||||||
import org.eclipse.aether.graph.Dependency;
|
import org.eclipse.aether.graph.Dependency;
|
||||||
import org.eclipse.aether.graph.DependencyFilter;
|
import org.eclipse.aether.graph.DependencyFilter;
|
||||||
import org.eclipse.aether.graph.DependencyNode;
|
import org.eclipse.aether.graph.DependencyNode;
|
||||||
|
import org.eclipse.aether.impl.ArtifactResolver;
|
||||||
|
import org.eclipse.aether.impl.VersionRangeResolver;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.inteligr8.maven.ban.model.ImportConfig;
|
||||||
|
|
||||||
@Named("ban")
|
@Named("ban")
|
||||||
@Singleton
|
@Singleton
|
||||||
public class BanExtension extends AbstractMavenLifecycleParticipant {
|
public class BanExtension extends AbstractMavenLifecycleParticipant {
|
||||||
@@ -52,22 +57,41 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
|
|||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ArtifactResolver artifactResolver;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private VersionRangeResolver versionRangeResolver;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ProjectDependenciesResolver projDepResolver;
|
private ProjectDependenciesResolver projDepResolver;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PluginDependenciesResolver pluginDepResolver;
|
private PluginDependenciesResolver pluginDepResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The configuration is parsed manually. This is here to prevent warning messages with IDEs and builders.
|
||||||
|
*/
|
||||||
|
@Parameter(name = "import")
|
||||||
|
private ImportConfig importConfig;
|
||||||
|
|
||||||
|
@Parameter(name = "includes")
|
||||||
|
private List<String> includes;
|
||||||
|
|
||||||
|
@Parameter(name = "excludes")
|
||||||
|
private List<String> excludes;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
|
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
|
||||||
MavenProject project = session.getCurrentProject();
|
BanConfiguration config = this.getConfiguration(session);
|
||||||
BanConfiguration config = this.getConfiguration(project);
|
|
||||||
if (config == null)
|
if (config == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
BanDependencyFilter depFilter = new BanDependencyFilter(config.getIncludeArtifacts(), config.getExcludeArtifacts());
|
BanDependencyFilter depFilter = new BanDependencyFilter(config.getIncludeArtifacts(), config.getExcludeArtifacts());
|
||||||
depFilter.setFailFast(true);
|
depFilter.setFailFast(true);
|
||||||
|
|
||||||
|
MavenProject project = session.getCurrentProject();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (Plugin plugin : project.getBuildPlugins()) {
|
for (Plugin plugin : project.getBuildPlugins()) {
|
||||||
this.logger.debug("Evaluating plugin dependencies: {}", plugin);
|
this.logger.debug("Evaluating plugin dependencies: {}", plugin);
|
||||||
@@ -97,7 +121,8 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BanConfigurationParser getConfiguration(MavenProject project) throws MavenExecutionException {
|
private BanConfiguration getConfiguration(MavenSession session) throws MavenExecutionException {
|
||||||
|
MavenProject project = session.getCurrentProject();
|
||||||
Plugin plugin = project.getPlugin(THIS_PLUGIN_KEY);
|
Plugin plugin = project.getPlugin(THIS_PLUGIN_KEY);
|
||||||
if (plugin == null)
|
if (plugin == null)
|
||||||
throw new MavenExecutionException("The plugin is executing but it cannot be found", project.getFile());
|
throw new MavenExecutionException("The plugin is executing but it cannot be found", project.getFile());
|
||||||
@@ -107,7 +132,7 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
|
|||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return new BanConfigurationParser(plugin);
|
return new BanConfigurationParser(session, this.artifactResolver, this.versionRangeResolver, plugin);
|
||||||
} catch (IOException | MojoFailureException e) {
|
} catch (IOException | MojoFailureException e) {
|
||||||
throw new MavenExecutionException(e.getMessage(), project.getFile());
|
throw new MavenExecutionException(e.getMessage(), project.getFile());
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@ package com.inteligr8.maven.ban;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.DirectoryNotEmptyException;
|
||||||
import java.nio.file.FileVisitResult;
|
import java.nio.file.FileVisitResult;
|
||||||
import java.nio.file.FileVisitor;
|
import java.nio.file.FileVisitor;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -23,6 +24,7 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -44,6 +46,8 @@ import org.apache.maven.plugins.annotations.Mojo;
|
|||||||
import org.apache.maven.plugins.annotations.Parameter;
|
import org.apache.maven.plugins.annotations.Parameter;
|
||||||
import org.apache.maven.project.MavenProject;
|
import org.apache.maven.project.MavenProject;
|
||||||
import org.codehaus.plexus.component.annotations.Component;
|
import org.codehaus.plexus.component.annotations.Component;
|
||||||
|
import org.eclipse.aether.impl.ArtifactResolver;
|
||||||
|
import org.eclipse.aether.impl.VersionRangeResolver;
|
||||||
|
|
||||||
import com.inteligr8.maven.model.ArtifactFilter;
|
import com.inteligr8.maven.model.ArtifactFilter;
|
||||||
|
|
||||||
@@ -54,6 +58,12 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
@Inject
|
@Inject
|
||||||
private MavenSession session;
|
private MavenSession session;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ArtifactResolver artifactResolver;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private VersionRangeResolver versionRangeResolver;
|
||||||
|
|
||||||
@Parameter(name = "skip", defaultValue = "false")
|
@Parameter(name = "skip", defaultValue = "false")
|
||||||
private boolean skip = false;
|
private boolean skip = false;
|
||||||
|
|
||||||
@@ -78,7 +88,7 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
private void purge() throws MojoFailureException, IOException {
|
private void purge() throws MojoFailureException, IOException {
|
||||||
List<Path> includePaths = new LinkedList<>();
|
List<Path> includePaths = new LinkedList<>();
|
||||||
|
|
||||||
BanConfigurationParser config = this.getConfiguration(this.session.getCurrentProject());
|
BanConfigurationParser config = this.getConfiguration();
|
||||||
|
|
||||||
for (ArtifactFilter afilter : config.getIncludeArtifacts()) {
|
for (ArtifactFilter afilter : config.getIncludeArtifacts()) {
|
||||||
if (afilter.getGroupId() == null) {
|
if (afilter.getGroupId() == null) {
|
||||||
@@ -86,10 +96,13 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Path groupPath = this.getGroupPath(afilter);
|
Path groupPath = this.resolveGroupPath(afilter);
|
||||||
List<Path> artifactPaths = this.getArtifactPaths(groupPath, afilter);
|
if (groupPath == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
List<Path> artifactPaths = this.resolveArtifactPaths(groupPath, afilter);
|
||||||
for (Path artifactPath : artifactPaths)
|
for (Path artifactPath : artifactPaths)
|
||||||
includePaths.addAll(this.getVersionPaths(artifactPath, afilter.getVersionRange()));
|
includePaths.addAll(this.resolveVersionPaths(artifactPath, afilter.getVersionRange()));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.getLog().debug("May be purging all files in " + includePaths.size() + " paths");
|
this.getLog().debug("May be purging all files in " + includePaths.size() + " paths");
|
||||||
@@ -101,7 +114,7 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
StringBuilder regex = new StringBuilder();
|
StringBuilder regex = new StringBuilder();
|
||||||
|
|
||||||
if (afilter.getGroupId() != null) {
|
if (afilter.getGroupId() != null) {
|
||||||
regex.append('^').append(this.getGroupPath(afilter));
|
regex.append('^').append(this.resolveGroupPath(afilter));
|
||||||
} else if (afilter.getGroupIdRegex() != null) {
|
} else if (afilter.getGroupIdRegex() != null) {
|
||||||
regex.append(afilter.getGroupIdRegex().replace("\\.", regexDirectorySeparator));
|
regex.append(afilter.getGroupIdRegex().replace("\\.", regexDirectorySeparator));
|
||||||
if (regex.charAt(0) != '^')
|
if (regex.charAt(0) != '^')
|
||||||
@@ -146,40 +159,60 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
this.getLog().info("DRYRUN: Would have deleted certain paths from local Maven cache: " + repoPath);
|
this.getLog().info("DRYRUN: Would have deleted certain paths from local Maven cache: " + repoPath);
|
||||||
this.getLog().info("DRYRUN: Would have deleted these paths: " + includePaths);
|
this.getLog().info("DRYRUN: Would have deleted these paths: " + includePaths);
|
||||||
} else {
|
} else {
|
||||||
for (Path path : includePaths) {
|
for (Path versionPath : includePaths) {
|
||||||
Path fullpath = repoPath.resolve(path);
|
Path fullVersionPath = repoPath.resolve(versionPath);
|
||||||
if (Files.exists(fullpath)) {
|
if (Files.exists(fullVersionPath)) {
|
||||||
this.getLog().info("Deleting version from Maven cache: " + path);
|
this.getLog().info("Deleting version from Maven cache: " + versionPath);
|
||||||
Files.walkFileTree(fullpath, new DeleteDirectoryVisitor());
|
Files.walkFileTree(fullVersionPath, new DeleteNonMetadataVisitor());
|
||||||
} else {
|
} else {
|
||||||
// this will probably never happen
|
// this will probably never happen
|
||||||
this.getLog().debug("Maven cache does not exist: " + path);
|
this.getLog().debug("Maven cache does not exist: " + versionPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BanConfigurationParser getConfiguration(MavenProject project) throws MojoFailureException, IOException {
|
private BanConfigurationParser getConfiguration() throws MojoFailureException, IOException {
|
||||||
|
MavenProject project = this.session.getCurrentProject();
|
||||||
Plugin plugin = project.getPlugin(BanExtension.THIS_PLUGIN_KEY);
|
Plugin plugin = project.getPlugin(BanExtension.THIS_PLUGIN_KEY);
|
||||||
if (plugin == null)
|
if (plugin == null)
|
||||||
throw new MojoFailureException("The plugin is executing but it cannot be found");
|
throw new MojoFailureException("The plugin is executing but it cannot be found");
|
||||||
return new BanConfigurationParser(plugin);
|
return new BanConfigurationParser(this.session, this.artifactResolver, this.versionRangeResolver, plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path getGroupPath(ArtifactFilter afilter) {
|
private Path resolveGroupPath(ArtifactFilter afilter) {
|
||||||
|
if (afilter.getGroupId() == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
String[] pathElements = afilter.getGroupId().split("\\.");
|
String[] pathElements = afilter.getGroupId().split("\\.");
|
||||||
Path groupPath = Paths.get("");
|
Path groupPath = Paths.get("");
|
||||||
for (String pathElement : pathElements)
|
for (String pathElement : pathElements)
|
||||||
groupPath = groupPath.resolve(pathElement);
|
groupPath = groupPath.resolve(pathElement);
|
||||||
|
|
||||||
|
Path fullGroupPath = this.getRepositoryPath().resolve(groupPath);
|
||||||
|
if (!Files.exists(fullGroupPath)) {
|
||||||
|
this.getLog().debug("The group path does not exist, so nothing to purge: " + fullGroupPath);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return groupPath;
|
return groupPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Path> getArtifactPaths(Path groupPath, ArtifactFilter afilter) throws IOException {
|
private List<Path> resolveArtifactPaths(Path groupPath, ArtifactFilter afilter) throws IOException {
|
||||||
if (afilter.getArtifactId() != null)
|
Path repoPath = this.getRepositoryPath();
|
||||||
return Arrays.asList(groupPath.resolve(afilter.getArtifactId()));
|
|
||||||
|
if (afilter.getArtifactId() != null) {
|
||||||
|
Path artifactPath = groupPath.resolve(afilter.getArtifactId());
|
||||||
|
Path fullArtifactPath = repoPath.resolve(artifactPath);
|
||||||
|
if (Files.exists(fullArtifactPath)) {
|
||||||
|
return Arrays.asList(artifactPath);
|
||||||
|
} else {
|
||||||
|
this.getLog().debug("The artifact path does not exist, so nothing to purge: " + fullArtifactPath);
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Pattern artifactPattern = afilter.getArtifactIdRegex() == null ? null : Pattern.compile(afilter.getArtifactIdRegex());
|
Pattern artifactPattern = afilter.getArtifactIdRegex() == null ? null : Pattern.compile(afilter.getArtifactIdRegex());
|
||||||
Path repoPath = this.getRepositoryPath();
|
|
||||||
List<Path> paths = new LinkedList<>();
|
List<Path> paths = new LinkedList<>();
|
||||||
|
|
||||||
if (artifactPattern == null)
|
if (artifactPattern == null)
|
||||||
@@ -187,14 +220,27 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
|
|
||||||
Files.list(repoPath.resolve(groupPath)).forEach(new Consumer<Path>() {
|
Files.list(repoPath.resolve(groupPath)).forEach(new Consumer<Path>() {
|
||||||
@Override
|
@Override
|
||||||
public void accept(Path t) {
|
public void accept(Path fullArtifactPath) {
|
||||||
if (artifactPattern == null) {
|
if (artifactPattern == null) {
|
||||||
paths.add(repoPath.relativize(t));
|
// these may include sub-groups and not just artifacts
|
||||||
|
// which will lead to paths with artifacts as versions
|
||||||
|
// so we are looping through versions to see if it is indeed an artifact
|
||||||
|
try {
|
||||||
|
Files.list(fullArtifactPath).findFirst().ifPresent(new Consumer<Path>() {
|
||||||
|
@Override
|
||||||
|
public void accept(Path fullVersionPath) {
|
||||||
|
if (Files.exists(fullVersionPath.resolve("_remote.repositories")))
|
||||||
|
paths.add(repoPath.relativize(fullArtifactPath));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IOException ie) {
|
||||||
|
getLog().error(ie.getMessage(), ie);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Matcher matcher = artifactPattern.matcher(t.getFileName().toString());
|
Matcher matcher = artifactPattern.matcher(fullArtifactPath.getFileName().toString());
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
getLog().debug("The artifact directory '" + t.getFileName() + "' qualifies as included");
|
getLog().debug("The artifact directory '" + fullArtifactPath.getFileName() + "' qualifies as included");
|
||||||
paths.add(repoPath.relativize(t));
|
paths.add(repoPath.relativize(fullArtifactPath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,7 +249,7 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Path> getVersionPaths(Path artifactPath, VersionRange versionRange) throws IOException {
|
private List<Path> resolveVersionPaths(Path artifactPath, VersionRange versionRange) throws IOException {
|
||||||
Path repoPath = this.getRepositoryPath();
|
Path repoPath = this.getRepositoryPath();
|
||||||
List<Path> paths = new LinkedList<>();
|
List<Path> paths = new LinkedList<>();
|
||||||
|
|
||||||
@@ -234,7 +280,9 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
private class DeleteDirectoryVisitor implements FileVisitor<Path> {
|
private class DeleteNonMetadataVisitor implements FileVisitor<Path> {
|
||||||
|
|
||||||
|
private final Pattern versionPathPattern = Pattern.compile("/([^/]+)/([^/]+)/[^/]+$");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||||
@@ -243,7 +291,35 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
Files.delete(file);
|
if (attrs.isDirectory()) {
|
||||||
|
getLog().warn("An unexpected directory was found: " + file);
|
||||||
|
return FileVisitResult.SKIP_SUBTREE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matcher matcher = this.versionPathPattern.matcher(file.toString());
|
||||||
|
if (!matcher.find()) {
|
||||||
|
getLog().debug("Ignoring file from purge: " + file);
|
||||||
|
return FileVisitResult.CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
String artifactId = matcher.group(1);
|
||||||
|
String version = matcher.group(2);
|
||||||
|
String includeName = artifactId + "-" + version;
|
||||||
|
String excludeName = artifactId + "-" + version + ".pom";
|
||||||
|
getLog().debug("artifact-version: " + includeName);
|
||||||
|
|
||||||
|
if (file.getFileName().toString().startsWith(includeName) &&
|
||||||
|
!file.getFileName().toString().startsWith(excludeName)) {
|
||||||
|
try {
|
||||||
|
getLog().info("Deleting artifact: " + file);
|
||||||
|
Files.delete(file);
|
||||||
|
} catch (IOException ie) {
|
||||||
|
getLog().debug(ie);
|
||||||
|
getLog().warn("The file failed to delete: " + file);
|
||||||
|
return FileVisitResult.SKIP_SIBLINGS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return FileVisitResult.CONTINUE;
|
return FileVisitResult.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,7 +330,15 @@ public class PurgeRepoMojo extends AbstractMojo {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||||
Files.delete(dir);
|
try {
|
||||||
|
Files.delete(dir);
|
||||||
|
} catch (DirectoryNotEmptyException dnee) {
|
||||||
|
getLog().debug("The folder will not be deleted as it is not empty: " + dir);
|
||||||
|
} catch (IOException ie) {
|
||||||
|
getLog().debug(ie);
|
||||||
|
getLog().warn("The folder failed to delete: " + dir);
|
||||||
|
}
|
||||||
|
|
||||||
return FileVisitResult.CONTINUE;
|
return FileVisitResult.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
src/main/java/com/inteligr8/maven/ban/model/Config.java
Normal file
11
src/main/java/com/inteligr8/maven/ban/model/Config.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inteligr8.maven.ban.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
public ImportConfig importConfigs;
|
||||||
|
public List<String> includes;
|
||||||
|
public List<String> excludes;
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,10 @@
|
|||||||
|
package com.inteligr8.maven.ban.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ImportConfig {
|
||||||
|
|
||||||
|
public List<String> url;
|
||||||
|
public List<String> artifact;
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user