Skip to content

Commit aa212c4

Browse files
kwinHannesWell
authored andcommitted
Only evaluate major and minor version of enforcer java rule
Fix evaluation of expressions in m-enforcer-p parameter Only select ExecutionEnvironments with at least one compatible VM installed. This closes #842 Also-by: Hannes Wellmann <[email protected]>
1 parent d07bc99 commit aa212c4

File tree

9 files changed

+248
-41
lines changed

9 files changed

+248
-41
lines changed

org.eclipse.m2e.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-SymbolicName: org.eclipse.m2e.core;singleton:=true
5-
Bundle-Version: 2.0.4.qualifier
5+
Bundle-Version: 2.0.5.qualifier
66
Bundle-Activator: org.eclipse.m2e.core.internal.MavenPluginActivator
77
Bundle-Vendor: %Bundle-Vendor
88
Bundle-Localization: plugin

org.eclipse.m2e.core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</parent>
2020

2121
<artifactId>org.eclipse.m2e.core</artifactId>
22-
<version>2.0.4-SNAPSHOT</version>
22+
<version>2.0.5-SNAPSHOT</version>
2323
<packaging>eclipse-plugin</packaging>
2424

2525
<name>Maven Integration for Eclipse Core Plug-in</name>

org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMaven.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ MojoExecution setupMojoExecution(MavenProject project, MojoExecution execution,
158158
throws CoreException;
159159

160160
/**
161+
* Resolves a configuration parameter from the given {@code mojoExecution}. It coerces from String to the given type
162+
* and considers expressions and default values.
163+
*
164+
* @param <T>
165+
* @param project the Maven project
166+
* @param mojoExecution the mojo execution from which to retrieve the configuration value
167+
* @param parameter the name of the parameter (may be nested with separating {@code .})
168+
* @param asType the type to coerce to
169+
* @param monitor the progress monitor
170+
* @return the parameter value or {@code null} if the parameter with the given name was not found
171+
* @throws CoreException
161172
* @since 1.4
162173
*/
163174
<T> T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter,

org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenImpl.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,24 @@ private String formatAsDirectory(String directory) {
709709
return directory.replace(GROUP_SEPARATOR, PATH_SEPARATOR);
710710
}
711711

712-
private <T> T getMojoParameterValue(MavenSession session, MojoExecution mojoExecution, String parameter,
712+
private <T> T getMojoParameterValue(MavenSession session, MojoExecution mojoExecution, List<String> parameterPath,
713713
Class<T> asType) throws CoreException {
714+
Xpp3Dom dom = mojoExecution.getConfiguration();
715+
if(dom == null) {
716+
return null;
717+
}
718+
PlexusConfiguration configuration = new XmlPlexusConfiguration(dom);
719+
for(String parameter : parameterPath) {
720+
configuration = configuration.getChild(parameter);
721+
if(configuration == null) {
722+
return null;
723+
}
724+
}
725+
return getMojoParameterValue(session, mojoExecution, configuration, asType, String.join("/", parameterPath));
726+
}
727+
728+
private <T> T getMojoParameterValue(MavenSession session, MojoExecution mojoExecution,
729+
PlexusConfiguration configuration, Class<T> asType, String parameterLabel) throws CoreException {
714730
try {
715731
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
716732

@@ -719,28 +735,44 @@ private <T> T getMojoParameterValue(MavenSession session, MojoExecution mojoExec
719735

720736
ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
721737
ConfigurationConverter typeConverter = converterLookup.lookupConverterForType(asType);
722-
Xpp3Dom dom = mojoExecution.getConfiguration();
723-
if(dom == null) {
724-
return null;
725-
}
726-
PlexusConfiguration configuration = new XmlPlexusConfiguration(dom).getChild(parameter);
727-
if(configuration == null) {
728-
return null;
729-
}
738+
730739
Object value = typeConverter.fromConfiguration(converterLookup, configuration, asType,
731740
mojoDescriptor.getImplementationClass(), pluginRealm, expressionEvaluator, null);
732741
return asType.cast(value);
733742
} catch(Exception e) {
734-
throw new CoreException(Status
735-
.error(NLS.bind(Messages.MavenImpl_error_param_for_execution, parameter, mojoExecution.getExecutionId()), e));
743+
throw new CoreException(Status.error(
744+
NLS.bind(Messages.MavenImpl_error_param_for_execution, parameterLabel, mojoExecution.getExecutionId()), e));
736745
}
737746
}
738747

739748
@Override
740749
public <T> T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter,
741750
Class<T> asType, IProgressMonitor monitor) throws CoreException {
742751
return getExecutionContext().execute(project,
743-
(context, pm) -> getMojoParameterValue(context.getSession(), mojoExecution, parameter, asType), monitor);
752+
(context, pm) -> getMojoParameterValue(context.getSession(), mojoExecution, List.of(parameter), asType),
753+
monitor);
754+
}
755+
756+
/**
757+
* Resolves a nested configuration parameter from the given {@code mojoExecution}. It coerces from String to the given
758+
* type and considers expressions and default values. Deliberately no public API yet as probably refactored in the
759+
* near future.
760+
*
761+
* @param <T>
762+
* @param project the Maven project
763+
* @param mojoExecution the mojo execution from which to retrieve the configuration value
764+
* @param parameterPath the path of the parameter to look up, the first item is the name of the element directly below
765+
* {@code <configuration>} and the last one is the element containing the actual value
766+
* @param asType the type to coerce to
767+
* @param monitor the progress monitor
768+
* @return the parameter value or {@code null} if the parameter with the given name was not found
769+
* @throws CoreException
770+
* @see IMaven#getMojoParameterValue(MavenProject, MojoExecution, String, Class, IProgressMonitor)
771+
*/
772+
public <T> T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, List<String> parameterPath,
773+
Class<T> asType, IProgressMonitor monitor) throws CoreException {
774+
return getExecutionContext().execute(project,
775+
(context, pm) -> getMojoParameterValue(context.getSession(), mojoExecution, parameterPath, asType), monitor);
744776
}
745777

746778
private <T> T getMojoParameterValue(String parameter, Class<T> type, MavenSession session, Plugin plugin,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>foo.bar</groupId>
7+
<artifactId>demo</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<build>
10+
<pluginManagement>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<version>3.10.1</version>
16+
<configuration>
17+
<source>1.8</source>
18+
<target>1.8</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</pluginManagement>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-enforcer-plugin</artifactId>
27+
<version>3.1.0</version>
28+
<executions>
29+
<execution>
30+
<id>enforce-java-version</id>
31+
<goals>
32+
<goal>enforce</goal>
33+
</goals>
34+
<phase>validate</phase>
35+
<configuration>
36+
<rules>
37+
<requireJavaVersion>
38+
<version>13.0.3</version>
39+
</requireJavaVersion>
40+
</rules>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>foo.bar</groupId>
7+
<artifactId>demo</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<build>
10+
<pluginManagement>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<version>3.10.1</version>
16+
<configuration>
17+
<source>1.8</source>
18+
<target>1.8</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</pluginManagement>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-enforcer-plugin</artifactId>
27+
<version>3.1.0</version>
28+
<executions>
29+
<execution>
30+
<id>enforce-java-version</id>
31+
<goals>
32+
<goal>enforce</goal>
33+
</goals>
34+
<phase>validate</phase>
35+
<configuration>
36+
<rules>
37+
<requireJavaVersion>
38+
<version>[11.0.10,16)</version>
39+
</requireJavaVersion>
40+
</rules>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2022, 2022 Hannes Wellmann and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Hannes Wellmann - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.m2e.jdt.tests;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import org.eclipse.core.resources.IProject;
22+
import org.eclipse.core.runtime.IPath;
23+
import org.eclipse.jdt.core.IClasspathEntry;
24+
import org.eclipse.jdt.core.IJavaProject;
25+
import org.eclipse.jdt.core.JavaCore;
26+
import org.eclipse.jdt.core.JavaModelException;
27+
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
28+
import org.junit.Test;
29+
30+
public class JavaConfigurationFromEnforcer extends AbstractMavenProjectTestCase {
31+
private static final String JRE_CONTAINER_PREFIX = "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/";
32+
33+
@Test
34+
public void testEnforcerVersion() throws Exception {
35+
IProject project = importProject("projects/enforcerSettingsWithVersion/pom.xml");
36+
waitForJobsToComplete();
37+
IJavaProject jproject = JavaCore.create(project);
38+
assertEquals("1.8", jproject.getOption(JavaCore.COMPILER_SOURCE, false));
39+
assertEquals("1.8", jproject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, false));
40+
assertEquals(List.of("JavaSE-13"), getJREContainerVMType(jproject));
41+
}
42+
43+
@Test
44+
public void testEnforcerVersionRange() throws Exception {
45+
IProject project = importProject("projects/enforcerSettingsWithVersionRange/pom.xml");
46+
waitForJobsToComplete();
47+
IJavaProject jproject = JavaCore.create(project);
48+
assertEquals("1.8", jproject.getOption(JavaCore.COMPILER_SOURCE, false));
49+
assertEquals("1.8", jproject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, false));
50+
assertEquals(List.of("JavaSE-11"), getJREContainerVMType(jproject));
51+
}
52+
53+
private static List<String> getJREContainerVMType(IJavaProject jproject) throws JavaModelException {
54+
return Arrays.stream(jproject.getRawClasspath())
55+
.filter(cp -> cp.getEntryKind() == IClasspathEntry.CPE_CONTAINER).map(IClasspathEntry::getPath)
56+
.map(IPath::toString).filter(p -> p.startsWith(JRE_CONTAINER_PREFIX))
57+
.map(p -> p.substring(JRE_CONTAINER_PREFIX.length())).toList();
58+
}
59+
}

org.eclipse.m2e.jdt/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-SymbolicName: org.eclipse.m2e.jdt;singleton:=true
5-
Bundle-Version: 2.0.2.qualifier
5+
Bundle-Version: 2.0.3.qualifier
66
Bundle-Localization: plugin
77
Export-Package: org.eclipse.m2e.jdt,
88
org.eclipse.m2e.jdt.internal;x-friends:="org.eclipse.m2e.jdt.ui",

0 commit comments

Comments
 (0)