You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "elharo (via GitHub)" <gi...@apache.org> on 2023/07/14 12:12:45 UTC

[GitHub] [maven] elharo commented on a diff in pull request #1200: [MNG-7843] Allow glob patterns in dependency exclusions

elharo commented on code in PR #1200:
URL: https://github.com/apache/maven/pull/1200#discussion_r1263664946


##########
maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java:
##########
@@ -28,37 +30,120 @@
  * Filter to exclude from a list of artifact patterns.
  */
 public class ExclusionArtifactFilter implements ArtifactFilter {
-    private static final String WILDCARD = "*";
 
     private final List<Exclusion> exclusions;
+    private final List<Predicate<Artifact>> predicates;
 
     public ExclusionArtifactFilter(List<Exclusion> exclusions) {
         this.exclusions = exclusions;
+        this.predicates =
+                exclusions.stream().map(ExclusionArtifactFilter::toPredicate).collect(Collectors.toList());
     }
 
-    private Predicate<Exclusion> sameArtifactId(Artifact artifact) {
-        return exclusion -> exclusion.getArtifactId().equals(artifact.getArtifactId());
+    @Override
+    public boolean include(Artifact artifact) {
+        return predicates.stream().noneMatch(p -> p.test(artifact));
     }
 
-    private Predicate<Exclusion> sameGroupId(Artifact artifact) {
-        return exclusion -> exclusion.getGroupId().equals(artifact.getGroupId());
+    private static Predicate<Artifact> toPredicate(Exclusion exclusion) {
+        Pattern groupId = Pattern.compile(convertGlobToRegex(exclusion.getGroupId()));
+        Pattern artifactId = Pattern.compile(convertGlobToRegex(exclusion.getArtifactId()));
+        Predicate<Artifact> predGroupId = a -> groupId.matcher(a.getGroupId()).matches();
+        Predicate<Artifact> predArtifactId =
+                a -> artifactId.matcher(a.getArtifactId()).matches();
+        return predGroupId.and(predArtifactId);
     }
 
-    private Predicate<Exclusion> groupIdIsWildcard = exclusion -> WILDCARD.equals(exclusion.getGroupId());
-
-    private Predicate<Exclusion> artifactIdIsWildcard = exclusion -> WILDCARD.equals(exclusion.getArtifactId());
-
-    private Predicate<Exclusion> groupIdAndArtifactIdIsWildcard = groupIdIsWildcard.and(artifactIdIsWildcard);
-
-    private Predicate<Exclusion> exclude(Artifact artifact) {
-        return groupIdAndArtifactIdIsWildcard
-                .or(groupIdIsWildcard.and(sameArtifactId(artifact)))
-                .or(artifactIdIsWildcard.and(sameGroupId(artifact)))
-                .or(sameGroupId(artifact).and(sameArtifactId(artifact)));
-    }
-
-    @Override
-    public boolean include(Artifact artifact) {
-        return !exclusions.stream().anyMatch(exclude(artifact));
+    /**
+     * Converts a standard POSIX Shell globbing pattern into a regular expression

Review Comment:
   This is unnecessary. There are already globstar patterns in the JDK. See  PathMatcher.getMatcher("glob")
   
   And there's glob support in maven-shared-utils that predates this being added to the JDK.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org