Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.toolchain;

import java.util.regex.Pattern;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
Expand Down Expand Up @@ -66,7 +68,7 @@ private VersionMatcher(String version) {
@Override
public boolean matches(String requirement) {
try {
VersionRange range = VersionRange.createFromVersionSpec(requirement);
VersionRange range = convertRequirementToVersionRange(requirement);
if (range.hasRestrictions()) {
return range.containsVersion(version);
} else {
Expand All @@ -79,6 +81,29 @@ public boolean matches(String requirement) {
}
}

private VersionRange convertRequirementToVersionRange(String requirement)
throws InvalidVersionSpecificationException {
// Specific for Version _requirement_ matching;
// If the version is a simple integer (like "25")
// then treat this as the requirement "the major version is 25"
if (Pattern.matches("^[0-9]+$", requirement)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move pattern compile to static filed

int majorVersion = Integer.parseInt(requirement);
return VersionRange.createFromVersionSpec("[" + majorVersion + "," + (majorVersion + 1) + ")");
}

// If the version is a major.minor (like "1.5")
// then treat this as the requirement "the major version is 1 and the minor is 5"
if (Pattern.matches("^[0-9]\\.[0-9]+$", requirement)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: the major version part is [0-9] (single digit only), so a requirement like "21.3" would silently fall through to the default path and be treated as an exact match. It should be [0-9]+ to mirror the first pattern.

Suggested change
if (Pattern.matches("^[0-9]\\.[0-9]+$", requirement)) {
if (Pattern.matches("^[0-9]+\\.[0-9]+$", requirement)) {

Also, as @slawekjaranowski mentioned, both patterns should be pre-compiled into private static final Pattern fields to avoid recompilation on every call.

String[] split = requirement.split("\\.", 2);
int majorVersion = Integer.parseInt(split[0]);
int minorVersion = Integer.parseInt(split[1]);
return VersionRange.createFromVersionSpec(
"[" + majorVersion + "." + minorVersion + "," + majorVersion + "." + (minorVersion + 1) + ")");
}

return VersionRange.createFromVersionSpec(requirement);
}

@Override
public String toString() {
return version.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ public void testCreateExactMatcher() {
public void testCreateVersionMatcher() {
RequirementMatcher matcher;
matcher = RequirementMatcherFactory.createVersionMatcher("1.5.2");
assertFalse(matcher.matches("1.5"));
assertTrue(matcher.matches("1.5.2"));
assertTrue(matcher.matches("1")); // Major matches
assertTrue(matcher.matches("1.5")); // Major.Minor matches
assertTrue(matcher.matches("1.5.2")); // Full match
assertFalse(matcher.matches("1.6")); // Wrong minor
assertFalse(matcher.matches("2")); // Wrong major

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding tests for multi-digit major versions to catch the regex bug above, e.g.:

matcher = RequirementMatcherFactory.createVersionMatcher("21.0.2");
assertTrue(matcher.matches("21"));     // major-only
assertTrue(matcher.matches("21.0"));   // major.minor
assertFalse(matcher.matches("21.1")); // wrong minor
assertFalse(matcher.matches("2"));     // wrong major

assertFalse(matcher.matches("2.5")); // Wrong major, right minor
assertFalse(matcher.matches("[1.4,1.5)"));
assertFalse(matcher.matches("[1.5,1.5.2)"));
assertTrue(matcher.matches("[1.5,1.5.3)"));
assertTrue(matcher.matches("(1.5.1,1.6)"));
assertFalse(matcher.matches("(1.5.2,1.6)"));
assertTrue(matcher.matches("[1.5.2,1.6)"));
assertTrue(matcher.matches("(1.4,1.5.2]"));
assertTrue(matcher.matches("(1.5,)"));
assertEquals("1.5.2", matcher.toString());
Expand Down