Skip to content

Commit

Permalink
Add a maven plugin for the dash license tool
Browse files Browse the repository at this point in the history
The maven plugin is fully configurable and has feature parity with
the command line tool. Also allows maven-dependency-plugin style
artifact filtering which allows users to ignore deps that are not
interesting (e.g. test deps or deps from the Eclipse Foundation
itself.)

Fixes #7

Signed-off-by: Mat Booth <mat.booth@redhat.com>
  • Loading branch information
mbooth101 committed Dec 18, 2020
1 parent 86c4373 commit dd8d6af
Show file tree
Hide file tree
Showing 11 changed files with 594 additions and 20 deletions.
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,29 @@ scrutiny.
### Example: Maven

```
$ mvn dependency:list | grep -Poh "\S+:(system|provided|compile)" | sort | uniq > maven.deps
$ cat maven.deps
commons-cli:commons-cli:jar:1.4:compile
commons-codec:commons-codec:jar:1.11:compile
commons-logging:commons-logging:jar:1.2:compile
org.apache.commons:commons-csv:jar:1.6:compile
org.apache.httpcomponents:httpclient:jar:4.5.10:compile
org.apache.httpcomponents:httpcore:jar:4.4.12:compile
org.glassfish:jakarta.json:jar:1.1.6:compile
$ java -jar target/org.eclipse.dash.licenses-<version>.jar -summary DEPENDENCIES maven.deps
Vetted license information was found for all content. No further investigation is required.
$ cat DEPENDENCIES
maven/mavencentral/org.glassfish/jakarta.json/1.1.6, EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0, approved, emo_ip_team
maven/mavencentral/commons-codec/commons-codec/1.11, Apache-2.0, approved, CQ15971
$ mvn org.eclipse.dash:dash-maven-plugin:license-check -Ddash.summary=DEPENDENCIES
[INFO] Scanning for projects...
[INFO]
[INFO] -------------< org.eclipse.dash:org.eclipse.dash.licenses >-------------
[INFO] Building org.eclipse.dash.licenses 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- dash-maven-plugin:0.0.1-SNAPSHOT:license-check (default-cli) @ org.eclipse.dash.licenses ---
[INFO] Vetted license information was found for all content. No further investigation is required.
[INFO] Summary file was written to: /home/mbooth/workspace-dash/dash-licenses/dash-core/DEPENDENCIES
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.434 s
[INFO] Finished at: 2020-12-18T11:27:38Z
[INFO] ------------------------------------------------------------------------
$ cat DEPENDENCIES
maven/mavencentral/org.glassfish/jakarta.json/2.0.0, EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0, approved, emo_ip_team
maven/mavencentral/org.checkerframework/checker-compat-qual/2.5.3, MIT, approved, clearlydefined
maven/mavencentral/commons-cli/commons-cli/1.4, Apache-2.0, approved, CQ13132
maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.12, Apache-2.0, approved, CQ18704
maven/mavencentral/commons-logging/commons-logging/1.2, Apache-2.0, approved, CQ10162
maven/mavencentral/org.apache.httpcomponents/httpclient/4.5.10, Apache-2.0, approved, CQ18703
maven/mavencentral/org.apache.commons/commons-csv/1.6, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.apache.commons/commons-csv/1.8, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.flogger/flogger/0.5.1, Apache-2.0, approved, clearlydefined
```

### Example: Gradle
Expand Down
38 changes: 38 additions & 0 deletions dash-maven-plugin/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions dash-maven-plugin/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>dash-maven-plugin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
6 changes: 6 additions & 0 deletions dash-maven-plugin/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
8 changes: 8 additions & 0 deletions dash-maven-plugin/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11
4 changes: 4 additions & 0 deletions dash-maven-plugin/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
50 changes: 50 additions & 0 deletions dash-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<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>
<parent>
<groupId>org.eclipse.dash</groupId>
<artifactId>dash-licenses-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dash-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-common-artifact-filters</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.dash</groupId>
<artifactId>org.eclipse.dash.licenses</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*************************************************************************
* Copyright (c) 2020, Red Hat Inc.
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution, and is available at https://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*************************************************************************/
package org.eclipse.dash.licenses.maven;

import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.apache.maven.shared.artifact.filter.collection.TypeFilter;

/**
* Abstract mojo containing artifact filtering capability.
*/
public abstract class AbstractArtifactFilteringMojo extends AbstractMojo {

/**
* Scope of dependencies to include, as determined by Maven. If specified, it
* must be one of the following values:
* <ul>
* <li><code>runtime</code> -- gives runtime and compile dependencies only.</li>
* <li><code>compile</code> -- (default) gives compile, provided, and system
* dependencies only.</li>
* <li><code>test</code> -- gives all dependencies.</li>
* <li><code>provided</code> -- gives provided dependencies only.</li>
* <li><code>system</code> -- gives system dependencies only.</li>
* </ul>
* If no scope is specified, then the default value of <code>compile</code> is
* used, including compile, provided, and system dependencies only.
*/
@Parameter(defaultValue = "compile")
protected String includeScope;

/**
* Scope to exclude. An empty string indicates no scopes (default).
*/
@Parameter(defaultValue = "")
protected String excludeScope;

/**
* Comma separated list of types to include. Empty string indicates include
* everything (default).
*/
@Parameter(defaultValue = "")
protected String includeTypes;

/**
* Comma separated list of types to exclude. Empty string indicates don't
* exclude anything (default).
*/
@Parameter(defaultValue = "")
protected String excludeTypes;

/**
* Comma separated list of classifiers to include. Empty string indicates
* include everything (default).
*/
@Parameter(defaultValue = "")
protected String includeClassifiers;

/**
* Comma separated list of classifiers to exclude. Empty string indicates don't
* exclude anything (default).
*/
@Parameter(defaultValue = "")
protected String excludeClassifiers;

/**
* Comma separated list of group IDs to include. Empty string indicates include
* everything (default).
*/
@Parameter(defaultValue = "")
protected String includeGroupIds;

/**
* Comma separated list of group IDs to exclude. Empty string indicates don't
* exclude anything (default).
*/
@Parameter(defaultValue = "")
protected String excludeGroupIds;

/**
* Comma separated list of artifact IDs to include. Empty string indicates
* include everything (default).
*/
@Parameter(defaultValue = "")
protected String includeArtifactIds;

/**
* Comma separated list of artifact IDs to exclude. Empty string indicates don't
* exclude anything (default).
*/
@Parameter(defaultValue = "")
protected String excludeArtifactIds;

/**
* Filter a set of artifacts based on the user-supplied filter parameters.
*
* @param artifacts a set of artifacts to filter
* @return a new set containing only the artifacts that match the filter
* @throws MojoExecutionException if there was a problem filtering the artifacts
*/
protected Set<Artifact> filterArtifacts(Set<Artifact> artifacts) throws MojoExecutionException {
FilterArtifacts filter = new FilterArtifacts();
filter.addFilter(new ScopeFilter(sanitise(includeScope), sanitise(excludeScope)));
filter.addFilter(new TypeFilter(sanitise(includeTypes), sanitise(excludeTypes)));
filter.addFilter(new ClassifierFilter(sanitise(includeClassifiers), sanitise(excludeClassifiers)));
filter.addFilter(new GroupIdFilter(sanitise(includeGroupIds), sanitise(excludeGroupIds)));
filter.addFilter(new ArtifactIdFilter(sanitise(includeArtifactIds), sanitise(excludeArtifactIds)));
try {
return filter.filter(artifacts);
} catch (ArtifactFilterException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

/**
* Utility to strip extraneous spaces from user-supplied filter parameters.
*/
private static String sanitise(String value) {
String sanitised = "";
if (value != null && !value.isEmpty()) {
sanitised = value.trim().replaceAll("[\\s]*,[\\s]*", ",");
}
return sanitised;
}
}

0 comments on commit dd8d6af

Please sign in to comment.