Skip to content

Commit

Permalink
Handle config of projects with m-enforcer-p without 'requireJavaVersion'
Browse files Browse the repository at this point in the history
Fixes #1103

And rename JavaConfigurationFromEnforcer to
JavaConfigurationFromEnforcerTest to enable it in the CI.
  • Loading branch information
HannesWell committed Nov 30, 2022
1 parent e990840 commit a75554f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>enforce-java-version</id>
<goals>
<goal>enforce</goal>
</goals>
<phase>validate</phase>
<configuration>
<rules>
<requireSnapshotVersion>
<message>No Releases Allowed!</message>
</requireSnapshotVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
import org.junit.Test;

public class JavaConfigurationFromEnforcer extends AbstractMavenProjectTestCase {
public class JavaConfigurationFromEnforcerTest extends AbstractMavenProjectTestCase {
private static final String JRE_CONTAINER_PREFIX = "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/";

@Test
public void testEnforcerVersion() throws Exception {
public void testEnforcer_Version() throws Exception {
IProject project = importProject("projects/enforcerSettingsWithVersion/pom.xml");
waitForJobsToComplete();
IJavaProject jproject = JavaCore.create(project);
Expand All @@ -41,7 +41,7 @@ public void testEnforcerVersion() throws Exception {
}

@Test
public void testEnforcerVersionRange() throws Exception {
public void testEnforcer_VersionRange() throws Exception {
IProject project = importProject("projects/enforcerSettingsWithVersionRange/pom.xml");
waitForJobsToComplete();
IJavaProject jproject = JavaCore.create(project);
Expand All @@ -50,6 +50,17 @@ public void testEnforcerVersionRange() throws Exception {
assertEquals(List.of("JavaSE-11"), getJREContainerVMType(jproject));
}

@Test
public void testEnforcer_NoVersionRange() throws Exception {
IProject project = importProject("projects/enforcerSettingsWithoutRequiredJavaVersion/pom.xml");
waitForJobsToComplete();
IJavaProject jproject = JavaCore.create(project);
assertEquals("1.8", jproject.getOption(JavaCore.COMPILER_SOURCE, false));
assertEquals("1.8", jproject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, false));
assertEquals(List.of("JavaSE-1.8"), getJREContainerVMType(jproject));
}


private static List<String> getJREContainerVMType(IJavaProject jproject) throws JavaModelException {
return Arrays.stream(jproject.getRawClasspath())
.filter(cp -> cp.getEntryKind() == IClasspathEntry.CPE_CONTAINER).map(IClasspathEntry::getPath)
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.m2e.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.m2e.jdt;singleton:=true
Bundle-Version: 2.0.3.qualifier
Bundle-Version: 2.0.4.qualifier
Bundle-Localization: plugin
Export-Package: org.eclipse.m2e.jdt,
org.eclipse.m2e.jdt.internal;x-friends:="org.eclipse.m2e.jdt.ui",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,9 @@ private String getMinimumJavaBuildEnvironmentId(MavenProject mavenProject, MojoE
// https://maven.apache.org/enforcer/enforcer-rules/requireJavaVersion.html
String version = ((MavenImpl) maven).getMojoParameterValue(mavenProject, mojoExecution,
List.of("rules", "requireJavaVersion", "version"), String.class, monitor);
if(version == null) {
return null;
}
return getMinimumJavaBuildEnvironmentId(version);
}

Expand Down

0 comments on commit a75554f

Please sign in to comment.