From 36e1c4a4c3759ad35394fa097fa1afd60ba2a6c5 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 7 Nov 2022 17:47:38 +0100 Subject: [PATCH] introduce MojoUtils to encapsulate Mojo parameter retrieval --- org.eclipse.m2e.core/META-INF/MANIFEST.MF | 2 +- .../org/eclipse/m2e/core/embedder/IMaven.java | 22 --- .../m2e/core/internal/embedder/MavenImpl.java | 116 +---------- .../m2e/core/internal/embedder/MojoUtils.java | 185 ++++++++++++++++++ .../AbstractJavaProjectConfigurator.java | 6 +- 5 files changed, 196 insertions(+), 135 deletions(-) create mode 100644 org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MojoUtils.java diff --git a/org.eclipse.m2e.core/META-INF/MANIFEST.MF b/org.eclipse.m2e.core/META-INF/MANIFEST.MF index 6aa829518d..a4944abc03 100644 --- a/org.eclipse.m2e.core/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.core/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name Bundle-SymbolicName: org.eclipse.m2e.core;singleton:=true -Bundle-Version: 2.1.0.qualifier +Bundle-Version: 2.0.5.qualifier Bundle-Activator: org.eclipse.m2e.core.internal.MavenPluginActivator Bundle-Vendor: %Bundle-Vendor Bundle-Localization: plugin diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMaven.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMaven.java index af905f20bf..9c3fbdaa26 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMaven.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMaven.java @@ -22,8 +22,6 @@ import java.util.List; import java.util.Map; -import org.osgi.annotation.versioning.ProviderType; - import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; @@ -59,7 +57,6 @@ * @author igor * @noimplement This interface is not intended to be implemented by clients. */ -@ProviderType public interface IMaven extends IComponentLookup { // POM Model read/write operations @@ -178,25 +175,6 @@ MojoExecution setupMojoExecution(MavenProject project, MojoExecution execution, T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter, Class asType, IProgressMonitor monitor) throws CoreException; - /** - * Resolves a nested configuration parameter from the given {@code mojoExecution}. It coerces from String to the given - * type and considers expressions and default values. - * - * @param - * @param project the Maven project - * @param mojoExecution the mojo execution from which to retrieve the configuration value - * @param parameterPath the path of the parameter to look up, the first item is the name of the element directly below - * {@code } and the last one is the element containing the actual value - * @param asType the type to coerce to - * @param monitor the progress monitor - * @return the parameter value or {@code null} if the parameter with the given name was not found - * @throws CoreException - * @since 2.1.0 - * @see IMaven#getMojoParameterValue(MavenProject, MojoExecution, String, Class, IProgressMonitor) - */ - T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, List parameterPath, - Class asType, IProgressMonitor monitor) throws CoreException; - /** * @since 1.4 */ diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenImpl.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenImpl.java index e5fa3666af..80908c9d3d 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenImpl.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenImpl.java @@ -58,15 +58,7 @@ import org.eclipse.osgi.util.NLS; import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.component.configurator.ComponentConfigurationException; -import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter; -import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup; -import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup; -import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.codehaus.plexus.configuration.PlexusConfiguration; -import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; import org.codehaus.plexus.util.dag.CycleDetectedException; import org.codehaus.plexus.util.xml.Xpp3Dom; @@ -94,16 +86,11 @@ import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.interpolation.ModelInterpolator; import org.apache.maven.plugin.BuildPluginManager; -import org.apache.maven.plugin.InvalidPluginDescriptorException; import org.apache.maven.plugin.MavenPluginManager; import org.apache.maven.plugin.Mojo; import org.apache.maven.plugin.MojoExecution; -import org.apache.maven.plugin.MojoNotFoundException; import org.apache.maven.plugin.PluginConfigurationException; -import org.apache.maven.plugin.PluginDescriptorParsingException; import org.apache.maven.plugin.PluginManagerException; -import org.apache.maven.plugin.PluginNotFoundException; -import org.apache.maven.plugin.PluginParameterExpressionEvaluator; import org.apache.maven.plugin.PluginResolutionException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.version.DefaultPluginVersionRequest; @@ -158,8 +145,6 @@ public class MavenImpl implements IMaven, IMavenConfigurationChangeListener { @Reference private IMavenConfiguration mavenConfiguration; - private final ConverterLookup converterLookup = new DefaultConverterLookup(); - @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) private final List settingsListeners = new CopyOnWriteArrayList<>(); @@ -709,110 +694,19 @@ private String formatAsDirectory(String directory) { return directory.replace(GROUP_SEPARATOR, PATH_SEPARATOR); } - private T getMojoParameterValue(MavenSession session, MojoExecution mojoExecution, List parameterPath, - Class asType) throws CoreException { - Xpp3Dom dom = mojoExecution.getConfiguration(); - if(dom == null) { - return null; - } - PlexusConfiguration configuration = new XmlPlexusConfiguration(dom); - for(String parameter : parameterPath) { - configuration = configuration.getChild(parameter); - if(configuration == null) { - return null; - } - } - return getMojoParameterValue(session, mojoExecution, configuration, asType, String.join("/", parameterPath)); - } - - private T getMojoParameterValue(MavenSession session, MojoExecution mojoExecution, - PlexusConfiguration configuration, Class asType, String parameterLabel) throws CoreException { - try { - MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); - - ClassRealm pluginRealm = lookup(BuildPluginManager.class).getPluginRealm(session, - mojoDescriptor.getPluginDescriptor()); - - ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution); - ConfigurationConverter typeConverter = converterLookup.lookupConverterForType(asType); - - Object value = typeConverter.fromConfiguration(converterLookup, configuration, asType, - mojoDescriptor.getImplementationClass(), pluginRealm, expressionEvaluator, null); - return asType.cast(value); - } catch(Exception e) { - throw new CoreException(Status - .error(NLS.bind(Messages.MavenImpl_error_param_for_execution, parameterLabel, mojoExecution.getExecutionId()), - e)); - } - } - @Override public T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter, Class asType, IProgressMonitor monitor) throws CoreException { - return getExecutionContext().execute(project, - (context, pm) -> getMojoParameterValue(context.getSession(), mojoExecution, - List.of(parameter), asType), - monitor); - } - - @Override - public T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, List parameterPath, - Class asType, IProgressMonitor monitor) throws CoreException { - return getExecutionContext().execute(project, - (context, pm) -> getMojoParameterValue(context.getSession(), mojoExecution, parameterPath, asType), monitor); - } - - private T getMojoParameterValue(String parameter, Class type, MavenSession session, Plugin plugin, - ConfigurationContainer configuration, String goal) throws CoreException { - Xpp3Dom config = (Xpp3Dom) configuration.getConfiguration(); - config = (config != null) ? config.getChild(parameter) : null; - - PlexusConfiguration paramConfig = null; - - if(config == null) { - MojoDescriptor mojoDescriptor; - - try { - mojoDescriptor = lookup(BuildPluginManager.class).getMojoDescriptor(plugin, goal, - session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession()); - } catch(PluginNotFoundException | PluginResolutionException | PluginDescriptorParsingException - | MojoNotFoundException | InvalidPluginDescriptorException ex) { - throw new CoreException(Status.error(Messages.MavenImpl_error_param, ex)); - } - - PlexusConfiguration defaultConfig = mojoDescriptor.getMojoConfiguration(); - if(defaultConfig != null) { - paramConfig = defaultConfig.getChild(parameter, false); - } - } else { - paramConfig = new XmlPlexusConfiguration(config); - } - - if(paramConfig == null) { - return null; - } - - try { - MojoExecution mojoExecution = new MojoExecution(plugin, goal, "default"); //$NON-NLS-1$ - - ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution); - - ConfigurationConverter typeConverter = converterLookup.lookupConverterForType(type); - - Object value = typeConverter.fromConfiguration(converterLookup, paramConfig, type, Object.class, - getPlexusContainer().getContainerRealm(), expressionEvaluator, null); - return type.cast(value); - } catch(ComponentConfigurationException | ClassCastException ex) { - throw new CoreException(Status.error(Messages.MavenImpl_error_param, ex)); - } + return new MojoUtils(this, getPlexusContainer().getContainerRealm(), getExecutionContext()) + .getParameterValue(project, mojoExecution, parameter, asType, monitor); + } @Override public T getMojoParameterValue(MavenProject project, String parameter, Class type, Plugin plugin, ConfigurationContainer configuration, String goal, IProgressMonitor monitor) throws CoreException { - return getExecutionContext().execute(project, - (context, pm) -> getMojoParameterValue(parameter, type, context.getSession(), plugin, configuration, goal), - monitor); + return new MojoUtils(this, getPlexusContainer().getContainerRealm(), getExecutionContext()) + .getParameterValue(project, parameter, type, plugin, configuration, goal, monitor); } @Override diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MojoUtils.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MojoUtils.java new file mode 100644 index 0000000000..a4b40f925c --- /dev/null +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MojoUtils.java @@ -0,0 +1,185 @@ +/******************************************************************************* + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Konrad Windszus - initial implementation + *******************************************************************************/ +package org.eclipse.m2e.core.internal.embedder; + +import java.util.List; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Status; +import org.eclipse.osgi.util.NLS; + +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; +import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter; +import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup; +import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; +import org.codehaus.plexus.configuration.PlexusConfiguration; +import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; +import org.codehaus.plexus.util.xml.Xpp3Dom; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.ConfigurationContainer; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.BuildPluginManager; +import org.apache.maven.plugin.InvalidPluginDescriptorException; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.MojoNotFoundException; +import org.apache.maven.plugin.PluginDescriptorParsingException; +import org.apache.maven.plugin.PluginNotFoundException; +import org.apache.maven.plugin.PluginParameterExpressionEvaluator; +import org.apache.maven.plugin.PluginResolutionException; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.project.MavenProject; + +import org.eclipse.m2e.core.embedder.IComponentLookup; +import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.embedder.IMavenExecutionContext; +import org.eclipse.m2e.core.internal.Messages; + + +/** + * Utility methods to resolve Mojo parameters Still restricted to internal use only until a proper API has been + * established. + */ +public final class MojoUtils { + + private final ConverterLookup converterLookup; + private final IComponentLookup componentManager; + private final ClassLoader classLoader; + private final IMavenExecutionContext executionContext; + + public MojoUtils(IComponentLookup componentManager, ClassLoader classLoader, + IMavenExecutionContext executionContext) { + super(); + this.converterLookup = new DefaultConverterLookup(); + this.componentManager = componentManager; + this.classLoader = classLoader; + this.executionContext = executionContext; + } + + public T getParameterValue(MavenProject project, String parameter, Class type, Plugin plugin, + ConfigurationContainer configuration, String goal, IProgressMonitor monitor) throws CoreException { + return executionContext.execute(project, + (context, pm) -> getParameterValue(parameter, type, context.getSession(), plugin, configuration, goal), + monitor); + } + + public T getParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter, + Class asType, IProgressMonitor monitor) throws CoreException { + return executionContext.execute(project, + (context, pm) -> getParameterValue(context.getSession(), mojoExecution, List.of(parameter), asType), + monitor); + } + + /** + * Resolves a nested configuration parameter from the given {@code mojoExecution}. It coerces from String to the given + * type and considers expressions and default values. + * + * @param + * @param project the Maven project + * @param mojoExecution the mojo execution from which to retrieve the configuration value + * @param parameterPath the path of the parameter to look up, the first item is the name of the element directly below + * {@code } and the last one is the element containing the actual value + * @param asType the type to coerce to + * @param monitor the progress monitor + * @return the parameter value or {@code null} if the parameter with the given name was not found + * @throws CoreException + * @see IMaven#getMojoParameterValue(MavenProject, MojoExecution, String, Class, IProgressMonitor) + */ + public T getParameterValue(MavenProject project, MojoExecution mojoExecution, List parameterPath, + Class asType, IProgressMonitor monitor) throws CoreException { + return executionContext.execute(project, + (context, pm) -> getParameterValue(context.getSession(), mojoExecution, parameterPath, asType), monitor); + } + + private T getParameterValue(MavenSession session, MojoExecution mojoExecution, List parameterPath, + Class asType) throws CoreException { + Xpp3Dom dom = mojoExecution.getConfiguration(); + if(dom == null) { + return null; + } + PlexusConfiguration configuration = new XmlPlexusConfiguration(dom); + for(String parameter : parameterPath) { + configuration = configuration.getChild(parameter); + if(configuration == null) { + return null; + } + } + return getParameterValue(session, mojoExecution, configuration, asType, String.join("/", parameterPath)); + } + + private T getParameterValue(MavenSession session, MojoExecution mojoExecution, + PlexusConfiguration configuration, Class asType, String parameterLabel) throws CoreException { + try { + MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); + + ClassRealm pluginRealm = componentManager.lookup(BuildPluginManager.class).getPluginRealm(session, + mojoDescriptor.getPluginDescriptor()); + + ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution); + ConfigurationConverter typeConverter = converterLookup.lookupConverterForType(asType); + + Object value = typeConverter.fromConfiguration(converterLookup, configuration, asType, + mojoDescriptor.getImplementationClass(), pluginRealm, expressionEvaluator, null); + return asType.cast(value); + } catch(Exception e) { + throw new CoreException(Status.error( + NLS.bind(Messages.MavenImpl_error_param_for_execution, parameterLabel, mojoExecution.getExecutionId()), e)); + } + } + + private T getParameterValue(String parameter, Class type, MavenSession session, Plugin plugin, + ConfigurationContainer configuration, String goal) throws CoreException { + Xpp3Dom config = (Xpp3Dom) configuration.getConfiguration(); + config = (config != null) ? config.getChild(parameter) : null; + + PlexusConfiguration paramConfig = null; + + if(config == null) { + MojoDescriptor mojoDescriptor; + + try { + mojoDescriptor = componentManager.lookup(BuildPluginManager.class).getMojoDescriptor(plugin, goal, + session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession()); + } catch(PluginNotFoundException | PluginResolutionException | PluginDescriptorParsingException + | MojoNotFoundException | InvalidPluginDescriptorException ex) { + throw new CoreException(Status.error(Messages.MavenImpl_error_param, ex)); + } + + PlexusConfiguration defaultConfig = mojoDescriptor.getMojoConfiguration(); + if(defaultConfig != null) { + paramConfig = defaultConfig.getChild(parameter, false); + } + } else { + paramConfig = new XmlPlexusConfiguration(config); + } + + if(paramConfig == null) { + return null; + } + + try { + MojoExecution mojoExecution = new MojoExecution(plugin, goal, "default"); //$NON-NLS-1$ + + ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution); + + ConfigurationConverter typeConverter = converterLookup.lookupConverterForType(type); + + Object value = typeConverter.fromConfiguration(converterLookup, paramConfig, type, Object.class, + classLoader, expressionEvaluator, null); + return type.cast(value); + } catch(ComponentConfigurationException | ClassCastException ex) { + throw new CoreException(Status.error(Messages.MavenImpl_error_param, ex)); + } + } + +} 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 58115199aa..0ad1a595aa 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.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.internal.M2EUtils; +import org.eclipse.m2e.core.internal.embedder.MavenImpl; +import org.eclipse.m2e.core.internal.embedder.MojoUtils; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.project.IProjectConfigurationManager; import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator; @@ -858,7 +860,9 @@ private String getMinimumJavaBuildEnvironmentId(ProjectConfigurationRequest requ private String getMinimumJavaBuildEnvironmentId(MavenProject mavenProject, MojoExecution mojoExecution, IProgressMonitor monitor) throws InvalidVersionSpecificationException, CoreException { - String version = maven.getMojoParameterValue(mavenProject, mojoExecution, + MojoUtils mojoUtils = new MojoUtils(maven, ((MavenImpl) maven).getPlexusContainer().getContainerRealm(), + maven.createExecutionContext()); + String version = mojoUtils.getParameterValue(mavenProject, mojoExecution, List.of("rules", "requireJavaVersion", "version"), String.class, monitor); return getMinimumJavaBuildEnvironmentId(version); }