@@ -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}
0 commit comments