This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
commit 5ae0e5bbc15fa194afe1cb721df877fa73eadcef Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 00:22:33 2026 +0100 [MDEP-196] Add includes and excludes patterns to dependency:list and resolve --- .../list-includes-excludes/both.properties | 20 +++++ .../list-includes-excludes/excludes.properties | 19 +++++ .../list-includes-excludes/includes.properties | 19 +++++ .../list-includes-excludes/invoker.properties | 28 +++++++ src/it/projects/list-includes-excludes/pom.xml | 51 +++++++++++++ .../list-includes-excludes/resolve.properties | 19 +++++ .../projects/list-includes-excludes/verify.groovy | 34 +++++++++ .../AbstractDependencyFilterMojo.java | 15 ++++ .../resolvers/ResolveDependenciesMojo.java | 48 ++++++++++++ .../utils/filters/PatternsArtifactsFilter.java | 70 ++++++++++++++++++ src/site/markdown/usage.md.vm | 7 ++ .../utils/filters/PatternsArtifactsFilterTest.java | 86 ++++++++++++++++++++++ 12 files changed, 416 insertions(+) diff --git a/src/it/projects/list-includes-excludes/both.properties b/src/it/projects/list-includes-excludes/both.properties new file mode 100644 index 00000000..c26363b9 --- /dev/null +++ b/src/it/projects/list-includes-excludes/both.properties @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +outputFile = target/both.txt +includeModuleInfo = false +includes = org.apache.maven.its.dependency +excludes = :version-range diff --git a/src/it/projects/list-includes-excludes/excludes.properties b/src/it/projects/list-includes-excludes/excludes.properties new file mode 100644 index 00000000..c229c519 --- /dev/null +++ b/src/it/projects/list-includes-excludes/excludes.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +outputFile = target/excludes.txt +includeModuleInfo = false +excludes = :ResourceArtifact diff --git a/src/it/projects/list-includes-excludes/includes.properties b/src/it/projects/list-includes-excludes/includes.properties new file mode 100644 index 00000000..773a0df3 --- /dev/null +++ b/src/it/projects/list-includes-excludes/includes.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +outputFile = target/includes.txt +includeModuleInfo = false +includes = :mdep510-dep,:version-range diff --git a/src/it/projects/list-includes-excludes/invoker.properties b/src/it/projects/list-includes-excludes/invoker.properties new file mode 100644 index 00000000..3ea42edb --- /dev/null +++ b/src/it/projects/list-includes-excludes/invoker.properties @@ -0,0 +1,28 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:list +invoker.userPropertiesFile.1 = includes.properties + +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:list +invoker.userPropertiesFile.2 = excludes.properties + +invoker.goals.3 = ${project.groupId}:${project.artifactId}:${project.version}:list +invoker.userPropertiesFile.3 = both.properties + +invoker.goals.4 = ${project.groupId}:${project.artifactId}:${project.version}:resolve +invoker.userPropertiesFile.4 = resolve.properties diff --git a/src/it/projects/list-includes-excludes/pom.xml b/src/it/projects/list-includes-excludes/pom.xml new file mode 100644 index 00000000..01cf5849 --- /dev/null +++ b/src/it/projects/list-includes-excludes/pom.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> + +<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>org.apache.maven.plugins.dependency.its</groupId> + <artifactId>list-includes-excludes</artifactId> + <version>1.0-SNAPSHOT</version> + + <description> + dependency:list with the includes and excludes patterns, as for dependency:tree. + </description> + + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>ResourceArtifact</artifactId> + <version>1.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>mdep510-dep</artifactId> + <version>1.0</version> + </dependency> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>version-range</artifactId> + <version>2.0</version> + </dependency> + </dependencies> +</project> diff --git a/src/it/projects/list-includes-excludes/resolve.properties b/src/it/projects/list-includes-excludes/resolve.properties new file mode 100644 index 00000000..78c88184 --- /dev/null +++ b/src/it/projects/list-includes-excludes/resolve.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +outputFile = target/resolve.txt +includeModuleInfo = false +includes = :::2.0 diff --git a/src/it/projects/list-includes-excludes/verify.groovy b/src/it/projects/list-includes-excludes/verify.groovy new file mode 100644 index 00000000..d2e64b3c --- /dev/null +++ b/src/it/projects/list-includes-excludes/verify.groovy @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.assertj.core.api.Assertions.assertThat + +def resolved = { String name -> + new File(basedir, "target/" + name + ".txt").readLines() + .collect { it.trim() } + .findAll { it.startsWith("org.apache.maven.its.dependency:") } + .collect { it.replaceFirst(/^org\.apache\.maven\.its\.dependency:/, "") } +} + +assertThat(resolved("includes")).containsExactlyInAnyOrder("mdep510-dep:jar:1.0:compile", "version-range:jar:2.0:compile") +assertThat(resolved("excludes")).containsExactlyInAnyOrder("mdep510-dep:jar:1.0:compile", "version-range:jar:2.0:compile") +assertThat(resolved("both")).containsExactlyInAnyOrder("ResourceArtifact:jar:1.0:compile", "mdep510-dep:jar:1.0:compile") +assertThat(resolved("resolve")).containsExactly("version-range:jar:2.0:compile") + +return true diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java index 8aa5468c..17fc2eaa 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java @@ -312,6 +312,16 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj */ protected abstract ArtifactsFilter getMarkedArtifactFilter(); + /** + * Return an {@link ArtifactsFilter} applied after the other filters of {@link #getDependencySets}, for goals + * that match artifacts by {@code [groupId]:[artifactId]:[type]:[version]} patterns. + * + * @return the filter, or {@code null} (the default) if the goal has no such patterns + */ + protected ArtifactsFilter getPatternsFilter() { + return null; + } + /** * Tells whether this goal asks Maven for the dependency graph only ({@code requiresDependencyCollection}) and * resolves the artifacts that are left after filtering itself. This way a dependency that the filters drop, for @@ -415,6 +425,11 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj filter.addFilter(new ExcludeReactorProjectsArtifactFilter(session.getProjects(), getLog())); } + ArtifactsFilter patternsFilter = getPatternsFilter(); + if (patternsFilter != null) { + filter.addFilter(patternsFilter); + } + // start with all artifacts. Set<Artifact> artifacts; diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java index 43842114..d09bdc98 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java @@ -44,6 +44,7 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.plugins.dependency.utils.DependencyStatusSets; import org.apache.maven.plugins.dependency.utils.DependencyUtil; import org.apache.maven.plugins.dependency.utils.ResolverUtil; +import org.apache.maven.plugins.dependency.utils.filters.PatternsArtifactsFilter; import org.apache.maven.plugins.dependency.utils.filters.ResolveFileFilter; import org.apache.maven.plugins.dependency.utils.markers.SourcesFileMarkerHandler; import org.apache.maven.project.MavenProject; @@ -120,6 +121,48 @@ public class ResolveDependenciesMojo extends AbstractResolveMojo { @Parameter(property = "outputType", defaultValue = "text") String outputType; + /** + * A comma-separated list of artifact patterns to include in the output, or <code>null</code> not to filter. + * The syntax is the same as for the <code>includes</code> parameter of <code>dependency:tree</code>: + * + * <pre> + * [groupId]:[artifactId]:[type]:[version] + * </pre> + * + * where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty + * pattern segment is treated as an implicit wildcard. + * <p> + * For example, <code>org.apache.*</code> matches all artifacts whose group id starts with + * <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> matches all snapshot artifacts. + * </p> + * The filter applies in addition to the other include and exclude parameters, such as + * <code>includeGroupIds</code>, and the {@link #excludes} patterns take precedence. Unlike in + * <code>dependency:tree</code>, which keeps the path to an included artifact, only the matching artifacts + * are listed, and an excluded artifact does not remove its dependencies. + * <p> + * The property <code>includes</code> is shared with <code>dependency:tree</code>, so one + * <code>-Dincludes</code> value applies to both goals in the same command line. + * </p> + * + * @since 3.12.0 + */ + @Parameter(property = "includes") + private List<String> includes; + + /** + * A comma-separated list of artifact patterns to leave out of the output, or <code>null</code> not to filter. + * The syntax is the same as for the <code>excludes</code> parameter of <code>dependency:tree</code>, see + * {@link #includes}. An artifact that matches an exclude pattern is not listed, even if it matches an include + * pattern. + * <p> + * The property <code>excludes</code> is shared with <code>dependency:tree</code>. + * </p> + * + * @since 3.12.0 + */ + @Parameter(property = "excludes") + private List<String> excludes; + /** * Only used to store results for integration test validation. */ @@ -162,6 +205,11 @@ public class ResolveDependenciesMojo extends AbstractResolveMojo { } // CHECKSTYLE_ON: ParameterNumber + @Override + protected ArtifactsFilter getPatternsFilter() { + return new PatternsArtifactsFilter(includes, excludes); + } + /** * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version. * diff --git a/src/main/java/org/apache/maven/plugins/dependency/utils/filters/PatternsArtifactsFilter.java b/src/main/java/org/apache/maven/plugins/dependency/utils/filters/PatternsArtifactsFilter.java new file mode 100644 index 00000000..e3685776 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/utils/filters/PatternsArtifactsFilter.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.dependency.utils.filters; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter; +import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter; +import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter; +import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; + +/** + * Filter that keeps the artifacts matching the {@code includes} patterns and not matching the {@code excludes} + * patterns. The patterns have the syntax {@code [groupId]:[artifactId]:[type]:[version]} of + * {@link StrictPatternIncludesArtifactFilter}, the same as the {@code includes} and {@code excludes} parameters of + * {@code dependency:tree}. An empty or {@code null} pattern list does not filter. + * + * @since 3.12.0 + */ +public class PatternsArtifactsFilter extends AbstractArtifactsFilter { + + private final StrictPatternIncludesArtifactFilter includesFilter; + + private final StrictPatternExcludesArtifactFilter excludesFilter; + + /** + * @param includes the patterns an artifact must match, or {@code null} or empty to keep every artifact + * @param excludes the patterns an artifact must not match, or {@code null} or empty to remove no artifact + */ + public PatternsArtifactsFilter(Collection<String> includes, Collection<String> excludes) { + this.includesFilter = includes == null || includes.isEmpty() + ? null + : new StrictPatternIncludesArtifactFilter(new ArrayList<>(includes)); + this.excludesFilter = excludes == null || excludes.isEmpty() + ? null + : new StrictPatternExcludesArtifactFilter(new ArrayList<>(excludes)); + } + + @Override + public Set<Artifact> filter(Set<Artifact> artifacts) throws ArtifactFilterException { + Set<Artifact> result = new LinkedHashSet<>(); + for (Artifact artifact : artifacts) { + if ((excludesFilter == null || excludesFilter.include(artifact)) + && (includesFilter == null || includesFilter.include(artifact))) { + result.add(artifact); + } + } + return result; + } +} diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm index 246e6a0d..ba98cbd9 100644 --- a/src/site/markdown/usage.md.vm +++ b/src/site/markdown/usage.md.vm @@ -368,6 +368,13 @@ The `outputType` parameter (since 3.12.0) of `dependency:resolve`, `dependency:l mvn dependency:list -DoutputType=pom -DoutputFile=dependencies.xml ``` +The `includes` and `excludes` parameters (since 3.12.0) of `dependency:resolve` and `dependency:list` take the same comma-separated `[groupId]:[artifactId]:[type]:[version]` patterns as the parameters of `dependency:tree`, with `*` wildcards. They apply in addition to the other `include*` and `exclude*` parameters, and an exclusion wins over an inclusion. The goals list only the matching artifacts; unlike `dependency:tree`, they do not keep the path to an included artifact. + +``` +mvn dependency:list -Dincludes=org.springframework.* +mvn dependency:list -Dincludes=:::*-SNAPSHOT -Dexcludes=org.acme +``` + `dependency:resolve-sources` ---------------------------- diff --git a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/PatternsArtifactsFilterTest.java b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/PatternsArtifactsFilterTest.java new file mode 100644 index 00000000..6720d7cd --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/PatternsArtifactsFilterTest.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.dependency.utils.filters; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.apache.maven.artifact.versioning.VersionRange; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PatternsArtifactsFilterTest { + + private final Artifact spring = artifact("org.springframework", "spring-core", "6.1.0"); + + private final Artifact snapshot = artifact("org.acme", "lib", "1.0-SNAPSHOT"); + + private final Artifact guava = artifact("com.google.guava", "guava", "33.0.0"); + + private Set<Artifact> all() { + return new LinkedHashSet<>(Arrays.asList(spring, snapshot, guava)); + } + + private static Artifact artifact(String groupId, String artifactId, String version) { + return new DefaultArtifact( + groupId, + artifactId, + VersionRange.createFromVersion(version), + "compile", + "jar", + null, + new DefaultArtifactHandler("jar")); + } + + @Test + void noPatternsKeepEverything() throws Exception { + assertEquals(all(), new PatternsArtifactsFilter(null, Collections.emptyList()).filter(all())); + } + + @Test + void includesKeepMatchingGroupId() throws Exception { + Set<Artifact> result = new PatternsArtifactsFilter(Arrays.asList("org.springframework"), null).filter(all()); + assertEquals(Collections.singleton(spring), result); + } + + @Test + void includesMatchSnapshotVersions() throws Exception { + Set<Artifact> result = new PatternsArtifactsFilter(Arrays.asList(":::*-SNAPSHOT"), null).filter(all()); + assertEquals(Collections.singleton(snapshot), result); + } + + @Test + void excludesRemoveMatching() throws Exception { + Set<Artifact> result = new PatternsArtifactsFilter(null, Arrays.asList("org.*")).filter(all()); + assertEquals(Collections.singleton(guava), result); + } + + @Test + void excludeTakesPrecedenceOverInclude() throws Exception { + Set<Artifact> result = + new PatternsArtifactsFilter(Arrays.asList("org.*"), Arrays.asList("org.acme")).filter(all()); + assertEquals(Collections.singleton(spring), result); + } +}
