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


The following commit(s) were added to refs/heads/master by this push:
     new e2e5e91e [MDEP-954] includeGroupIds/excludeGroupIds should not match 
unrelated group ids sharing a string prefix
e2e5e91e is described below

commit e2e5e91e7ed7898175541c70ba913d14bfc437d0
Author: Arturo Bernal <[email protected]>
AuthorDate: Thu Oct 29 09:07:12 2026 +0100

    [MDEP-954] includeGroupIds/excludeGroupIds should not match unrelated group 
ids sharing a string prefix
    
    A group id such as org.example incorrectly also matched org.example2
    because the underlying filter did a plain startsWith comparison. It
    now only matches org.example itself and group ids nested under it,
    e.g. org.example.foo, through a GroupIdFilter local to the plugin. A
    new groupIdsExactMatch parameter lets includeGroupIds/excludeGroupIds
    match a group id exactly, the same way includeArtifactIds does.
---
 .../AbstractDependencyFilterMojo.java              | 16 +++-
 .../dependency/resolvers/GoOfflineMojo.java        |  5 +-
 .../dependency/utils/filters/GroupIdFilter.java    | 76 +++++++++++++++++++
 .../fromDependencies/TestCopyDependenciesMojo.java | 39 ++++++++++
 .../utils/filters/TestGroupIdFilter.java           | 85 ++++++++++++++++++++++
 5 files changed, 217 insertions(+), 4 deletions(-)

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 4d1428b5..8aa5468c 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
@@ -45,6 +45,7 @@ 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.ExcludeOptionalArtifactsFilter;
+import org.apache.maven.plugins.dependency.utils.filters.GroupIdFilter;
 import 
org.apache.maven.plugins.dependency.utils.translators.ArtifactTranslator;
 import 
org.apache.maven.plugins.dependency.utils.translators.ClassifierTypeTranslator;
 import org.apache.maven.project.DefaultProjectBuildingRequest;
@@ -57,7 +58,6 @@ import 
org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
 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.ProjectTransitivityFilter;
 import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
 import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
@@ -227,6 +227,17 @@ public abstract class AbstractDependencyFilterMojo extends 
AbstractDependencyMoj
     @Parameter(property = "includeGroupIds", defaultValue = "")
     protected String includeGroupIds;
 
+    /**
+     * Whether {@code includeGroupIds}/{@code excludeGroupIds} only match a 
listed group id exactly, the same way
+     * {@code includeArtifactIds}/{@code excludeArtifactIds} match artifact 
ids. By default, a listed group id such
+     * as {@code org.example} also matches every group id nested under it, 
such as {@code org.example.foo}, but
+     * never an unrelated group id that merely shares the same string prefix, 
such as {@code org.example2}.
+     *
+     * @since 3.12.0
+     */
+    @Parameter(property = "groupIdsExactMatch", defaultValue = "false")
+    protected boolean groupIdsExactMatch;
+
     /**
      * Directory to store flag files.
      *
@@ -389,7 +400,8 @@ public abstract class AbstractDependencyFilterMojo extends 
AbstractDependencyMoj
 
         filter.addFilter(new GroupIdFilter(
                 DependencyUtil.cleanToBeTokenizedString(this.includeGroupIds),
-                
DependencyUtil.cleanToBeTokenizedString(this.excludeGroupIds)));
+                DependencyUtil.cleanToBeTokenizedString(this.excludeGroupIds),
+                this.groupIdsExactMatch));
 
         filter.addFilter(new ArtifactIdFilter(
                 
DependencyUtil.cleanToBeTokenizedString(this.includeArtifactIds),
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java
index da0726d3..7fe66745 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java
@@ -44,6 +44,7 @@ import org.apache.maven.plugins.annotations.Parameter;
 import 
org.apache.maven.plugins.dependency.fromDependencies.AbstractDependencyFilterMojo;
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.plugins.dependency.utils.ResolverUtil;
+import org.apache.maven.plugins.dependency.utils.filters.GroupIdFilter;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectBuilder;
 import 
org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
@@ -51,7 +52,6 @@ import 
org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
 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;
 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
@@ -292,7 +292,8 @@ public class GoOfflineMojo extends 
AbstractDependencyFilterMojo {
 
         filter.addFilter(new GroupIdFilter(
                 DependencyUtil.cleanToBeTokenizedString(this.includeGroupIds),
-                
DependencyUtil.cleanToBeTokenizedString(this.excludeGroupIds)));
+                DependencyUtil.cleanToBeTokenizedString(this.excludeGroupIds),
+                this.groupIdsExactMatch));
 
         filter.addFilter(new ArtifactIdFilter(
                 
DependencyUtil.cleanToBeTokenizedString(this.includeArtifactIds),
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/utils/filters/GroupIdFilter.java
 
b/src/main/java/org/apache/maven/plugins/dependency/utils/filters/GroupIdFilter.java
new file mode 100644
index 00000000..babf212e
--- /dev/null
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/utils/filters/GroupIdFilter.java
@@ -0,0 +1,76 @@
+/*
+ * 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 org.apache.maven.artifact.Artifact;
+import 
org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter;
+
+/**
+ * Filters artifacts by group id. Unlike {@code 
org.apache.maven.shared.artifact.filter.collection.GroupIdFilter},
+ * a group id such as {@code org.example} matches artifacts whose group id is 
{@code org.example} or is nested
+ * under it, such as {@code org.example.foo}, but never an unrelated group id 
that merely shares the same string
+ * prefix, such as {@code org.example2}.
+ *
+ * @since 3.12.0
+ */
+public class GroupIdFilter extends AbstractArtifactFeatureFilter {
+
+    private final boolean exactMatch;
+
+    /**
+     * Creates a filter that matches a group id and every group id nested 
under it.
+     *
+     * @param include comma separated list of group ids to include, may be 
{@code null}
+     * @param exclude comma separated list of group ids to exclude, may be 
{@code null}
+     */
+    public GroupIdFilter(final String include, final String exclude) {
+        this(include, exclude, false);
+    }
+
+    /**
+     * Creates a filter that matches either a group id and every group id 
nested under it, or, with
+     * {@code exactMatch}, only the group id itself.
+     *
+     * @param include comma separated list of group ids to include, may be 
{@code null}
+     * @param exclude comma separated list of group ids to exclude, may be 
{@code null}
+     * @param exactMatch {@code true} to only match a listed group id exactly, 
the same way
+     *            {@code includeArtifactIds}/{@code excludeArtifactIds} match 
artifact ids; {@code false} to also
+     *            match every group id nested under a listed one, so that 
{@code org.example} matches
+     *            {@code org.example.foo}
+     */
+    public GroupIdFilter(final String include, final String exclude, final 
boolean exactMatch) {
+        super(include, exclude);
+        this.exactMatch = exactMatch;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected String getArtifactFeature(final Artifact artifact) {
+        return artifact.getGroupId();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected boolean compareFeatures(final String lhs, final String rhs) {
+        if (exactMatch) {
+            return lhs.equals(rhs);
+        }
+        return lhs.equals(rhs) || lhs.startsWith(rhs + ".");
+    }
+}
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
index ad0fb2c9..39bceac9 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java
@@ -396,6 +396,45 @@ class TestCopyDependenciesMojo {
         }
     }
 
+    @Test
+    @InjectMojo(goal = "copy-dependencies")
+    void 
testIncludeGroupIdDoesNotMatchUnrelatedGroupIdSharingStringPrefix(CopyDependenciesMojo
 mojo) throws Exception {
+        Set<Artifact> artifacts = new HashSet<>();
+        artifacts.add(stubFactory.createArtifact("org.example", "one", "1.0"));
+        artifacts.add(stubFactory.createArtifact("org.example.foo", "two", 
"1.0"));
+        artifacts.add(stubFactory.createArtifact("org.example2", "three", 
"1.0"));
+        mojo.getProject().setArtifacts(artifacts);
+
+        // nested under org.example is included, an unrelated group id sharing 
the same string prefix is not
+        mojo.includeGroupIds = "org.example";
+        mojo.execute();
+
+        for (Artifact artifact : artifacts) {
+            String fileName = DependencyUtil.getFormattedFileName(artifact, 
false);
+            File file = new File(mojo.outputDirectory, fileName);
+            assertEquals(!"org.example2".equals(artifact.getGroupId()), 
file.exists());
+        }
+    }
+
+    @Test
+    @InjectMojo(goal = "copy-dependencies")
+    void 
testIncludeGroupIdExactMatchOnlyMatchesTheListedGroupId(CopyDependenciesMojo 
mojo) throws Exception {
+        Set<Artifact> artifacts = new HashSet<>();
+        artifacts.add(stubFactory.createArtifact("org.example", "one", "1.0"));
+        artifacts.add(stubFactory.createArtifact("org.example.foo", "two", 
"1.0"));
+        mojo.getProject().setArtifacts(artifacts);
+
+        mojo.includeGroupIds = "org.example";
+        mojo.groupIdsExactMatch = true;
+        mojo.execute();
+
+        for (Artifact artifact : artifacts) {
+            String fileName = DependencyUtil.getFormattedFileName(artifact, 
false);
+            File file = new File(mojo.outputDirectory, fileName);
+            assertEquals("org.example".equals(artifact.getGroupId()), 
file.exists());
+        }
+    }
+
     @Test
     @InjectMojo(goal = "copy-dependencies")
     void testExcludeClassifier(CopyDependenciesMojo mojo) throws Exception {
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestGroupIdFilter.java
 
b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestGroupIdFilter.java
new file mode 100644
index 00000000..04675e3b
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/utils/filters/TestGroupIdFilter.java
@@ -0,0 +1,85 @@
+/*
+ * 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 java.util.TreeSet;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.testing.stubs.ArtifactStub;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class TestGroupIdFilter {
+
+    private static Artifact groupId(final String groupId) {
+        final ArtifactStub artifact = new ArtifactStub();
+        artifact.setGroupId(groupId);
+        artifact.setArtifactId("artifact-" + groupId);
+        artifact.setVersion("1.0");
+        return artifact;
+    }
+
+    private static Set<Artifact> artifacts(final String... groupIds) {
+        final Set<Artifact> result = new LinkedHashSet<>();
+        for (final String groupId : groupIds) {
+            result.add(groupId(groupId));
+        }
+        return result;
+    }
+
+    private static Set<String> groupIdsOf(final Set<Artifact> artifacts) {
+        final Set<String> result = new TreeSet<>();
+        for (final Artifact artifact : artifacts) {
+            result.add(artifact.getGroupId());
+        }
+        return result;
+    }
+
+    @Test
+    void includeDoesNotMatchUnrelatedGroupIdSharingStringPrefix() {
+        final GroupIdFilter filter = new GroupIdFilter("org.example", "");
+
+        final Set<Artifact> result = filter.filter(artifacts("org.example", 
"org.example.foo", "org.example2"));
+
+        assertEquals(new TreeSet<>(Arrays.asList("org.example", 
"org.example.foo")), groupIdsOf(result));
+    }
+
+    @Test
+    void excludeDoesNotMatchUnrelatedGroupIdSharingStringPrefix() {
+        final GroupIdFilter filter = new GroupIdFilter("", "org.example");
+
+        final Set<Artifact> result = filter.filter(artifacts("org.example", 
"org.example.foo", "org.example2"));
+
+        assertEquals(new TreeSet<>(Collections.singleton("org.example2")), 
groupIdsOf(result));
+    }
+
+    @Test
+    void exactMatchOnlyMatchesTheListedGroupId() {
+        final GroupIdFilter filter = new GroupIdFilter("org.example", "", 
true);
+
+        final Set<Artifact> result = filter.filter(artifacts("org.example", 
"org.example.foo", "org.example2"));
+
+        assertEquals(new TreeSet<>(Collections.singleton("org.example")), 
groupIdsOf(result));
+    }
+}

Reply via email to