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 ad272c1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 76 deletions.
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 @@ -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 @@ -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;
Expand Down Expand Up @@ -311,57 +313,41 @@ protected void addProjectSourceFolders(IClasspathDescriptor classpath, Map<Strin
IPath[] inclusionTest = new IPath[0];
IPath[] exclusionTest = new IPath[0];

String mainSourceEncoding = null;
String testSourceEncoding = null;
IConfigurationParameter mainSourceEncoding = null;
IConfigurationParameter testSourceEncoding = null;

String mainResourcesEncoding = null;
String testResourcesEncoding = null;

List<MojoExecution> 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);
Expand Down Expand Up @@ -391,16 +377,16 @@ protected boolean isCompileExecution(MojoExecution execution, MavenProject maven

private boolean isCompliant(MojoExecution execution, MavenProject mavenProject, Map<String, String> 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;
Expand All @@ -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<String> 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);
Expand All @@ -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());
Expand Down Expand Up @@ -619,8 +600,8 @@ protected void addJavaProjectOptions(Map<String, String> 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];
Expand Down Expand Up @@ -706,15 +687,16 @@ 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
}

//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);
}
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -958,7 +940,7 @@ private List<String> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -138,7 +140,8 @@ private IPath getMetainfPath(IMavenProjectFacade facade, List<MojoExecution> 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());
}
Expand Down

0 comments on commit ad272c1

Please sign in to comment.