Skip to content

Commit

Permalink
Configure Maven JRE separately
Browse files Browse the repository at this point in the history
The value is automatically derived from maven-enforcer rule requireJava

This closes eclipse-m2e#1134
This closes eclipse-m2e#1099
  • Loading branch information
kwin committed Dec 21, 2022
1 parent 2a76924 commit 27cd558
Show file tree
Hide file tree
Showing 24 changed files with 888 additions and 171 deletions.
4 changes: 3 additions & 1 deletion org.eclipse.m2e.core.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Require-Bundle: org.eclipse.m2e.tests.common,
org.eclipse.core.resources,
org.eclipse.core.runtime,
org.eclipse.m2e.maven.runtime
Import-Package: javax.annotation;version="1.2.0"
Import-Package: javax.annotation;version="1.2.0",
org.eclipse.jdt.internal.launching,
org.eclipse.jdt.launching.environments
Eclipse-BundleShape: dir
Automatic-Module-Name: org.eclipse.m2e.core.tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/********************************************************************************
* Copyright (c) 2022, 2022 Konrad Windszus and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Konrad Windszus - initial API and implementation
********************************************************************************/

package org.eclipse.m2e.core;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.launching.JREContainerInitializer;
import org.eclipse.jdt.launching.AbstractVMInstall;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.m2e.core.internal.IInstalledJREListingService;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

/**
* Replaces the default {@link IInstalledJREListingService} from m2e.core with a hard-coded list of JREs per execution environment id.
*/
@SuppressWarnings("restriction")
@Component(property = Constants.SERVICE_RANKING + ":Integer=1")
public class TestJREListingService implements IInstalledJREListingService {

private static final Map<String, IVMInstall> INSTALLED_JRES_PER_ENVIRONMENT_ID = Map.of(
"JavaSE-1.8", new TestJREListingService.JRE("1.8"),
"JavaSE-11", new TestJREListingService.JRE("11.0.4"),
"JavaSE-13", new TestJREListingService.JRE("13.0.1"));

private static final class JRE extends AbstractVMInstall implements IVMInstall {

public JRE(String id) {
super(null, id);
setName(id);
}

@Override
public String getJavaVersion() {
return getId();
}
}

@Override
public Collection<IVMInstall> getAllInstalledJREs() {
return INSTALLED_JRES_PER_ENVIRONMENT_ID.values();
}

@Override
public Optional<IVMInstall> getProjectBuildJRE(IProject project) {
if (project == null || !project.exists()) {
throw new IllegalStateException("Non existing project");
}
final IJavaProject javaProject;
javaProject = JavaCore.create(project);
if (!javaProject.exists()) {
throw new IllegalStateException("Java project does not exist for " + project.getName());
}
IClasspathEntry[] classpath;
try {
classpath = javaProject.getRawClasspath();
} catch (JavaModelException e) {
throw new IllegalStateException("Could not get raw classpath from java project" + project.getName());
}
IClasspathEntry entry = null;
for (int i = 0; i < classpath.length; i++) {
entry = classpath[i];
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_VARIABLE:
throw new IllegalStateException("Classpath entries of type CPE_VARIABLE not supported");
case IClasspathEntry.CPE_CONTAINER:
return Optional.of(resolveJRE(entry.getPath()));
}
}
throw new IllegalStateException("No proper Java build classpath set on project " + project);
}

private static IVMInstall resolveJRE(IPath containerPath) {
if (containerPath.segmentCount() > 1) {
@SuppressWarnings("restriction")
String id = JREContainerInitializer.getExecutionEnvironmentId(containerPath);
if (id != null) {
return resolveJRE(id);
} else {
throw new IllegalStateException(
"Could not extract execution environment from classpath entry " + containerPath);
}
} else {
throw new IllegalStateException("Unsupported container classpath " + containerPath);
}
}

private static IVMInstall resolveJRE(String environmentId) {
return INSTALLED_JRES_PER_ENVIRONMENT_ID.get(environmentId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2022, 2022 Hannes Wellmann and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Hannes Wellmann - initial API and implementation
*******************************************************************************/

package org.eclipse.m2e.core.internal.project;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstall2;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
import org.junit.Test;

public class MavenJreFromEnforcerTest extends AbstractMavenProjectTestCase {

@Test
public void testEnforcer_Version() throws Exception {
IProject project = importProject("projects/enforcerSettingsWithVersion/pom.xml");
waitForJobsToComplete();
assertMavenExecutionJRE(project, "13.0.1");
assertBuildJRE(project, "1.8");
}

@Test
public void testEnforcer_VersionRange() throws Exception {
IProject project = importProject("projects/enforcerSettingsWithVersionRange/pom.xml");
waitForJobsToComplete();
assertMavenExecutionJRE(project, "11.0.4");
assertBuildJRE(project, "1.8");
}

@Test
public void testEnforcer_NoVersionRange() throws Exception {
IProject project = importProject("projects/enforcerSettingsWithoutRequiredJavaVersion/pom.xml");
waitForJobsToComplete();
assertMavenExecutionJRE(project, "1.8");
assertBuildJRE(project, "1.8");
}

private void assertBuildJRE(IProject project, String expectedJavaVersion) throws CoreException {
IJavaProject jproject = JavaCore.create(project);
if (jproject == null || jproject.exists()) {
fail("Imported project is no Java project");
}
IVMInstall buildJRE = JavaRuntime.getVMInstall(jproject);
if (buildJRE instanceof IVMInstall2 buildJRE2) {
assertEquals("Found wrong Build JRE", expectedJavaVersion, buildJRE2.getJavaVersion());
}
}

private void assertMavenExecutionJRE(IProject project, String expectedJavaVersion) {
IVMInstall mavenExecutionJRE = ResolverConfigurationIO.readResolverConfiguration(project).getMavenJre();
if (mavenExecutionJRE instanceof IVMInstall2 mavenExecutionJRE2) {
assertEquals("Found wrong Maven Execution JRE", expectedJavaVersion, mavenExecutionJRE2.getJavaVersion());
}
}
}
2 changes: 2 additions & 0 deletions org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Require-Bundle: org.eclipse.m2e.core;bundle-version="[2.0.0,3.0.0)",
org.eclipse.core.filebuffers,
org.eclipse.ui
Import-Package: org.eclipse.compare.rangedifferencer,
org.eclipse.jdt.core,
org.eclipse.jdt.launching,
org.eclipse.ltk.core.refactoring,
org.slf4j;version="[1.7.0,3.0.0)"
Service-Component: OSGI-INF/component.xml,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,18 @@ public class Messages extends NLS {

public static String MavenProjectPreferencePage_title;

public static String MavenProjectPreferencePage_defaultWorkspaceJRE;

public static String MavenProjectPreferencePage_defaultProjectJRE;

public static String MavenProjectPreferencePage_mavenJRE;

public static String MavenProjectPreferencePage_alternateJRE;

public static String MavenProjectPreferencePage_btnInstalledJREs;

public static String MavenProjectPreferencePage_jreNotSet;

public static String MavenProjectWizardArchetypePage_add_title;

public static String MavenProjectWizardArchetypePage_all;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ MavenProjectPreferencePage_dialog_title=Maven Settings
MavenProjectPreferencePage_job=Updating {0} Sources
MavenProjectPreferencePage_lblProfiles=Active Maven &Profiles (comma separated)\:
MavenProjectPreferencePage_title=Maven
MavenProjectPreferencePage_defaultWorkspaceJRE=Default Workspace JRE ({0})
MavenProjectPreferencePage_defaultProjectJRE=Default Project JRE ({0})
MavenProjectPreferencePage_mavenJRE=Maven JRE:
MavenProjectPreferencePage_alternateJRE=Alternate JRE:
MavenProjectPreferencePage_btnInstalledJREs=Installed JREs...
MavenProjectPreferencePage_jreNotSet=Not set
MavenProjectWizardArchetypePage_add_title=Add Archetype
MavenProjectWizardArchetypePage_all=All Catalogs
MavenProjectWizardArchetypePage_btnAdd=&Add Archetype...
Expand Down
Loading

0 comments on commit 27cd558

Please sign in to comment.