Skip to content

Commit cca73a7

Browse files
committed
Distinguish versions vs. classifiers more robustly
This fixes the failing do-not-delete-natives test after the earlier "Simplify encroaching version logic" commit broke it. Finally now, the case of jinput-2.0.9.jar and jinput-2.0.9-natives-all.jar is handled correctly: the "natives-all" classifier is recognized properly, so they are treated as separate artifacts, avoiding the spurious behavior: [INFO] Copying jinput-2.0.9.jar to Fiji.app/jars [INFO] Deleted overridden jinput-2.0.9.jar [INFO] Copying jinput-2.0.9-natives-all.jar to Fiji.app/jars
1 parent 7733263 commit cca73a7

File tree

3 files changed

+89
-13
lines changed

3 files changed

+89
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.scijava</groupId>
77
<artifactId>pom-scijava</artifactId>
8-
<version>26.0.0</version>
8+
<version>28.0.0</version>
99
<relativePath />
1010
</parent>
1111

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

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ else if (isBioFormatsArtifact(artifact)) {
230230
final String majorVersionToInstall = majorVersion(toInstall);
231231
final String majorVersionOther = majorVersion(otherVersion);
232232
if (!majorVersionToInstall.equals(majorVersionOther)) {
233-
getLog().warn("Version " + otherVersion + " of " + artifact +
234-
" is incompatible according to SemVer: " +
235-
majorVersionToInstall + " != " + majorVersionOther);
233+
getLog().warn("Version " + versionToString(otherVersion) + //
234+
" of " + artifact + " is incompatible according to SemVer: " +
235+
majorVersionToInstall + " != " + //
236+
versionToString(majorVersionOther));
236237
}
237238
if (newerVersion) break;
238239
//$FALL-THROUGH$
@@ -260,6 +261,10 @@ else if (newerVersion) {
260261
}
261262
}
262263

264+
private static String versionToString(final String v) {
265+
return v == null || v.isEmpty() ? "(none)" : v;
266+
}
267+
263268
private static boolean isBioFormatsArtifact(final Artifact artifact) {
264269
final String fileName = artifact.getFile().getName();
265270
return "ome".equals(artifact.getGroupId()) ||
@@ -343,19 +348,40 @@ private Map<Path, String> getEncroachingVersions(final Path directory,
343348
// artifactId-version-classifier.type
344349
//
345350
// with '-classifier' absent for the main classifier.
346-
final String classifier = artifact.getClassifier();
347-
final String patternString = artifact.getArtifactId() + "-?(.*)" +
348-
(classifier != null && !classifier.isEmpty() ? "-" + classifier : "") +
349-
"\\." + artifact.getType();
351+
352+
final String artifactPattern = Pattern.quote(artifact.getArtifactId());
353+
354+
final String normalVersion = "[0-9].*?"; // begin with a digit (non-greedy)
355+
final String jitpackVersion = "[0-9a-f]{5}[0-9a-f]*"; // git hash
356+
final String versionPattern = "(-" + normalVersion + "|-" + jitpackVersion + ")?";
357+
358+
// For the main artifact, matching is tricky, because we don't want to
359+
// delete artifacts of other classifiers. For example, for lib-1.2.3.jar,
360+
// we _do_ want to delete lib-1.2.3-beta-1.jar (i.e. v1.2.3-beta-1), but
361+
// _not_ lib-1.2.3-natives-macosx.jar (i.e. classifier natives-macosx).
362+
// Unfortunately, we cannot easily tell these cases apart. :-(
363+
// So we hardcode known classifiers into the regex as a heuristic.
364+
final String rawClassifier = artifact.getClassifier();
365+
final String classifier = rawClassifier == null ? "" : rawClassifier;
366+
final List<String> classifiers = new ArrayList<>();
367+
classifiers.add(classifier);
368+
classifiers.addAll(KnownPlatforms.nativeClassifiers());
369+
final String[] quotedClassifiers = classifiers.stream() //
370+
.map(c -> c.isEmpty() ? "" : "-" + Pattern.quote(c)) //
371+
.toArray(String[]::new);
372+
final String classifierPattern = //
373+
"(" + String.join("|", quotedClassifiers) + ")?";
374+
375+
final String typePattern = Pattern.quote("." + artifact.getType());
376+
377+
final String patternString = artifactPattern + //
378+
versionPattern + classifierPattern + typePattern;
350379
final Pattern pattern = Pattern.compile(patternString);
351380

352381
try {
353382
Files.walk(directory, 1).forEach(path -> {
354-
final Matcher m = pattern.matcher(path.getFileName().toString());
355-
if (m.matches()) {
356-
final String version = m.group(1);
357-
result.put(path, version);
358-
}
383+
final String version = getEncroachingVersion(path, pattern, classifier);
384+
if (version != null) result.put(path, version);
359385
});
360386
}
361387
catch (IOException e) {
@@ -364,4 +390,23 @@ private Map<Path, String> getEncroachingVersions(final Path directory,
364390

365391
return result;
366392
}
393+
394+
private static String getEncroachingVersion(final Path path,
395+
final Pattern pattern, final String expectedClassifier)
396+
{
397+
final Matcher m = pattern.matcher(path.getFileName().toString());
398+
if (!m.matches()) return null; // not a candidate for a clash
399+
400+
final String classifier = trimLeadingDash(m.group(2));
401+
if (!expectedClassifier.equals(classifier)) return null;
402+
403+
final String version = trimLeadingDash(m.group(1));
404+
return version;
405+
}
406+
407+
private static String trimLeadingDash(final String s) {
408+
if (s == null) return "";
409+
if (s.startsWith("-")) return s.substring(1);
410+
return s;
411+
}
367412
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ public static String shortName(final String family, final String arch) {
7676
return null;
7777
}
7878

79+
/**
80+
* Gets a list of {@link SubdirectoryPattern}s corresponding to known native
81+
* classifiers. This list includes the following patterns:
82+
* <ul>
83+
* <li>{@code <family>-<arch>}</li>
84+
* <li>{@code native-<family>-<arch>}</li>
85+
* <li>{@code natives-<family>-<arch>}</li>
86+
* <li>{@code <family>}</li>
87+
* <li>{@code native-<family>}</li>
88+
* <li>{@code natives-<family>}</li>
89+
* </ul>
90+
* <p>
91+
* For every {@code <family>} in {@link #FAMILIES} and every {@code <arch>} in
92+
* {@link #ARCHES}.
93+
* </p>
94+
*/
95+
public static List<String> nativeClassifiers() {
96+
final List<String> classifiers = new ArrayList<>();
97+
for (final String family : FAMILIES) {
98+
for (final String arch : ARCHES) {
99+
classifiers.add(family + "-" + arch);
100+
classifiers.add("native-" + family + "-" + arch);
101+
classifiers.add("natives-" + family + "-" + arch);
102+
}
103+
classifiers.add(family);
104+
classifiers.add("native-" + family);
105+
classifiers.add("natives-" + family);
106+
}
107+
return classifiers;
108+
}
109+
79110
/**
80111
* Gets a list of {@link SubdirectoryPattern}s corresponding to known native
81112
* classifiers. E.g.:

0 commit comments

Comments
 (0)