From 8514ffda89f9861448a84133adf13e3616244f46 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 8 Oct 2022 11:18:16 +0200 Subject: [PATCH] Move createProblemMarker() to AbstractProjectConfigurator and clean up This makes createProblemMarker() part of the AbstractProjectConfigurator API and therefore available to downstream implementors. --- .../AbstractProjectConfigurator.java | 82 ++++++++++++------- .../PDEMavenBundlePluginConfigurator.java | 31 +------ .../TychoPackagingsConfigurator.java | 21 ++--- 3 files changed, 62 insertions(+), 72 deletions(-) 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..c901d5c1b2 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008-2010 Sonatype, Inc. + * Copyright (c) 2008-2022 Sonatype, Inc. * 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 @@ -14,15 +14,19 @@ package org.eclipse.m2e.core.project.configurator; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IResource; @@ -41,9 +45,13 @@ import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; import org.eclipse.m2e.core.embedder.IMavenConfiguration; +import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.internal.Messages; import org.eclipse.m2e.core.internal.lifecyclemapping.LifecycleMappingFactory; import org.eclipse.m2e.core.internal.markers.IMavenMarkerManager; +import org.eclipse.m2e.core.internal.markers.MavenProblemInfo; +import org.eclipse.m2e.core.internal.markers.SourceLocation; +import org.eclipse.m2e.core.internal.markers.SourceLocationHelper; import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata; import org.eclipse.m2e.core.lifecyclemapping.model.PluginExecutionAction; import org.eclipse.m2e.core.project.IMavenProjectChangedListener; @@ -167,15 +175,43 @@ public static void addNature(IProject project, String natureId, int updateFlags, throws CoreException { if(!project.hasNature(natureId)) { IProjectDescription description = project.getDescription(); - String[] prevNatures = description.getNatureIds(); - String[] newNatures = new String[prevNatures.length + 1]; - System.arraycopy(prevNatures, 0, newNatures, 1, prevNatures.length); - newNatures[0] = natureId; - description.setNatureIds(newNatures); + var natures = Stream.concat(Stream.of(natureId), Arrays.stream(description.getNatureIds())); + description.setNatureIds(natures.toArray(String[]::new)); project.setDescription(description, updateFlags, monitor); } } + /** + * Creates a problem marker in the pom.xml file at the specified element of the given Plugin-execution with the passed + * message and severity. + *

+ * If the attribute of the execution is specified in a parent pom.xml outside of the workspace the marker is created + * at the parent-element of the request's project. + *

+ * + * @param execution the execution + * @param element the XML-element to mark + * @param request the request of the project being build + * @param problemSeverity the problems severity, one of {@link IMarker#SEVERITY_INFO SEVERITY_INFO}, + * {@link IMarker#SEVERITY_WARNING SEVERITY_WARNING} or {@link IMarker#SEVERITY_ERROR SEVERITY_ERROR} + * @param problemMessage the message of the created problem marker + */ + protected void createProblemMarker(MojoExecution execution, String element, ProjectConfigurationRequest request, + int problemSeverity, String problemMessage) { + SourceLocation location = SourceLocationHelper.findLocation(execution.getPlugin(), element); + + String[] gav = location.getResourceId().split(":"); + IMavenProjectFacade facade = projectManager.getMavenProject(gav[0], gav[1], gav[2]); + if(facade == null) { + // attribute specifying project (probably parent) is not in the workspace. + // The following code returns the location of the project's parent-element. + location = SourceLocationHelper.findLocation(request.mavenProject(), new MojoExecutionKey(execution)); + facade = request.mavenProjectFacade(); + } + MavenProblemInfo problem = new MavenProblemInfo(problemMessage, problemSeverity, location); + markerManager.addErrorMarker(facade.getPom(), IMavenConstants.MARKER_CONFIGURATION_ID, problem); + } + /** * @since 1.4 */ @@ -216,21 +252,11 @@ public boolean equals(Object obj) { if(this == obj) { return true; } - if(obj == null) { - return false; - } - if(getClass() != obj.getClass()) { + if(obj == null || getClass() != obj.getClass()) { return false; } AbstractProjectConfigurator other = (AbstractProjectConfigurator) obj; - if(getId() == null) { - if(other.getId() != null) { - return false; - } - } else if(!getId().equals(other.getId())) { - return false; - } - return true; + return Objects.equals(getId(), other.getId()); } /** @@ -242,7 +268,7 @@ protected List getMojoExecutions(ProjectConfigurationRequest requ Map> configuratorExecutions = getConfiguratorExecutions(projectFacade); - ArrayList executions = new ArrayList<>(); + List executions = new ArrayList<>(); Set executionKeys = configuratorExecutions.get(id); if(executionKeys != null) { @@ -259,25 +285,19 @@ protected List getMojoExecutions(ProjectConfigurationRequest requ */ public static Map> getConfiguratorExecutions(IMavenProjectFacade projectFacade) { Map> configuratorExecutions = new HashMap<>(); - Map> executionMapping = projectFacade.getMojoExecutionMapping(); - for(Map.Entry> entry : executionMapping.entrySet()) { - List metadatas = entry.getValue(); + projectFacade.getMojoExecutionMapping().forEach((key, metadatas) -> { if(metadatas != null) { for(IPluginExecutionMetadata metadata : metadatas) { if(metadata.getAction() == PluginExecutionAction.configurator) { - String configuratorId = LifecycleMappingFactory.getProjectConfiguratorId(metadata); - if(configuratorId != null) { - Set executions = configuratorExecutions.get(configuratorId); - if(executions == null) { - executions = new LinkedHashSet<>(); - configuratorExecutions.put(configuratorId, executions); - } - executions.add(entry.getKey()); + String id = LifecycleMappingFactory.getProjectConfiguratorId(metadata); + if(id != null) { + Set executions = configuratorExecutions.computeIfAbsent(id, i -> new LinkedHashSet<>()); + executions.add(key); } } } } - } + }); return configuratorExecutions; } 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..5739114fe7 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,14 +22,9 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; -import org.eclipse.m2e.core.internal.IMavenConstants; -import org.eclipse.m2e.core.internal.markers.IMavenMarkerManager; -import org.eclipse.m2e.core.internal.markers.MavenProblemInfo; -import org.eclipse.m2e.core.internal.markers.SourceLocation; import org.eclipse.m2e.core.internal.markers.SourceLocationHelper; import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata; import org.eclipse.m2e.core.project.IMavenProjectFacade; -import org.eclipse.m2e.core.project.IMavenProjectRegistry; import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant; import org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator; import org.eclipse.m2e.core.project.configurator.ILifecycleMappingConfiguration; @@ -70,7 +65,8 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni Boolean supportIncremental = maven.getMojoParameterValue(request.mavenProject(), execution, FELIX_PARAM_SUPPORTINCREMENTALBUILD, Boolean.class, monitor); if (supportIncremental == null || !supportIncremental.booleanValue()) { - createWarningMarker(request, execution, SourceLocationHelper.CONFIGURATION, + createProblemMarker(execution, SourceLocationHelper.CONFIGURATION, request, + IMarker.SEVERITY_WARNING, "Incremental updates are currently disabled, set supportIncrementalBuild=true to support automatic manifest updates for this project."); } @@ -82,7 +78,7 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni } if (!hasManifestExecution && !executions.isEmpty()) { MojoExecution execution = executions.get(0); - createWarningMarker(request, execution, "executions", + createProblemMarker(execution, "executions", request, IMarker.SEVERITY_WARNING, "There is currently no execution that generates a manifest, consider adding an execution for one of the following goal: " + (isFelix(execution.getPlugin()) ? FELIX_MANIFEST_GOAL : BND_MANIFEST_GOALS) + "."); } @@ -92,27 +88,6 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni PDEProjectHelper.addPDENature(facade.getProject(), metainfPath, monitor); } - private void createWarningMarker(ProjectConfigurationRequest request, MojoExecution execution, String attribute, - String message) { - createWarningMarker(projectManager, markerManager, request, execution, attribute, message); - } - - static void createWarningMarker(IMavenProjectRegistry projectManager, IMavenMarkerManager markerManager, - ProjectConfigurationRequest request, MojoExecution execution, String attribute, String message) { - SourceLocation location = SourceLocationHelper.findLocation(execution.getPlugin(), attribute); - - String[] gav = location.getResourceId().split(":"); - IMavenProjectFacade facade = projectManager.getMavenProject(gav[0], gav[1], gav[2]); - if (facade == null) { - // attribute specifying project (probably parent) is not in the workspace. - // The following code returns the location of the project's parent-element. - location = SourceLocationHelper.findLocation(request.mavenProject(), new MojoExecutionKey(execution)); - facade = request.mavenProjectFacade(); - } - MavenProblemInfo problem = new MavenProblemInfo(message, IMarker.SEVERITY_WARNING, location); - markerManager.addErrorMarker(facade.getPom(), IMavenConstants.MARKER_LIFECYCLEMAPPING_ID, problem); - } - private boolean isFelixManifestGoal(MojoExecution execution) { return FELIX_MANIFEST_GOAL.equals(execution.getGoal()); } diff --git a/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/TychoPackagingsConfigurator.java b/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/TychoPackagingsConfigurator.java index ef941525d5..f9c5b50f97 100644 --- a/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/TychoPackagingsConfigurator.java +++ b/org.eclipse.m2e.pde.connector/src/org/eclipse/m2e/pde/connector/TychoPackagingsConfigurator.java @@ -9,7 +9,7 @@ * SPDX-License-Identifier: EPL-2.0 * * Contributors: - * Konrad Windszus + * Konrad Windszus - initial API and implementation *******************************************************************************/ package org.eclipse.m2e.pde.connector; @@ -18,6 +18,7 @@ import org.apache.maven.plugin.MojoExecution; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ProjectScope; import org.eclipse.core.runtime.CoreException; @@ -61,10 +62,9 @@ private void applyDsConfiguration(ProjectConfigurationRequest request, IProgress return; } if (mojoExecutions.size() > 1) { - String message = String.format( - "Found more than one execution for plugin %s:%s and goal %s, only consider configuration of this one", - TYCHO_GROUP_ID, TYCHO_DS_PLUGIN_ARTIFACT_ID, GOAL_DECLARATIVE_SERVICES); - createWarningMarker(request, mojoExecutions.get(0), "executions", message); + createProblemMarker(mojoExecutions.get(0), "executions", request, IMarker.SEVERITY_WARNING, + "Found more than one execution for plugin " + TYCHO_GROUP_ID + ":" + TYCHO_DS_PLUGIN_ARTIFACT_ID + + " and goal " + GOAL_DECLARATIVE_SERVICES + ", only consider configuration of this one"); } // first mojo execution is relevant Xpp3Dom dom = mojoExecutions.get(0).getConfiguration(); @@ -85,8 +85,9 @@ private void applyDsConfiguration(ProjectConfigurationRequest request, IProgress if (version != null) { prefs.put(org.eclipse.pde.ds.internal.annotations.Activator.PREF_SPEC_VERSION, version.name()); } else { - String message = "Unsupported DS spec version " + versionValue + " found, using default instead"; - createWarningMarker(request, mojoExecutions.get(0), SourceLocationHelper.CONFIGURATION, message); + createProblemMarker(mojoExecutions.get(0), SourceLocationHelper.CONFIGURATION, request, + IMarker.SEVERITY_WARNING, + "Unsupported DS spec version " + versionValue + " found, using default instead"); } } Xpp3Dom path = dom.getChild("path"); @@ -124,10 +125,4 @@ private List getTychoDsPluginMojoExecutions(ProjectConfigurationR return request.mavenProjectFacade().getMojoExecutions(TYCHO_GROUP_ID, TYCHO_DS_PLUGIN_ARTIFACT_ID, monitor, GOAL_DECLARATIVE_SERVICES); } - - private void createWarningMarker(ProjectConfigurationRequest request, MojoExecution execution, String attribute, - String message) { - PDEMavenBundlePluginConfigurator.createWarningMarker(projectManager, markerManager, request, execution, - attribute, message); - } }