Skip to content

Commit d99e14b

Browse files
committed
SubdirectoryPattern: expand logic to support GAVCP
Rather than only supporting exact classifier matches, let's support any combination of G, A, V, C or P, as well as wildcards and limited regex.
1 parent a1fa5c4 commit d99e14b

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

src/main/java/org/scijava/maven/plugin/install/SubdirectoryPattern.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,78 @@
4040
*/
4141
public class SubdirectoryPattern {
4242

43+
/** The subdirectory into which matching artifacts should be installed. */
4344
public String subdirectory;
45+
46+
/**
47+
* List of pattern strings. An artifact matching any of these patterns will be
48+
* installed into the affiliated subdirectory. Valid pattern syntaxes are:
49+
* <ol>
50+
* <li>classifier</li>
51+
* <li>groupId:artifactId</li>
52+
* <li>groupId:artifactId:version</li>
53+
* <li>groupId:artifactId:version:classifier</li>
54+
* <li>groupId:artifactId:version:classifier:packaging</li>
55+
* </ol>
56+
* <p>
57+
* Additionally, the wildcard character ({@code *}) is allowed and matches
58+
* anything. For example, the pattern {@code org.scijava:*:*:natives-*}
59+
* would match any artifact with groupId {@code org.scijava} and classifier
60+
* beginning with {@code natives-}.
61+
* </p>
62+
*/
4463
public List<String> patterns;
4564

65+
/** Returns true iff this pattern matches the given artifact. */
4666
public boolean matches(final Artifact artifact) {
47-
return patterns.contains(artifact.getClassifier());
67+
return patterns.stream().anyMatch(pattern -> matches(artifact, pattern));
68+
}
69+
70+
private static boolean matches(final Artifact artifact, final String pattern) {
71+
final String[] tokens = pattern.split(":");
72+
final String g, a, v, c, p;
73+
if (tokens.length == 1) {
74+
g = a = v = p = "*";
75+
c = tokens[0];
76+
}
77+
else if (tokens.length == 2) {
78+
g = tokens[0];
79+
a = tokens[1];
80+
v = c = p = "*";
81+
}
82+
else if (tokens.length == 3) {
83+
g = tokens[0];
84+
a = tokens[1];
85+
v = tokens[2];
86+
c = p = "*";
87+
}
88+
else if (tokens.length == 4) {
89+
g = tokens[0];
90+
a = tokens[1];
91+
v = tokens[2];
92+
c = tokens[3];
93+
p = "*";
94+
}
95+
else if (tokens.length == 5) {
96+
g = tokens[0];
97+
a = tokens[1];
98+
v = tokens[2];
99+
c = tokens[3];
100+
p = tokens[4];
101+
}
102+
else {
103+
throw new IllegalArgumentException("Invalid subdirectory pattern: " + pattern);
104+
}
105+
return matches(artifact.getGroupId(), g) && //
106+
matches(artifact.getArtifactId(), a) && //
107+
matches(artifact.getVersion(), v) && //
108+
matches(artifact.getClassifier(), c) && //
109+
matches(artifact.getType(), p);
110+
}
111+
112+
private static boolean matches(final String string, final String pattern) {
113+
final String s = string == null ? "" : string;
114+
final String regex = pattern.replaceAll("\\*", ".*");
115+
return s.matches(regex);
48116
}
49117
}

0 commit comments

Comments
 (0)