Skip to content

Commit 2a02ddd

Browse files
[MENFORCER-458] Move Require Java, Maven Version to new API
1 parent 5e7c61e commit 2a02ddd

File tree

15 files changed

+363
-316
lines changed

15 files changed

+363
-316
lines changed

enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/utils/ArtifactMatcher.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import java.util.function.Function;
2525

2626
import org.apache.maven.artifact.Artifact;
27+
import org.apache.maven.artifact.versioning.ArtifactVersion;
2728
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
2829
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
2930
import org.apache.maven.artifact.versioning.VersionRange;
3031
import org.apache.maven.model.Dependency;
31-
import org.apache.maven.plugins.enforcer.AbstractVersionEnforcer;
3232
import org.codehaus.plexus.util.StringUtils;
3333

3434
import static java.util.Optional.ofNullable;
@@ -126,7 +126,7 @@ private boolean match(
126126

127127
case 3:
128128
if (!matches(parts[2], version)) {
129-
if (!AbstractVersionEnforcer.containsVersion(
129+
if (!containsVersion(
130130
VersionRange.createFromVersionSpec(parts[2]), new DefaultArtifactVersion(version))) {
131131
return false;
132132
}
@@ -216,4 +216,24 @@ public boolean match(Artifact artifact) {
216216
public boolean match(Dependency dependency) {
217217
return match(p -> p.match(dependency));
218218
}
219+
220+
/**
221+
* Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
222+
* containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
223+
* "[2.0.4,)"
224+
*
225+
* @param allowedRange range of allowed versions.
226+
* @param theVersion the version to be checked.
227+
* @return true if the version is contained by the range.
228+
*/
229+
public static boolean containsVersion(VersionRange allowedRange, ArtifactVersion theVersion) {
230+
ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
231+
if (recommendedVersion == null) {
232+
return allowedRange.containsVersion(theVersion);
233+
} else {
234+
// only singular versions ever have a recommendedVersion
235+
int compareTo = recommendedVersion.compareTo(theVersion);
236+
return (compareTo <= 0);
237+
}
238+
}
219239
}

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.java renamed to enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/AbstractVersionEnforcer.java

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugins.enforcer;
19+
package org.apache.maven.enforcer.rules.version;
2020

2121
import org.apache.maven.artifact.versioning.ArtifactVersion;
2222
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
2323
import org.apache.maven.artifact.versioning.VersionRange;
24-
import org.apache.maven.enforcer.rule.api.EnforcerRule;
2524
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
26-
import org.apache.maven.plugin.logging.Log;
25+
import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
2726
import org.codehaus.plexus.util.StringUtils;
2827

28+
import static org.apache.maven.enforcer.rules.utils.ArtifactMatcher.containsVersion;
29+
2930
/**
3031
* Contains the common code to compare a version against a version range.
3132
*
3233
* @author <a href="mailto:[email protected]">Brian Fox</a>
3334
*/
34-
public abstract class AbstractVersionEnforcer extends AbstractStandardEnforcerRule {
35+
abstract class AbstractVersionEnforcer extends AbstractStandardEnforcerRule {
3536

3637
/**
3738
* Specify the required version. Some examples are:
@@ -51,14 +52,13 @@ public abstract class AbstractVersionEnforcer extends AbstractStandardEnforcerRu
5152
/**
5253
* Compares the specified version to see if it is allowed by the defined version range.
5354
*
54-
* @param log the log
55-
* @param variableName name of variable to use in messages (Example: "Maven" or "Java" etc).
55+
* @param variableName name of variable to use in messages (Example: "Maven" or "Java" etc).
5656
* @param requiredVersionRange range of allowed versions.
57-
* @param actualVersion the version to be checked.
57+
* @param actualVersion the version to be checked.
5858
* @throws EnforcerRuleException the enforcer rule exception
5959
*/
6060
// CHECKSTYLE_OFF: LineLength
61-
public void enforceVersion(Log log, String variableName, String requiredVersionRange, ArtifactVersion actualVersion)
61+
public void enforceVersion(String variableName, String requiredVersionRange, ArtifactVersion actualVersion)
6262
throws EnforcerRuleException
6363
// CHECKSTYLE_ON: LineLength
6464
{
@@ -71,13 +71,13 @@ public void enforceVersion(Log log, String variableName, String requiredVersionR
7171

7272
// short circuit check if the strings are exactly equal
7373
if (actualVersion.toString().equals(requiredVersionRange)) {
74-
log.debug(msg + " is allowed in the range " + requiredVersionRange + ".");
74+
getLog().debug(msg + " is allowed in the range " + requiredVersionRange + ".");
7575
} else {
7676
try {
7777
vr = VersionRange.createFromVersionSpec(requiredVersionRange);
7878

7979
if (containsVersion(vr, actualVersion)) {
80-
log.debug(msg + " is allowed in the range " + toString(vr) + ".");
80+
getLog().debug(msg + " is allowed in the range " + toString(vr) + ".");
8181
} else {
8282
String message = getMessage();
8383

@@ -103,25 +103,6 @@ protected static String toString(VersionRange vr) {
103103
return vr.toString();
104104
}
105105
}
106-
/**
107-
* Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
108-
* containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
109-
* "[2.0.4,)"
110-
*
111-
* @param allowedRange range of allowed versions.
112-
* @param theVersion the version to be checked.
113-
* @return true if the version is contained by the range.
114-
*/
115-
public static boolean containsVersion(VersionRange allowedRange, ArtifactVersion theVersion) {
116-
ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
117-
if (recommendedVersion == null) {
118-
return allowedRange.containsVersion(theVersion);
119-
} else {
120-
// only singular versions ever have a recommendedVersion
121-
int compareTo = recommendedVersion.compareTo(theVersion);
122-
return (compareTo <= 0);
123-
}
124-
}
125106

126107
@Override
127108
public String getCacheId() {
@@ -133,19 +114,6 @@ public String getCacheId() {
133114
}
134115
}
135116

136-
@Override
137-
public boolean isCacheable() {
138-
// the maven version is not going to change between projects in the same build.
139-
return true;
140-
}
141-
142-
@Override
143-
public boolean isResultValid(EnforcerRule theCachedRule) {
144-
// i will always return the hash of the parameters as my id. If my parameters are the same, this
145-
// rule must always have the same result.
146-
return true;
147-
}
148-
149117
/**
150118
* Gets the required version.
151119
*
@@ -170,4 +138,9 @@ public final String getVersion() {
170138
public void setVersion(String theVersion) {
171139
this.version = theVersion;
172140
}
141+
142+
@Override
143+
public String toString() {
144+
return String.format("%s[version=%s]", getClass().getSimpleName(), version);
145+
}
173146
}

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVersion.java renamed to enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugins.enforcer;
19+
package org.apache.maven.enforcer.rules.version;
20+
21+
import javax.inject.Named;
2022

2123
import java.util.Arrays;
2224
import java.util.Iterator;
@@ -30,16 +32,15 @@
3032
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
3133
import org.apache.maven.artifact.versioning.VersionRange;
3234
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
33-
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
34-
import org.apache.maven.plugin.logging.Log;
3535
import org.codehaus.plexus.util.StringUtils;
3636

3737
/**
3838
* This rule checks that the Java version is allowed.
3939
*
4040
* @author <a href="mailto:[email protected]">Brian Fox</a>
4141
*/
42-
public class RequireJavaVersion extends AbstractVersionEnforcer {
42+
@Named("requireJavaVersion")
43+
public final class RequireJavaVersion extends AbstractVersionEnforcer {
4344

4445
private static final Pattern JDK8_VERSION_PATTERN = Pattern.compile("([\\[(,]?)(1\\.8|8)([]),]?)");
4546

@@ -63,24 +64,23 @@ public void setVersion(String theVersion) {
6364
}
6465

6566
@Override
66-
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
67+
public void execute() throws EnforcerRuleException {
6768
String javaVersion = SystemUtils.JAVA_VERSION;
68-
Log log = helper.getLog();
6969

70-
log.debug("Detected Java String: '" + javaVersion + "'");
70+
getLog().debug("Detected Java String: '" + javaVersion + "'");
7171
javaVersion = normalizeJDKVersion(javaVersion);
72-
log.debug("Normalized Java String: '" + javaVersion + "'");
72+
getLog().debug("Normalized Java String: '" + javaVersion + "'");
7373

7474
ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion(javaVersion);
7575

76-
log.debug("Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: "
76+
getLog().debug("Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: "
7777
+ detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion()
7878
+ " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: "
7979
+ detectedJdkVersion.getQualifier());
8080

81-
setCustomMessageIfNoneConfigured(detectedJdkVersion, getVersion(), log);
81+
setCustomMessageIfNoneConfigured(detectedJdkVersion, getVersion());
8282

83-
enforceVersion(helper.getLog(), "JDK", getVersion(), detectedJdkVersion);
83+
enforceVersion("JDK", getVersion(), detectedJdkVersion);
8484
}
8585

8686
/**
@@ -117,15 +117,14 @@ public static String normalizeJDKVersion(String theJdkVersion) {
117117
return StringUtils.stripEnd(version, ".");
118118
}
119119

120-
private void setCustomMessageIfNoneConfigured(
121-
ArtifactVersion detectedJdkVersion, String allowedVersionRange, Log log) {
120+
private void setCustomMessageIfNoneConfigured(ArtifactVersion detectedJdkVersion, String allowedVersionRange) {
122121
if (getMessage() == null) {
123122
String version;
124123
try {
125124
VersionRange vr = VersionRange.createFromVersionSpec(allowedVersionRange);
126125
version = AbstractVersionEnforcer.toString(vr);
127126
} catch (InvalidVersionSpecificationException e) {
128-
log.debug("Could not parse allowed version range " + allowedVersionRange, e);
127+
getLog().debug("Could not parse allowed version range " + allowedVersionRange + " " + e.getMessage());
129128
version = allowedVersionRange;
130129
}
131130
String message = String.format(
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.enforcer.rules.version;
20+
21+
import javax.inject.Inject;
22+
import javax.inject.Named;
23+
24+
import java.util.Objects;
25+
26+
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
27+
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
28+
import org.apache.maven.rtinfo.RuntimeInformation;
29+
30+
/**
31+
* This rule checks that the Maven version is allowed.
32+
*
33+
* @author <a href="mailto:[email protected]">Brian Fox</a>
34+
*/
35+
@Named("requireMavenVersion")
36+
public final class RequireMavenVersion extends AbstractVersionEnforcer {
37+
38+
private final RuntimeInformation runtimeInformation;
39+
40+
@Inject
41+
public RequireMavenVersion(RuntimeInformation runtimeInformation) {
42+
this.runtimeInformation = Objects.requireNonNull(runtimeInformation);
43+
}
44+
45+
@Override
46+
public void execute() throws EnforcerRuleException {
47+
String mavenVersion = runtimeInformation.getMavenVersion();
48+
getLog().debug("Detected Maven Version: " + mavenVersion);
49+
if (mavenVersion == null) {
50+
throw new EnforcerRuleException("Unable to detect Maven Version");
51+
}
52+
DefaultArtifactVersion detectedVersion = new DefaultArtifactVersion(mavenVersion);
53+
enforceVersion("Maven", getVersion(), detectedVersion);
54+
}
55+
}

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireMavenVersion.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

enforcer-rules/src/site/apt/requireJavaVersion.apt.vm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Require Java Version
2929

3030
The following parameters are supported by this rule:
3131

32-
* message - an optional message to the user if the rule fails.
32+
* <<message>> - an optional message to the user if the rule fails.
3333

34-
* {{{../apidocs/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.html#version}version}} - {{{./versionRanges.html}range}} of allowed JDKs.
34+
* <<version>> - {{{./versionRanges.html}range}} of allowed JDKs.
3535

3636
For JDK 1.8 you can also use simple <<<8>>> as <version> - it will be internally changed to <<<1.8>>>
3737

@@ -55,10 +55,10 @@ Require Java Version
5555
+---+
5656
mvn enforcer:display-info
5757
...
58-
[enforcer:display-info]
59-
Maven Version: 2.0.8
60-
JDK Version: 1.5.0_11 normalized as: 1.5.0-11
61-
OS Info: Arch: x86 Family: windows Name: windows xp Version: 5.1
58+
[INFO] Maven Version: 3.8.7
59+
[INFO] JDK Version: 1.8.0_352 normalized as: 1.8.0-352
60+
[INFO] Java Vendor: Homebrew
61+
[INFO] OS Info - Arch: x86_64, Family: mac, Name: mac os x, Version: 12.6.1
6262
+---+
6363

6464
Sample Plugin Configuration:

enforcer-rules/src/site/apt/requireMavenVersion.apt.vm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Require Maven Version
2929

3030
The following parameters are supported by this rule:
3131

32-
* message - an optional message to the user if the rule fails.
32+
* <<message>> - an optional message to the user if the rule fails.
3333

34-
* {{{../enforcer-rules/apidocs/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.html#version}version}} - {{{./versionRanges.html}range}} of allowed Maven versions.
34+
* <<version>> - {{{./versionRanges.html}range}} of allowed Maven versions.
3535

3636
[]
3737

0 commit comments

Comments
 (0)