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.
The value is automatically derived from maven-enforcer rule requireJava This closes eclipse-m2e#1134 This closes eclipse-m2e#1099
- Loading branch information
Showing
24 changed files
with
888 additions
and
171 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
110 changes: 110 additions & 0 deletions
110
org.eclipse.m2e.core.tests/src/org/eclipse/m2e/core/TestJREListingService.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,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); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...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,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()); | ||
} | ||
} | ||
} |
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.