diff --git a/cli/common/src/main/java/io/helidon/build/cli/common/SemVer.java b/cli/common/src/main/java/io/helidon/build/cli/common/SemVer.java index daa814e61..e6ca0db17 100644 --- a/cli/common/src/main/java/io/helidon/build/cli/common/SemVer.java +++ b/cli/common/src/main/java/io/helidon/build/cli/common/SemVer.java @@ -35,7 +35,7 @@ */ final class SemVer { - private static final Pattern MAJOR_PATTERN = Pattern.compile("^(?[0-9]+).+$"); + private static final Pattern MAJOR_PATTERN = Pattern.compile("^(?[0-9]+)(.*)"); private SemVer() { } @@ -49,14 +49,14 @@ private SemVer() { static List latestMajorVersions(List versions) { List ids = versions.stream().map(ArchetypesData.Version::id).collect(Collectors.toList()); // versions grouped by major digit - Map> groups = Lists.mappedBy(ids, SemVer::majorDigit); + Map> groups = Lists.mappedBy(ids, SemVer::lowerVersionFromMajorDigit); // Maven versions grouped by version range Map> ranges = Maps.mapEntry(groups, Strings::isValid, VersionRange::higherOrEqual, MavenVersion::toMavenVersion); // the latest of each group - Collection latest = Maps.mapEntryValue(ranges, entry->entry.getKey().resolveLatest(entry.getValue())) + Collection latest = Maps.mapEntryValue(ranges, entry -> entry.getKey().resolveLatest(entry.getValue())) .values(); List latestVersions = versions.stream() .filter(version -> latest.contains(version.toMavenVersion())) @@ -72,10 +72,10 @@ static List sortVersions(List ve return versions; } - private static String majorDigit(String version) { + private static String lowerVersionFromMajorDigit(String version) { Matcher matcher = MAJOR_PATTERN.matcher(version); if (matcher.find()) { - return matcher.group("major"); + return matcher.group("major") + "-SNAPSHOT"; } return ""; } diff --git a/cli/common/src/test/java/io/helidon/build/cli/common/ArchetypesDataTest.java b/cli/common/src/test/java/io/helidon/build/cli/common/ArchetypesDataTest.java index 169e47fb4..743ba3f64 100644 --- a/cli/common/src/test/java/io/helidon/build/cli/common/ArchetypesDataTest.java +++ b/cli/common/src/test/java/io/helidon/build/cli/common/ArchetypesDataTest.java @@ -88,6 +88,30 @@ public void testVersionOrdering() { Matchers.is(List.of("2.0.0", "1.0.1", "4.0.0"))); } + @Test + public void testVersionQualifierOrdering() { + String version1; + String version2; + List versions = List.of( + "0-SNAPSHOT", + "0-ALPHA", + "0-BETA", + "0-MILESTONE", + "0-RC", + "0", + "0-sp"); + + for (int i = 0; i < versions.size() - 1; i += 2) { + version1 = versions.get(i); + version2 = versions.get(i + 1); + assertThat(String.format("%s should be picked over %s", version1, version2), + data(version(version1, 100), + version(version2, 100)) + .latestMajorVersions(), + Matchers.is(List.of(version2))); + } + } + private static ArchetypesData data(ArchetypesData.Version... versions) { return ArchetypesData.builder().versions(versions).build(); } diff --git a/common/common/src/main/java/io/helidon/build/common/Maps.java b/common/common/src/main/java/io/helidon/build/common/Maps.java index 75adae29c..e302b56e6 100644 --- a/common/common/src/main/java/io/helidon/build/common/Maps.java +++ b/common/common/src/main/java/io/helidon/build/common/Maps.java @@ -432,9 +432,9 @@ public static Map> mapEntry( Function keyMapper, Function valueMapper) { return map.entrySet().stream() - .filter(entry-> keyFilter.test(entry.getKey())) + .filter(entry -> keyFilter.test(entry.getKey())) .collect(Collectors.toMap( - entry->keyMapper.apply(entry.getKey()), - entry->entry.getValue().stream().map(valueMapper).collect(Collectors.toList()))); + entry -> keyMapper.apply(entry.getKey()), + entry -> entry.getValue().stream().map(valueMapper).collect(Collectors.toList()))); } } diff --git a/common/maven/src/main/java/io/helidon/build/common/maven/ComparableVersion.java b/common/maven/src/main/java/io/helidon/build/common/maven/ComparableVersion.java index bf4d35f01..d50099ff4 100644 --- a/common/maven/src/main/java/io/helidon/build/common/maven/ComparableVersion.java +++ b/common/maven/src/main/java/io/helidon/build/common/maven/ComparableVersion.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -259,11 +259,11 @@ public String toString() { private static class StringItem implements Item { private static final List QUALIFIERS = Arrays.asList( + "snapshot", "alpha", "beta", "milestone", "rc", - "snapshot", "", "sp"); diff --git a/common/maven/src/test/java/io/helidon/build/common/maven/MavenVersionTest.java b/common/maven/src/test/java/io/helidon/build/common/maven/MavenVersionTest.java index f416f4c3b..faf475b37 100644 --- a/common/maven/src/test/java/io/helidon/build/common/maven/MavenVersionTest.java +++ b/common/maven/src/test/java/io/helidon/build/common/maven/MavenVersionTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package io.helidon.build.common.maven; +import java.util.List; + import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -82,4 +84,25 @@ void testReleaseCandidateVersions() { assertThat(toMavenVersion("2.0.0-RC2"), is(org.hamcrest.Matchers.greaterThan(toMavenVersion("2.0.0-RC1")))); assertThat(toMavenVersion("2.0.0"), is(org.hamcrest.Matchers.greaterThan(toMavenVersion("2.0.0-RC2")))); } + + @Test + void testQualifierComparison() { + MavenVersion version1; + MavenVersion version2; + List versions = List.of( + toMavenVersion("0-SNAPSHOT"), + toMavenVersion("0-ALPHA"), + toMavenVersion("0-BETA"), + toMavenVersion("0-MILESTONE"), + toMavenVersion("0-RC"), + toMavenVersion("0"), + toMavenVersion("0-sp")); + + for (int i = 0; i < versions.size() - 1; i += 2) { + version1 = versions.get(i); + version2 = versions.get(i + 1); + assertThat(String.format("%s should be lower than %s", version1, version2), + version1, is(org.hamcrest.Matchers.lessThan(version2))); + } + } }