From e9bb4a39e56d0f6850516899ad29ed7e24505760 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Tue, 11 Oct 2022 14:35:19 +0200 Subject: [PATCH] Only evaluate major and minor version of enforcer java rule Fix evaluation of expressions in m-enforcer-p parameter Only select ExecutionEnvironments with at least one compatible VM installed. This closes #842 --- .../AbstractJavaProjectConfigurator.java | 67 ++++++++++++------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java index acebe4f8c0..42905e7092 100644 --- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java +++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java @@ -44,11 +44,10 @@ import org.eclipse.jdt.launching.environments.IExecutionEnvironment; import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; -import org.codehaus.plexus.util.xml.Xpp3Dom; - import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; +import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.model.Resource; import org.apache.maven.plugin.MojoExecution; @@ -254,11 +253,12 @@ private IExecutionEnvironment getExecutionEnvironment(String environmentId) { return null; } IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); - for(IExecutionEnvironment environment : manager.getExecutionEnvironments()) { - if(environment.getId().equals(environmentId)) { - return environment; - } + IExecutionEnvironment environment = manager.getEnvironment(environmentId); + if(environment != null && environment.getCompatibleVMs().length > 0) { + return environment; } + log.error("Failed to find a compatible VM for environment id '{}', falling back to workspace default", + environmentId); return null; } @@ -845,34 +845,46 @@ private String getMinimumJavaBuildEnvironmentId(ProjectConfigurationRequest requ try { List mojoExecutions = getEnforcerMojoExecutions(request, monitor); for(MojoExecution mojoExecution : mojoExecutions) { - String version = getMinimumJavaBuildEnvironmentId(mojoExecution); + String version = getMinimumJavaBuildEnvironmentId(request.mavenProject(), mojoExecution, monitor); if(version != null) { return version; } } } catch(CoreException | InvalidVersionSpecificationException ex) { - log.error("Failed to determine minimum build version, assuming default", ex); + log.error("Failed to determine minimum build Java version, assuming default", ex); } return null; } - private String getMinimumJavaBuildEnvironmentId(MojoExecution mojoExecution) - throws InvalidVersionSpecificationException { - // https://maven.apache.org/enforcer/enforcer-rules/requireJavaVersion.html - Xpp3Dom dom = mojoExecution.getConfiguration(); - Xpp3Dom rules = dom.getChild("rules"); - if(rules == null) { - return null; - } - Xpp3Dom requireJavaVersion = rules.getChild("requireJavaVersion"); - if(requireJavaVersion == null) { - return null; - } - Xpp3Dom version = requireJavaVersion.getChild("version"); - if(version == null) { - return null; + private String getMinimumJavaBuildEnvironmentId(MavenProject mavenProject, MojoExecution mojoExecution, + IProgressMonitor monitor) throws InvalidVersionSpecificationException, CoreException { + String version = maven.getMojoParameterValue(mavenProject, mojoExecution, + Arrays.asList("rules", "requireJavaVersion", "version"), String.class, monitor); + return getMinimumJavaBuildEnvironmentId(version); + } + + private static boolean containsVersionIgnoringMicroAndQualifier(VersionRange versionRange, ArtifactVersion version) { + for(Restriction restriction : versionRange.getRestrictions()) { + Restriction normalizedRestriction = getRestrictionIgnoringMicroAndQualifier(restriction); + if(normalizedRestriction.containsVersion(version)) { + return true; + } } - return getMinimumJavaBuildEnvironmentId(version.getValue()); + return false; + } + + private static Restriction getRestrictionIgnoringMicroAndQualifier(Restriction restriction) { + return new Restriction( + restriction.getLowerBound() != null + ? new DefaultArtifactVersion( + restriction.getLowerBound().getMajorVersion() + "." + restriction.getLowerBound().getMinorVersion()) + : null, + restriction.isLowerBoundInclusive(), + restriction.getUpperBound() != null + ? new DefaultArtifactVersion( + restriction.getUpperBound().getMajorVersion() + "." + restriction.getUpperBound().getMinorVersion()) + : null, + restriction.isUpperBoundInclusive()); } private String getMinimumJavaBuildEnvironmentId(String versionSpec) throws InvalidVersionSpecificationException { @@ -883,10 +895,13 @@ private String getMinimumJavaBuildEnvironmentId(String versionSpec) throws Inval ArtifactVersion environmentVersion = new DefaultArtifactVersion(entry.getKey()); boolean foundMatchingVersion = false; if(recommendedVersion == null) { - foundMatchingVersion = vr.containsVersion(environmentVersion); + foundMatchingVersion = containsVersionIgnoringMicroAndQualifier(vr, environmentVersion); } else { // only singular versions ever have a recommendedVersion - int compareTo = recommendedVersion.compareTo(environmentVersion); + // only consider major version here, minor version not relevant inside IDE (probably) + ArtifactVersion normalizedRecommendedVersion = new DefaultArtifactVersion( + recommendedVersion.getMajorVersion() + "." + recommendedVersion.getMinorVersion()); + int compareTo = normalizedRecommendedVersion.compareTo(environmentVersion); foundMatchingVersion = compareTo <= 0; } if(foundMatchingVersion) {