-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Improve the matching of required versions #11770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: maven-3.9.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||
|
|
@@ -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 { | ||||||
|
|
@@ -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)) { | ||||||
| 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)) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: the major version part is
Suggested change
Also, as @slawekjaranowski mentioned, both patterns should be pre-compiled into |
||||||
| 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(); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
|
||
There was a problem hiding this comment.
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