forked from eclipse-m2e/m2e-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure Maven Execution JRE separately
The value is automatically derived from maven-enforcer rule requireJava This closes eclipse-m2e#1134 This closes eclipse-m2e#1099
- Loading branch information
Showing
30 changed files
with
924 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
org.eclipse.m2e.core.tests/.settings/org.eclipse.pde.ds.annotations.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/org.eclipse.m2e.*.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
137 changes: 137 additions & 0 deletions
137
org.eclipse.m2e.core.tests/src/org/eclipse/m2e/core/internal/TestJREManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...se.m2e.core.tests/src/org/eclipse/m2e/core/internal/project/MavenJreFromEnforcerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.