Skip to content

Commit

Permalink
Move createProblemMarker() to AbstractProjectConfigurator and clean up
Browse files Browse the repository at this point in the history
This makes createProblemMarker() part of the AbstractProjectConfigurator
API and therefore available to downstream implementors.
  • Loading branch information
HannesWell committed Oct 13, 2022
1 parent 03b0152 commit 8514ffd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
* <p>
* 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.
* </p>
*
* @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
*/
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -242,7 +268,7 @@ protected List<MojoExecution> getMojoExecutions(ProjectConfigurationRequest requ

Map<String, Set<MojoExecutionKey>> configuratorExecutions = getConfiguratorExecutions(projectFacade);

ArrayList<MojoExecution> executions = new ArrayList<>();
List<MojoExecution> executions = new ArrayList<>();

Set<MojoExecutionKey> executionKeys = configuratorExecutions.get(id);
if(executionKeys != null) {
Expand All @@ -259,25 +285,19 @@ protected List<MojoExecution> getMojoExecutions(ProjectConfigurationRequest requ
*/
public static Map<String, Set<MojoExecutionKey>> getConfiguratorExecutions(IMavenProjectFacade projectFacade) {
Map<String, Set<MojoExecutionKey>> configuratorExecutions = new HashMap<>();
Map<MojoExecutionKey, List<IPluginExecutionMetadata>> executionMapping = projectFacade.getMojoExecutionMapping();
for(Map.Entry<MojoExecutionKey, List<IPluginExecutionMetadata>> entry : executionMapping.entrySet()) {
List<IPluginExecutionMetadata> 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<MojoExecutionKey> 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<MojoExecutionKey> executions = configuratorExecutions.computeIfAbsent(id, i -> new LinkedHashSet<>());
executions.add(key);
}
}
}
}
}
});
return configuratorExecutions;
}

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

Expand All @@ -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) + ".");
}
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -124,10 +125,4 @@ private List<MojoExecution> 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);
}
}

0 comments on commit 8514ffd

Please sign in to comment.