Skip to content

Commit

Permalink
Use enhanced Mojo-Configuration methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 18, 2022
1 parent 28c0022 commit 3c5a887
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -357,13 +357,12 @@ private IClasspathEntryDescriptor getEntryDescriptor(IClasspathDescriptor classp

protected <T> T getParameterValue(String parameter, Class<T> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -64,12 +66,13 @@ public Set<IProject> 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();
Expand Down Expand Up @@ -112,8 +115,7 @@ public Set<IProject> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ default <T> T getMojoParameterValue(MavenProject project, MojoExecution mojoExec
/**
* @since 1.4
*/
//TODO: deprecate that as well? Without 1:1 replacement? M2E code base does not use it anymore
<T> T getMojoParameterValue(MavenProject project, String parameter, Class<T> type, Plugin plugin,
ConfigurationContainer configuration, String goal, IProgressMonitor monitor) throws CoreException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -705,14 +705,14 @@ private static List<PluginExecutionMetadata> applyParametersFilter(List<PluginEx
private static boolean hasMatchingParameterValue(MavenProject mavenProject, MojoExecution execution,
PluginExecutionMetadata metadata, IMaven maven, IProgressMonitor monitor) throws CoreException {
Map<String, Object> parameters = metadata.getFilter().getParameters();
for(Entry<String, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> T getParameterValue(MavenProject project, String parameter, Class<T> 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 {
Expand Down
Loading

0 comments on commit 3c5a887

Please sign in to comment.