From 2e1aff1d0b8dbc8072719dbf22fd31b578262c24 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Tue, 18 Oct 2022 18:44:25 +0200 Subject: [PATCH] Use enhanced Mojo-Configuration methods --- .../MavenCompilerBuildParticipant.java | 16 ++-- .../LifecycleMappingFactory.java | 18 ++-- .../AbstractJavaProjectConfigurator.java | 94 ++++++++----------- .../PDEMavenBundlePluginConfigurator.java | 11 ++- 4 files changed, 63 insertions(+), 76 deletions(-) diff --git a/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/compiler/MavenCompilerBuildParticipant.java b/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/compiler/MavenCompilerBuildParticipant.java index e538dd80e0..8060f7abba 100644 --- a/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/compiler/MavenCompilerBuildParticipant.java +++ b/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/compiler/MavenCompilerBuildParticipant.java @@ -35,6 +35,8 @@ import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.embedder.IMaven.ConfigurationElement; +import org.eclipse.m2e.core.embedder.IMaven.ConfigurationParameter; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant; @@ -64,12 +66,13 @@ public Set build(int kind, IProgressMonitor monitor) throws Exception //TODO check delta / scan source for *.java IMavenProjectFacade mavenProjectFacade = getMavenProjectFacade(); MavenProject project = mavenProjectFacade.getMavenProject(); - String compilerArgument = maven.getMojoParameterValue(project, mojoExecution, "compilerArgument", String.class, - null); - boolean isAnnotationProcessingEnabled = (compilerArgument == null) || !compilerArgument.contains("-proc:none"); + ConfigurationElement config = maven.getMojoConfiguration(project, mojoExecution, null); + ConfigurationParameter compilerArgumentConfig = config.get("compilerArgument"); + boolean isAnnotationProcessingEnabled = !compilerArgumentConfig.exists() + || !compilerArgumentConfig.as(String.class).contains("-proc:none"); if(isAnnotationProcessingEnabled) { - String proc = maven.getMojoParameterValue(project, mojoExecution, PROC, String.class, null); - isAnnotationProcessingEnabled = !"none".equals(proc); + ConfigurationParameter procConfig = config.get(PROC); + isAnnotationProcessingEnabled = !procConfig.exists() || !"none".equals(procConfig.as(String.class)); } if(!isAnnotationProcessingEnabled) { return Collections.emptySet(); @@ -112,8 +115,7 @@ public Set build(int kind, IProgressMonitor monitor) throws Exception } // tell m2e builder to refresh generated files - File generated = maven.getMojoParameterValue(project, getMojoExecution(), - MavenCompilerJdtAptDelegate.OUTPUT_DIRECTORY_PARAMETER, File.class, null); + File generated = maven.getMojoConfiguration(project, getMojoExecution(), null).get(MavenCompilerJdtAptDelegate.OUTPUT_DIRECTORY_PARAMETER).as(File.class); if(generated != null) { buildContext.refresh(generated); } diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/lifecyclemapping/LifecycleMappingFactory.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/lifecyclemapping/LifecycleMappingFactory.java index 4f896b7340..a463611874 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/lifecyclemapping/LifecycleMappingFactory.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/lifecyclemapping/LifecycleMappingFactory.java @@ -38,7 +38,6 @@ import java.util.List; import java.util.ListIterator; import java.util.Map; -import java.util.Map.Entry; import java.util.Objects; import java.util.Set; import java.util.jar.JarFile; @@ -83,6 +82,8 @@ import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.embedder.IMaven.ConfigurationElement; +import org.eclipse.m2e.core.embedder.IMaven.ConfigurationParameter; import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.internal.MavenPluginActivator; import org.eclipse.m2e.core.internal.Messages; @@ -705,14 +706,13 @@ private static List applyParametersFilter(List parameters = metadata.getFilter().getParameters(); - for(Entry entry : parameters.entrySet()) { - MojoExecution setupExecution = maven.setupMojoExecution(mavenProject, execution, monitor); - String value = maven.getMojoParameterValue(mavenProject, setupExecution, entry.getKey(), String.class, monitor); - if(!Objects.equals(entry.getValue(), value)) { - return false; - } - } - return true; + MojoExecution setupExecution = maven.setupMojoExecution(mavenProject, execution, monitor); + ConfigurationElement mojoConfig = maven.getMojoConfiguration(mavenProject, setupExecution, monitor); + + return parameters.entrySet().stream().allMatch(e -> { + ConfigurationParameter parameter = mojoConfig.get(e.getKey()); + return parameter.exists() && Objects.equals(parameter.as(String.class), e.getValue()); + }); } private static boolean isValidPluginExecutionMetadata(PluginExecutionMetadata metadata) { diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java index acebe4f8c0..5b458b9a12 100644 --- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java +++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java @@ -55,6 +55,8 @@ import org.apache.maven.project.MavenProject; import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.IMaven.ConfigurationElement; +import org.eclipse.m2e.core.embedder.IMaven.ConfigurationParameter; import org.eclipse.m2e.core.internal.M2EUtils; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.project.IProjectConfigurationManager; @@ -311,8 +313,8 @@ protected void addProjectSourceFolders(IClasspathDescriptor classpath, Map executions = getCompilerMojoExecutions(request, mon.newChild(1)); for(MojoExecution compile : executions) { if(isCompileExecution(compile, mavenProject, options, monitor)) { - mainSourceEncoding = maven.getMojoParameterValue(mavenProject, compile, "encoding", String.class, monitor); //$NON-NLS-1$ - try { - inclusion = toPaths( - maven.getMojoParameterValue(mavenProject, compile, "includes", String[].class, monitor)); //$NON-NLS-1$ - } catch(CoreException ex) { - log.error("Failed to determine compiler inclusions, assuming defaults", ex); - } - try { - exclusion = toPaths( - maven.getMojoParameterValue(mavenProject, compile, "excludes", String[].class, monitor)); //$NON-NLS-1$ - } catch(CoreException ex) { - log.error("Failed to determine compiler exclusions, assuming defaults", ex); - } + ConfigurationElement mojoConfig = maven.getMojoConfiguration(mavenProject, compile, monitor); + mainSourceEncoding = mojoConfig.get("encoding"); //$NON-NLS-1$ + inclusion = toPaths(mojoConfig.get("includes")); //$NON-NLS-1$ + exclusion = toPaths(mojoConfig.get("excludes")); //$NON-NLS-1$ } } for(MojoExecution compile : executions) { if(isTestCompileExecution(compile, mavenProject, options, monitor)) { - testSourceEncoding = maven.getMojoParameterValue(mavenProject, compile, "encoding", String.class, monitor); //$NON-NLS-1$ - try { - inclusionTest = toPaths( - maven.getMojoParameterValue(mavenProject, compile, "testIncludes", String[].class, monitor)); //$NON-NLS-1$ - } catch(CoreException ex) { - log.error("Failed to determine compiler test inclusions, assuming defaults", ex); - } - try { - exclusionTest = toPaths( - maven.getMojoParameterValue(mavenProject, compile, "testExcludes", String[].class, monitor)); //$NON-NLS-1$ - } catch(CoreException ex) { - log.error("Failed to determine compiler test exclusions, assuming defaults", ex); - } + ConfigurationElement mojoConfiguration = maven.getMojoConfiguration(mavenProject, compile, monitor); + testSourceEncoding = mojoConfiguration.get("encoding"); + inclusionTest = toPaths(mojoConfiguration.get("testIncludes")); //$NON-NLS-1$ + exclusionTest = toPaths(mojoConfiguration.get("testExcludes")); //$NON-NLS-1$ } } for(MojoExecution resources : projectFacade.getMojoExecutions(RESOURCES_PLUGIN_GROUP_ID, RESOURCES_PLUGIN_ARTIFACT_ID, mon.newChild(1), GOAL_RESOURCES)) { - mainResourcesEncoding = maven.getMojoParameterValue(mavenProject, resources, "encoding", String.class, monitor); //$NON-NLS-1$ + mainResourcesEncoding = maven.getMojoConfiguration(mavenProject, resources, monitor).get("encoding") //$NON-NLS-1$ + .as(String.class); } for(MojoExecution resources : projectFacade.getMojoExecutions(RESOURCES_PLUGIN_GROUP_ID, RESOURCES_PLUGIN_ARTIFACT_ID, mon.newChild(1), GOAL_TESTRESOURCES)) { - testResourcesEncoding = maven.getMojoParameterValue(mavenProject, resources, "encoding", String.class, monitor); //$NON-NLS-1$ + testResourcesEncoding = maven.getMojoConfiguration(mavenProject, resources, monitor).get("encoding") //$NON-NLS-1$ + .as(String.class); } addSourceDirs(classpath, project, mavenProject.getCompileSourceRoots(), classes.getFullPath(), inclusion, exclusion, mainSourceEncoding, mon.newChild(1), false); @@ -391,16 +377,16 @@ protected boolean isCompileExecution(MojoExecution execution, MavenProject maven private boolean isCompliant(MojoExecution execution, MavenProject mavenProject, Map options, IProgressMonitor monitor) throws CoreException { - String release = maven.getMojoParameterValue(mavenProject, execution, "release", String.class, monitor); //$NON-NLS-1$ + String release = maven.getMojoConfiguration(mavenProject, execution, monitor).get("release").as(String.class); //$NON-NLS-1$ if(release != null && !sanitizeJavaVersion(release).equals(options.get(JavaCore.COMPILER_COMPLIANCE))) { return false; } if(release == null) { - String source = maven.getMojoParameterValue(mavenProject, execution, "source", String.class, monitor); //$NON-NLS-1$ + String source = maven.getMojoConfiguration(mavenProject, execution, monitor).get("source").as(String.class); //$NON-NLS-1$ if(source != null && !sanitizeJavaVersion(source).equals(options.get(JavaCore.COMPILER_SOURCE))) { return false; } - String target = maven.getMojoParameterValue(mavenProject, execution, "target", String.class, monitor); //$NON-NLS-1$ + String target = maven.getMojoConfiguration(mavenProject, execution, monitor).get("target").as(String.class); //$NON-NLS-1$ if(target != null && !sanitizeJavaVersion(target).equals(options.get(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM))) { return false; @@ -409,22 +395,17 @@ private boolean isCompliant(MojoExecution execution, MavenProject mavenProject, return true; } - private IPath[] toPaths(String[] values) { - if(values == null) { + private IPath[] toPaths(ConfigurationParameter configParameter) { + if(!configParameter.exists()) { return new IPath[0]; } - IPath[] paths = new IPath[values.length]; - for(int i = 0; i < values.length; i++ ) { - if(values[i] != null && !"".equals(values[i].trim())) { - paths[i] = new Path(values[i]); - } - } - return paths; + return Arrays.stream(configParameter.as(String[].class)).filter(v -> v != null && !v.isBlank()).map(Path::new) + .toArray(Path[]::new); } private void addSourceDirs(IClasspathDescriptor classpath, IProject project, List sourceRoots, - IPath outputPath, IPath[] inclusion, IPath[] exclusion, String sourceEncoding, IProgressMonitor monitor, - boolean addTestFlag) throws CoreException { + IPath outputPath, IPath[] inclusion, IPath[] exclusion, ConfigurationParameter sourceEncoding, + IProgressMonitor monitor, boolean addTestFlag) throws CoreException { for(String sourceRoot : sourceRoots) { IFolder sourceFolder = getFolder(project, sourceRoot); @@ -445,7 +426,7 @@ private void addSourceDirs(IClasspathDescriptor classpath, IProject project, Lis // Set folder encoding (null = platform/container default) if(sourceFolder.exists()) { - sourceFolder.setDefaultCharset(sourceEncoding, monitor); + sourceFolder.setDefaultCharset(sourceEncoding.exists() ? sourceEncoding.as(String.class) : null, monitor); } IClasspathEntryDescriptor enclosing = getEnclosingEntryDescriptor(classpath, sourceFolder.getFullPath()); @@ -619,8 +600,8 @@ protected void addJavaProjectOptions(Map options, ProjectConfigu || isEnablePreviewFeatures(request.mavenProject(), execution, monitor); // process -err:+deprecation , -warn:-serial ... - for(Object o : maven.getMojoParameterValue(request.mavenProject(), execution, "compilerArgs", List.class, - monitor)) { + for(Object o : maven.getMojoConfiguration(request.mavenProject(), execution, monitor).get("compilerArgs") + .as(List.class)) { if(o instanceof String compilerArg) { boolean err = false/*, warn = false*/; String[] settings = new String[0]; @@ -706,7 +687,8 @@ private boolean isGenerateParameters(MavenProject mavenProject, MojoExecution ex Boolean generateParameters = null; //1st, check the parameters option try { - generateParameters = maven.getMojoParameterValue(mavenProject, execution, "parameters", Boolean.class, monitor);//$NON-NLS-1$ + generateParameters = maven.getMojoConfiguration(mavenProject, execution, monitor).get("parameters") //$NON-NLS-1$ + .as(Boolean.class); } catch(Exception ex) { //ignore } @@ -714,7 +696,7 @@ private boolean isGenerateParameters(MavenProject mavenProject, MojoExecution ex //2nd, check the parameters flag in the compilerArgs list if(!Boolean.TRUE.equals(generateParameters)) { try { - List args = maven.getMojoParameterValue(mavenProject, execution, "compilerArgs", List.class, monitor);//$NON-NLS-1$ + List args = maven.getMojoConfiguration(mavenProject, execution, monitor).get("compilerArgs").as(List.class);//$NON-NLS-1$ if(args != null) { generateParameters = args.contains(JavaSettingsUtils.PARAMETERS_JVM_FLAG); } @@ -726,8 +708,8 @@ private boolean isGenerateParameters(MavenProject mavenProject, MojoExecution ex //3rd, check the parameters flag in the compilerArgument String if(!Boolean.TRUE.equals(generateParameters)) { try { - String compilerArgument = maven.getMojoParameterValue(mavenProject, execution, "compilerArgument", String.class, //$NON-NLS-1$ - monitor); + String compilerArgument = maven.getMojoConfiguration(mavenProject, execution, monitor).get("compilerArgument") + .as(String.class); if(compilerArgument != null) { generateParameters = compilerArgument.contains(JavaSettingsUtils.PARAMETERS_JVM_FLAG); } @@ -743,7 +725,7 @@ private boolean isEnablePreviewFeatures(MavenProject mavenProject, MojoExecution IProgressMonitor monitor) { //1st, check the --enable-preview flag in the compilerArgs list try { - List args = maven.getMojoParameterValue(mavenProject, execution, "compilerArgs", List.class, monitor);//$NON-NLS-1$ + List args = maven.getMojoConfiguration(mavenProject, execution, monitor).get("compilerArgs").as(List.class);//$NON-NLS-1$ if(args != null && args.contains(JavaSettingsUtils.ENABLE_PREVIEW_JVM_FLAG)) { return true; } @@ -753,8 +735,8 @@ private boolean isEnablePreviewFeatures(MavenProject mavenProject, MojoExecution //2nd, check the --enable-preview flag in the compilerArgument String try { - String compilerArgument = maven.getMojoParameterValue(mavenProject, execution, "compilerArgument", String.class, //$NON-NLS-1$ - monitor); + String compilerArgument = maven.getMojoConfiguration(mavenProject, execution, monitor).get("compilerArgument") + .as(String.class); if(compilerArgument != null && compilerArgument.contains(JavaSettingsUtils.ENABLE_PREVIEW_JVM_FLAG)) { return true; } @@ -804,7 +786,7 @@ private String getCompilerLevel(MavenProject mavenProject, MojoExecution executi int levelIdx = getLevelIndex(source, levels); try { - source = maven.getMojoParameterValue(mavenProject, execution, parameter, String.class, monitor); + source = maven.getMojoConfiguration(mavenProject, execution, monitor).get(parameter).as(String.class); } catch(CoreException ex) { log.error("Failed to determine compiler " + parameter + " setting, assuming default", ex); } @@ -958,7 +940,7 @@ private List getCompilerArguments(MavenProject mavenProject, MojoExecuti //1st, get the arguments in the compilerArgs list try { - List args = maven.getMojoParameterValue(mavenProject, execution, "compilerArgs", List.class, monitor);//$NON-NLS-1$ + List args = maven.getMojoConfiguration(mavenProject, execution, monitor).get("compilerArgs").as(List.class);//$NON-NLS-1$ if(args != null) {//$NON-NLS-1$ args.stream().filter(a -> a != null).forEach(a -> arguments.add(a.toString())); } diff --git a/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/PDEMavenBundlePluginConfigurator.java b/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/PDEMavenBundlePluginConfigurator.java index fa0a3c1d85..1bb507df84 100644 --- a/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/PDEMavenBundlePluginConfigurator.java +++ b/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/PDEMavenBundlePluginConfigurator.java @@ -22,6 +22,7 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.embedder.IMaven.ConfigurationParameter; import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.internal.markers.IMavenMarkerManager; import org.eclipse.m2e.core.internal.markers.MavenProblemInfo; @@ -67,9 +68,10 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni if (isFelix(plugin)) { if (isFelixManifestGoal(execution)) { IMaven maven = MavenPlugin.getMaven(); - Boolean supportIncremental = maven.getMojoParameterValue(request.mavenProject(), execution, - FELIX_PARAM_SUPPORTINCREMENTALBUILD, Boolean.class, monitor); - if (supportIncremental == null || !supportIncremental.booleanValue()) { + ConfigurationParameter incrementalBuildConfig = maven + .getMojoConfiguration(request.mavenProject(), execution, monitor) + .get(FELIX_PARAM_SUPPORTINCREMENTALBUILD); + if (!incrementalBuildConfig.exists() || !incrementalBuildConfig.as(Boolean.class).booleanValue()) { createWarningMarker(request, execution, SourceLocationHelper.CONFIGURATION, "Incremental updates are currently disabled, set supportIncrementalBuild=true to support automatic manifest updates for this project."); } @@ -138,7 +140,8 @@ private IPath getMetainfPath(IMavenProjectFacade facade, List exe Plugin plugin = execution.getPlugin(); MavenProject project = facade.getMavenProject(monitor); String manifestParameter = isBND(plugin) ? BND_PARAM_MANIFESTLOCATION : FELIX_PARAM_MANIFESTLOCATION; - File location = maven.getMojoParameterValue(project, execution, manifestParameter, File.class, monitor); + File location = maven.getMojoConfiguration(project, execution, monitor).get(manifestParameter) + .as(File.class); if (location != null) { return facade.getProjectRelativePath(location.getAbsolutePath()); }