diff --git a/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/AbstractAptConfiguratorDelegate.java b/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/AbstractAptConfiguratorDelegate.java index 6d24421281..2bd521357b 100644 --- a/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/AbstractAptConfiguratorDelegate.java +++ b/org.eclipse.m2e.apt.core/src/org/eclipse/m2e/apt/internal/AbstractAptConfiguratorDelegate.java @@ -50,12 +50,12 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.project.MavenProject; import org.eclipse.m2e.apt.internal.utils.ProjectUtils; import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.IMaven.IConfigurationParameter; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant; import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator; @@ -357,13 +357,12 @@ private IClasspathEntryDescriptor getEntryDescriptor(IClasspathDescriptor classp protected T getParameterValue(String parameter, Class asType, MojoExecution mojoExecution) throws CoreException { - PluginExecution execution = new PluginExecution(); - execution.setConfiguration(mojoExecution.getConfiguration()); MavenProject mavenProject = mavenFacade.getMavenProject(); return mavenFacade.createExecutionContext().execute(mavenProject, (context, monitor) -> { //TODO provide as part of the execution context? We then probably won't need the project parameter at all? - return MavenPlugin.getMaven().getMojoParameterValue(mavenProject, parameter, asType, mojoExecution.getPlugin(), - execution, mojoExecution.getGoal(), null); + IConfigurationParameter configParameter = MavenPlugin.getMaven() + .getMojoConfiguration(mavenProject, mojoExecution, null).get(parameter); + return configParameter.exists() ? configParameter.as(asType) : null; }, null); } 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..0d60f3b05e 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.IConfigurationElement; +import org.eclipse.m2e.core.embedder.IMaven.IConfigurationParameter; 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"); + IConfigurationElement config = maven.getMojoConfiguration(project, mojoExecution, null); + IConfigurationParameter 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); + IConfigurationParameter 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/embedder/IMaven.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMaven.java index a61ca63238..58a753e26d 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 @@ -21,6 +21,8 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; +import java.util.stream.Stream; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; @@ -88,8 +90,8 @@ Artifact resolve(String groupId, String artifactId, String version, String type, * Returns path of the specified artifact relative to repository baseDir. Can use used to access local repository * files bypassing maven resolution logic. */ - String getArtifactPath(ArtifactRepository repository, String groupId, String artifactId, String version, - String type, String classifier) throws CoreException; + String getArtifactPath(ArtifactRepository repository, String groupId, String artifactId, String version, String type, + String classifier) throws CoreException; /** * Returns true if the artifact does NOT exist in the local repository and known to be UNavailable from all specified @@ -112,8 +114,7 @@ boolean isUnavailable(String groupId, String artifactId, String version, String /** * @since 1.10 */ - Map readMavenProjects(Collection pomFiles, - ProjectBuildingRequest configuration) + Map readMavenProjects(Collection pomFiles, ProjectBuildingRequest configuration) throws CoreException; /** @@ -148,15 +149,51 @@ MavenExecutionPlan calculateExecutionPlan(MavenProject project, List goa MojoExecution setupMojoExecution(MavenProject project, MojoExecution execution, IProgressMonitor monitor) throws CoreException; + /** + * Instances should not be cached + * + * @since 2.1 + */ + public interface IConfigurationElement { + // TODO or firstChild() ? That's what it is actually but it does not look so nice when chained + IConfigurationParameter get(String name) throws NoSuchElementException; + + // TODO: or all(String) or similar. + Stream children(String name) throws NoSuchElementException; + + // TODO: or all() or similar. + Stream children() throws NoSuchElementException; + } + + public interface IConfigurationParameter extends IConfigurationElement { + + boolean exists(); + + T as(Class clazz) throws NoSuchElementException, IllegalStateException; + } + + /** + * @since 2.1 + */ + IConfigurationElement getMojoConfiguration(MavenProject project, MojoExecution mojoExecution, + IProgressMonitor monitor) throws CoreException; + /** * @since 1.4 + * @deprecated use {@link #getMojoConfiguration(MavenProject, MojoExecution, IProgressMonitor)} instead and query the + * returned {@link IConfigurationElement}. */ - T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter, - Class asType, IProgressMonitor monitor) throws CoreException; + @Deprecated(forRemoval = true, since = "2.1") + default T getMojoParameterValue(MavenProject project, MojoExecution mojoExecution, String parameter, + Class asType, IProgressMonitor monitor) throws CoreException { + IConfigurationParameter configParameter = getMojoConfiguration(project, mojoExecution, monitor).get(parameter); + return configParameter.exists() ? configParameter.as(asType) : null; + } /** * @since 1.4 */ + //TODO: deprecate that as well? Without 1:1 replacement? M2E code base does not use it anymore T getMojoParameterValue(MavenProject project, String parameter, Class type, Plugin plugin, ConfigurationContainer configuration, String goal, IProgressMonitor monitor) throws CoreException; @@ -223,8 +260,7 @@ T getMojoParameterValue(MavenProject project, String parameter, Class typ * {@link #releaseMojo(Object, MojoExecution)}. This method is intended to allow introspection of mojo configuration * parameters, use {@link #execute(MavenSession, MojoExecution, IProgressMonitor)} to execute mojo. */ - T getConfiguredMojo(MavenSession session, MojoExecution mojoExecution, Class clazz) - throws CoreException; + T getConfiguredMojo(MavenSession session, MojoExecution mojoExecution, Class clazz) throws CoreException; /** * Releases resources used by Mojo acquired with {@link #getConfiguredMojo(MavenSession, MojoExecution, Class)} diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/ConfigurationElementImpl.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/ConfigurationElementImpl.java new file mode 100644 index 0000000000..011e7d7294 --- /dev/null +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/ConfigurationElementImpl.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * 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.embedder; + +import java.util.Arrays; +import java.util.NoSuchElementException; +import java.util.stream.Stream; + +import org.eclipse.osgi.util.NLS; + +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.expression.ExpressionEvaluator; +import org.codehaus.plexus.configuration.PlexusConfiguration; + +import org.apache.maven.plugin.descriptor.MojoDescriptor; + +import org.eclipse.m2e.core.embedder.IMaven.IConfigurationElement; +import org.eclipse.m2e.core.embedder.IMaven.IConfigurationParameter; + + +class ConfigurationElementImpl implements IConfigurationElement { + final PlexusConfiguration configuration; + + final String path; + + final ValueFactory valueFactory; + + ConfigurationElementImpl(PlexusConfiguration configuration, String path, ValueFactory valueComputer) { + this.path = path; + this.configuration = configuration; + this.valueFactory = valueComputer; + } + + @Override + public IConfigurationParameter get(String name) { + requireExists(); + PlexusConfiguration child = configuration.getChild(name); + return new ConfigurationParameterImpl(child, this.path + "/" + name, valueFactory); + } + + @Override + public Stream children(String name) { + requireExists(); + return Arrays.stream(configuration.getChildren(name)) + .map(c -> new ConfigurationParameterImpl(c, this.path + "/" + name, valueFactory)); + } + + @Override + public Stream children() throws NoSuchElementException { + requireExists(); + return Arrays.stream(configuration.getChildren()) + .map(c -> new ConfigurationParameterImpl(c, this.path + "/" + c.getName(), valueFactory)); + } + + void requireExists() { + if(configuration == null) { + throw new NoSuchElementException( + "Plugin execution " + valueFactory.mojo.getId() + "does not have a configuration parameter " + path); + } + } + + record ValueFactory(ConverterLookup lookup, MojoDescriptor mojo, ClassLoader pluginRealm, + ExpressionEvaluator evaluator) { + + private T create(PlexusConfiguration configuration, Class clazz) throws ComponentConfigurationException { + ConfigurationConverter typeConverter = lookup.lookupConverterForType(clazz); + Object value = typeConverter.fromConfiguration(lookup, configuration, clazz, mojo.getImplementationClass(), + pluginRealm, evaluator, null); + return clazz.cast(value); + } + } + + static class ConfigurationParameterImpl extends ConfigurationElementImpl implements IConfigurationParameter { + + private ConfigurationParameterImpl(PlexusConfiguration configuration, String name, ValueFactory valueComputer) { + super(configuration, name, valueComputer); + } + + @Override + public boolean exists() { + return configuration != null; + } + + @Override + public T as(Class clazz) throws NoSuchElementException { + requireExists(); + try { + return valueFactory.create(configuration, clazz); + } catch(ComponentConfigurationException e) { + //TODO: or throw a IllegalArgument exception + // Probably the catched exception is thrown on the wrong type. TODO: test that. + throw new IllegalStateException( + NLS.bind("Failed to compute configuration for for plugin execution {0}", valueFactory.mojo.getId(), e)); + } + } + + } + +} 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 9fdb0f82a5..865c0c1fd2 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 @@ -17,14 +17,14 @@ import static org.eclipse.m2e.core.internal.M2EUtils.copyProperties; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -151,6 +151,7 @@ import org.eclipse.m2e.core.embedder.MavenConfigurationChangeEvent; import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.internal.Messages; +import org.eclipse.m2e.core.internal.embedder.ConfigurationElementImpl.ValueFactory; import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants; import org.eclipse.m2e.core.project.IMavenProjectFacade; @@ -248,8 +249,7 @@ private MavenExecutionPlan calculateExecutionPlan(MavenSession session, List goals, boolean setup, IProgressMonitor monitor) throws CoreException { return getExecutionContext().execute(project, - (context, pm) -> calculateExecutionPlan(context.getSession(), goals, setup), - monitor); + (context, pm) -> calculateExecutionPlan(context.getSession(), goals, setup), monitor); } private MojoExecution setupMojoExecution(MavenSession session, MavenProject project, MojoExecution execution) @@ -273,8 +273,7 @@ private MojoExecution setupMojoExecution(MavenSession session, MavenProject proj public MojoExecution setupMojoExecution(MavenProject project, MojoExecution execution, IProgressMonitor monitor) throws CoreException { return getExecutionContext().execute(project, - (context, pm) -> setupMojoExecution(context.getSession(), project, execution), - monitor); + (context, pm) -> setupMojoExecution(context.getSession(), project, execution), monitor); } @Override @@ -548,9 +547,8 @@ private MavenProject resolveParentProject(RepositorySystemSession repositorySess } public MavenProject resolveParentProject(MavenProject child, IProgressMonitor monitor) throws CoreException { - return getExecutionContext().execute(child, - (context, pm) -> resolveParentProject(context.getRepositorySession(), child, - context.getExecutionRequest().getProjectBuildingRequest()), monitor); + return getExecutionContext().execute(child, (context, pm) -> resolveParentProject(context.getRepositorySession(), + child, context.getExecutionRequest().getProjectBuildingRequest()), monitor); } @Override @@ -639,7 +637,7 @@ void setLastUpdated(ArtifactRepository localRepository, List File lastUpdatedFile = getLastUpdatedFile(localRepository, artifact); lastUpdatedFile.getParentFile().mkdirs(); - try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(lastUpdatedFile))) { + try (OutputStream os = new FileOutputStream(lastUpdatedFile)) { lastUpdated.store(os, null); } catch(IOException ex) { throw new CoreException(Status.error(Messages.MavenImpl_error_write_lastUpdated, ex)); @@ -703,7 +701,7 @@ private String getLastUpdatedKey(ArtifactRepository repository, Artifact artifac private Properties loadLastUpdated(ArtifactRepository localRepository, Artifact artifact) throws CoreException { Properties lastUpdated = new Properties(); File lastUpdatedFile = getLastUpdatedFile(localRepository, artifact); - try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(lastUpdatedFile))) { + try (InputStream is = new FileInputStream(lastUpdatedFile)) { lastUpdated.load(is); } catch(FileNotFoundException ex) { // that's okay @@ -714,54 +712,31 @@ private Properties loadLastUpdated(ArtifactRepository localRepository, Artifact } private File getLastUpdatedFile(ArtifactRepository localRepository, Artifact artifact) { - return new File(localRepository.getBasedir(), basePathOf(artifact) + "/m2e-lastUpdated.properties"); - } - - private static final char PATH_SEPARATOR = '/'; - - private static final char GROUP_SEPARATOR = '.'; - - private String basePathOf(Artifact artifact) { - return formatAsDirectory(artifact.getGroupId()) + PATH_SEPARATOR + artifact.getArtifactId() + PATH_SEPARATOR - + artifact.getBaseVersion() + PATH_SEPARATOR; - } - - private String formatAsDirectory(String directory) { - return directory.replace(GROUP_SEPARATOR, PATH_SEPARATOR); - } - - private T getMojoParameterValue(MavenSession session, MojoExecution mojoExecution, String parameter, - Class asType) 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); - Xpp3Dom dom = mojoExecution.getConfiguration(); - if(dom == null) { - return null; - } - PlexusConfiguration configuration = new XmlPlexusConfiguration(dom).getChild(parameter); - if(configuration == null) { - return null; - } - 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, parameter, mojoExecution.getExecutionId()), e)); - } + return Path.of(localRepository.getBasedir(), // + artifact.getGroupId().replace('.', '/'), artifact.getArtifactId(), artifact.getBaseVersion(), + "m2e-lastUpdated.properties").toFile(); } @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, parameter, asType), monitor); + public IConfigurationElement getMojoConfiguration(MavenProject project, MojoExecution execution, + IProgressMonitor monitor) throws CoreException { + return getExecutionContext().execute(project, (context, pm) -> { + try { + MavenSession session = context.getSession(); + MojoDescriptor mojo = execution.getMojoDescriptor(); + ClassRealm pluginRealm = lookup(BuildPluginManager.class).getPluginRealm(session, mojo.getPluginDescriptor()); + PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(session, execution); + ValueFactory valueComputer = new ValueFactory(converterLookup, mojo, pluginRealm, evaluator); + + Xpp3Dom dom = execution.getConfiguration(); + PlexusConfiguration configuration = dom != null ? new XmlPlexusConfiguration(dom) + : new XmlPlexusConfiguration(""); + return new ConfigurationElementImpl(configuration, "", valueComputer); + } catch(Exception e) { + throw new CoreException(Status.error( + NLS.bind("Could not get the configuration for for plugin execution {0}", execution.getExecutionId()), e)); + } + }, monitor); } private T getMojoParameterValue(String parameter, Class type, MavenSession session, Plugin plugin, @@ -1050,7 +1025,6 @@ public Collection lookupCollection(Class type) throws CoreException { } } - @Override public ClassLoader getProjectRealm(MavenProject project) { Objects.requireNonNull(project); @@ -1085,8 +1059,7 @@ public void interpolateModel(MavenProject project, Model model) throws CoreExcep * @since 1.4 */ public static V execute(IMaven maven, boolean offline, boolean forceDependencyUpdate, ICallable callable, - IProgressMonitor monitor) - throws CoreException { + IProgressMonitor monitor) throws CoreException { IMavenExecutionContext context = maven.createExecutionContext(); context.getExecutionRequest().setOffline(offline); context.getExecutionRequest().setUpdateSnapshots(forceDependencyUpdate); 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..d9bdd8df02 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,7 @@ import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.embedder.IMaven.IConfigurationParameter; import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.internal.MavenPluginActivator; import org.eclipse.m2e.core.internal.Messages; @@ -705,14 +705,14 @@ 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); + org.eclipse.m2e.core.embedder.IMaven.IConfigurationElement mojoConfig = maven.getMojoConfiguration(mavenProject, + setupExecution, monitor); + + return parameters.entrySet().stream().allMatch(e -> { + IConfigurationParameter 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.core/src/org/eclipse/m2e/core/project/configurator/AbstractProjectConfigurator.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/configurator/AbstractProjectConfigurator.java index 2852250ac8..731b24c86d 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/configurator/AbstractProjectConfigurator.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/project/configurator/AbstractProjectConfigurator.java @@ -34,12 +34,12 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; -import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.project.MavenProject; import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; +import org.eclipse.m2e.core.embedder.IMaven.IConfigurationParameter; import org.eclipse.m2e.core.embedder.IMavenConfiguration; import org.eclipse.m2e.core.internal.Messages; import org.eclipse.m2e.core.internal.lifecyclemapping.LifecycleMappingFactory; @@ -178,13 +178,15 @@ public static void addNature(IProject project, String natureId, int updateFlags, /** * @since 1.4 + * @deprecated use {@link IMaven#getMojoConfiguration(MavenProject, MojoExecution, IProgressMonitor)} directly instead + * and query the returned {@link org.eclipse.m2e.core.embedder.IMaven.IConfigurationElement} */ + @Deprecated(forRemoval = true, since = "2.1") protected T getParameterValue(MavenProject project, String parameter, Class asType, MojoExecution mojoExecution, IProgressMonitor monitor) throws CoreException { - PluginExecution execution = new PluginExecution(); - execution.setConfiguration(mojoExecution.getConfiguration()); - return maven.getMojoParameterValue(project, parameter, asType, mojoExecution.getPlugin(), execution, - mojoExecution.getGoal(), monitor); + IConfigurationParameter configParameter = maven.getMojoConfiguration(project, mojoExecution, monitor) + .get(parameter); + return configParameter.exists() ? configParameter.as(asType) : null; } protected void assertHasNature(IProject project, String natureId) throws CoreException { 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..7e984e0dcb 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.IConfigurationElement; +import org.eclipse.m2e.core.embedder.IMaven.IConfigurationParameter; 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); - } + IConfigurationElement 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); - } + IConfigurationElement 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(IConfigurationParameter 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, IConfigurationParameter 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..b3afb658f5 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.IConfigurationParameter; 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()) { + IConfigurationParameter 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()); }