Skip to content

Commit

Permalink
Configure Maven Execution 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 f616d7d
Show file tree
Hide file tree
Showing 30 changed files with 924 additions and 172 deletions.
5 changes: 5 additions & 0 deletions org.eclipse.m2e.core.tests/.project
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dsVersion=V1_3
eclipse.preferences.version=1
enabled=true
generateBundleActivationPolicyLazy=false
path=OSGI-INF
validationErrorLevel=error
validationErrorLevel.missingImplicitUnbindMethod=error
5 changes: 4 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,9 @@ 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"
Service-Component: OSGI-INF/org.eclipse.m2e.core.internal.TestJREManager.xml
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
1 change: 1 addition & 0 deletions org.eclipse.m2e.core.tests/OSGI-INF/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/org.eclipse.m2e.*.xml
4 changes: 3 additions & 1 deletion org.eclipse.m2e.core.tests/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ source.. = src/
output.. = bin/
bin.includes = META-INF/,\
resources/,\
.
.,\
OSGI-INF/
# Maven Properties, see: https://github.com/eclipse/tycho/wiki/Tycho-Pomless#define-properties
pom.model.property.tycho.surefire.useUIHarness = false
pom.model.property.tycho.surefire.useUIThread = false
bin.excludes = OSGI-INF/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/********************************************************************************
* 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.internal;

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

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.jdt.launching.JavaRuntime;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

/**
* Replaces the {@link DefaultJREManager} from the host bundle {@code m2e.core}.
* Uses a hard-coded list of JREs per execution environment id. Make sure to load the service descriptor from the host (compare with <a href=
* "https://docs.osgi.org/specification/osgi.cmpn/8.0.0/service.component.html#d0e30931">Declarative
* Services, Service Component Header</a>)
*/
@SuppressWarnings("restriction")
@Component(property = Constants.SERVICE_RANKING + ":Integer=100")
public class TestJREManager implements IJREManager {

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

/** Dummy JRE which has a name and java version equal to its id */
private static final class JRE extends AbstractVMInstall implements IVMInstall {

public JRE(String id) {
// first argument must not be null but its value doesn't matter
super(JavaRuntime.getVMInstallTypes()[0], id);
setName(id);
}

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

@Override
public String toString() {
return "JRE with id " + getId();
}
}

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

@Override
public 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 resolveJRE(entry.getPath());
}
}
throw new IllegalStateException("No proper Java build classpath set on project " + project);
}

private IVMInstall resolveJRE(IPath containerPath) {
if (containerPath.segmentCount() > 1) {
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 IVMInstall resolveJRE(String environmentId) {
return INSTALLED_JRES_PER_ENVIRONMENT_ID.get(environmentId);
}

@Override
public IVMInstall getJREForCompositeId(String jreCompositeId) {
return INSTALLED_JRES_PER_ENVIRONMENT_ID.values().stream().filter(vm -> jreCompositeId.equals(vm.getId()))
.findFirst().orElse(null);
}

@Override
public String getCompositeIdForJRE(IVMInstall jre) {
if (jre == null) {
return null;
}
return jre.getId();
}

@Override
public String toString() {
return "TestJREManager exposing JREs: " + INSTALLED_JRES_PER_ENVIRONMENT_ID;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*******************************************************************************
* 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.assertNotNull;
import static org.junit.Assert.assertNull;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.internal.IJREManager;
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
import org.junit.Before;
import org.junit.Test;

public class MavenJreFromEnforcerTest extends AbstractMavenProjectTestCase {

private IJREManager jreManager;

@Before
public void setUp() throws Exception {
super.setUp();
jreManager = MavenPlugin.getJREManager();
}

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

@Test
public void testEnforcer_VersionRange() throws Exception {
IProject project = importProject("resources/projects/enforcerSettingsWithVersionRange/pom.xml");
waitForJobsToComplete();
assertMavenExecutionJRE(project, "13.0.3"); // range is [11.0.10,16) but available 11.0.4 does not satisfy
assertBuildJRE(project, "1.8");
}

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

private void assertBuildJRE(IProject project, String expectedJreId) throws CoreException {
IVMInstall jre = jreManager.getProjectBuildJRE(project);
if (expectedJreId == null) {
assertNull(jre);
} else {
assertNotNull("No Build JRE set", jre);
assertEquals(expectedJreId, jre.getId());
}
}

private void assertMavenExecutionJRE(IProject project, String expectedJreId) {
ResolverConfigurationIO.readResolverConfiguration(project).getMavenJRECompositeId();
IVMInstall jre = jreManager.getJREForCompositeId(expectedJreId);
if (expectedJreId == null) {
assertNull("Non default Maven execution JRE set but expected the default", jre);
} else {
assertNotNull("No explicit Maven Execution JRE set", jre);
assertEquals(expectedJreId, jre.getId());
}
}
}
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=Default Maven Execution 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 f616d7d

Please sign in to comment.