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..c2b4888dd1 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 @@ -22,6 +22,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -172,7 +174,8 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni String executionEnvironmentId = getExecutionEnvironmentId(options); String buildEnvironmentId = getMinimumJavaBuildEnvironmentId(request, monitor); - addJREClasspathContainer(classpath, buildEnvironmentId != null ? buildEnvironmentId : executionEnvironmentId); + // use the higher environment id as JREs are backwards compatible + addJREClasspathContainer(classpath, getHighestEnvironmentId(buildEnvironmentId, executionEnvironmentId)); addMavenClasspathContainer(classpath); @@ -198,6 +201,24 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni MavenJdtPlugin.getDefault().getBuildpathManager().updateClasspath(project, monitor); } + static String getHighestEnvironmentId(String... environmentIds) { + List environmentIdList = Arrays.stream(environmentIds).filter(Objects::nonNull) + .collect(Collectors.toList()); + if(environmentIdList.size() == 1) { + return environmentIdList.get(0); + } + // ENVIRONMENTS map is sorted from lowest first to highest last + List sortedExecutionEnvironmentIds = new ArrayList<>(ENVIRONMENTS.values()); + Collections.reverse(sortedExecutionEnvironmentIds); // reverse order to see highest id first + for(String executionEnvironmentId : sortedExecutionEnvironmentIds) { + if(environmentIdList.contains(executionEnvironmentId)) { + return executionEnvironmentId; + } + } + throw new IllegalArgumentException( + "None of the environment ids from '" + String.join(",", environmentIds) + "' found in ENVIRONMENTS"); + } + protected IContainer getOutputLocation(ProjectConfigurationRequest request, IProject project) { MavenProject mavenProject = request.mavenProject(); return getFolder(project, mavenProject.getBuild().getOutputDirectory());