Skip to content

Commit

Permalink
Call AbstractMavenLifecycleParticipant after project read
Browse files Browse the repository at this point in the history
Fix #118
  • Loading branch information
laeubi committed Jun 26, 2023
1 parent 1cd7ae9 commit 1cf7ac2
Showing 1 changed file with 72 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -71,11 +72,13 @@

import org.codehaus.plexus.util.dag.CycleDetectedException;

import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.building.ModelProblem;
Expand Down Expand Up @@ -164,6 +167,9 @@ public class ProjectRegistryManager implements ISaveParticipant {

@Reference
private MavenProjectCache mavenProjectCache;

@Reference
private ILog eclipseLog;

private final Set<IMavenProjectChangedListener> projectChangeListeners = new LinkedHashSet<>();

Expand Down Expand Up @@ -219,8 +225,7 @@ public MavenProjectFacade create(IFile pom, boolean load, IProgressMonitor monit
List<Throwable> exceptions = executionResult.getExceptions();
if(exceptions != null) {
for(Throwable ex : exceptions) {
String msg = "Failed to read Maven project: " + ex.getMessage();
log.error(msg, ex);
log.error("Failed to read Maven project: " + ex.getMessage(), ex);
}
}
}
Expand Down Expand Up @@ -371,8 +376,7 @@ Map<IFile, IStatus> refresh(MutableProjectRegistry newState, Collection<IFile> p
}
}

private void refresh(MutableProjectRegistry newState, DependencyResolutionContext context,
IProgressMonitor monitor)
private void refresh(MutableProjectRegistry newState, DependencyResolutionContext context, IProgressMonitor monitor)
throws CoreException {

Set<IFile> allProcessedPoms = new LinkedHashSet<>();
Expand All @@ -384,8 +388,7 @@ private void refresh(MutableProjectRegistry newState, DependencyResolutionContex
// phase 1: build projects without dependencies and populate workspace with known projects
while(!context.isEmpty()) { // context may be augmented, so we need to keep processing
List<IFile> pomsForUpdate = calculateFacadesForUpdate(newState, context, allProcessedPoms::add,
allNewFacades::contains,
monitor);
allNewFacades::contains, monitor);
context.clearErrors(pomsForUpdate);
Map<IFile, MavenProjectFacade> newFacades = readMavenProjectFacades(pomsForUpdate, newState, context, monitor);
for(Entry<IFile, MavenProjectFacade> entry : newFacades.entrySet()) {
Expand Down Expand Up @@ -755,36 +758,49 @@ private Map<IFile, MavenProjectFacade> readMavenProjectFacades(Collection<IFile>
configureExecutionRequest(context.getExecutionRequest(), state,
fileList.size() == 1 ? fileList.iterator().next() : null, resolverConfiguration);
try {
context.execute((ctx, mon) -> {
Map<IFile, File> pomFiles = fileList.stream().filter(IFile::isAccessible)
.collect(Collectors.toMap(Function.identity(), ProjectRegistryManager::toJavaIoFile));
Map<File, MavenExecutionResult> mavenResults = IMavenToolbox.of(ctx).readMavenProjects(pomFiles.values(),
ctx.newProjectBuildingRequest());
Map<IFile, MavenProjectFacade> facades = new HashMap<>(mavenResults.size(), 1.f);
Map<MavenProject, MavenProjectFacade> facadeMap = new HashMap<>();
for(var fileEntry : pomFiles.entrySet()) {
IFile pom = fileEntry.getKey();
File file = fileEntry.getValue();
if(!pom.isAccessible()) {
continue;
context.execute((ctx, mon) -> {
Map<IFile, File> pomFiles = fileList.stream().filter(IFile::isAccessible)
.collect(Collectors.toMap(Function.identity(), ProjectRegistryManager::toJavaIoFile));
ProjectBuildingRequest buildingRequest = ctx.newProjectBuildingRequest();
Map<File, MavenExecutionResult> mavenResults = IMavenToolbox.of(ctx).readMavenProjects(pomFiles.values(),
buildingRequest);
Map<IFile, MavenProjectFacade> facades = new HashMap<>(mavenResults.size(), 1.f);
Map<MavenProject, MavenProjectFacade> facadeMap = new HashMap<>();
for(var fileEntry : pomFiles.entrySet()) {
IFile pom = fileEntry.getKey();
File file = fileEntry.getValue();
if(!pom.isAccessible()) {
continue;
}
MavenExecutionResult mavenResult = mavenResults.get(file);
MavenProject mavenProject = mavenResult.getProject();
MarkerUtils.addEditorHintMarkers(markerManager, pom, mavenProject, IMavenConstants.MARKER_POM_LOADING_ID);
markerManager.addMarkers(pom, IMavenConstants.MARKER_POM_LOADING_ID, mavenResult);
if(mavenProject != null && mavenProject.getArtifact() != null) {
MavenProjectFacade mavenProjectFacade = new MavenProjectFacade(ProjectRegistryManager.this, pom,
mavenProject, resolverConfiguration);
putMavenProject(mavenProjectFacade, mavenProject); // maintain maven project cache
facadeMap.put(mavenProject, mavenProjectFacade);
}
}
MavenExecutionResult mavenResult = mavenResults.get(file);
MavenProject mavenProject = mavenResult.getProject();
MarkerUtils.addEditorHintMarkers(markerManager, pom, mavenProject, IMavenConstants.MARKER_POM_LOADING_ID);
markerManager.addMarkers(pom, IMavenConstants.MARKER_POM_LOADING_ID, mavenResult);
if(mavenProject != null && mavenProject.getArtifact() != null) {
MavenProjectFacade mavenProjectFacade = new MavenProjectFacade(ProjectRegistryManager.this, pom,
mavenProject, resolverConfiguration);
putMavenProject(mavenProjectFacade, mavenProject); // maintain maven project cache
facadeMap.put(mavenProject, mavenProjectFacade);
Set<AbstractMavenLifecycleParticipant> executed = new HashSet<>();
List<MavenProject> projects = facadeMap.keySet().stream().toList();
executeParticipants(ctx, projects, executed, buildingRequest);
facadeMap.values().forEach(facade -> {
try {
facade.createExecutionContext().execute((projectContext, y) -> {
executeParticipants(projectContext, projects, executed, buildingRequest);
return null;
}, null);
} catch(CoreException ex) {
}
});
for(var mp : getSortedProjects(facadeMap.keySet())) {
MavenProjectFacade facade = facadeMap.get(mp);
result.put(facade.getPom(), facade);
}
}
for(var mp : getSortedProjects(facadeMap.keySet())) {
MavenProjectFacade facade = facadeMap.get(mp);
result.put(facade.getPom(), facade);
}
return facades;
}, subMonitor.split(1));
return facades;
}, subMonitor.split(1));
} catch(CoreException e) {
for(IFile file : fileList) {
resolutionContext.setStatus(file, e.getStatus());
Expand All @@ -794,6 +810,28 @@ private Map<IFile, MavenProjectFacade> readMavenProjectFacades(Collection<IFile>
return result;
}

private void executeParticipants(IMavenExecutionContext ctx, List<MavenProject> projects,
Set<AbstractMavenLifecycleParticipant> executed, ProjectBuildingRequest buildingRequest) throws CoreException {
Collection<AbstractMavenLifecycleParticipant> participants = ctx.getComponentLookup()
.lookupCollection(AbstractMavenLifecycleParticipant.class);
for(AbstractMavenLifecycleParticipant participant : participants) {
if(executed.add(participant)) {
MavenSession session = ctx.getSession();
MavenSession clone = session.clone();
clone.setProjects(projects);
//FIXME without this call there are sometime NPE see https://github.com/eclipse-m2e/m2e-core/issues/118#issuecomment-1607118196
clone.getProjectBuildingRequest();
try {
participant.afterProjectsRead(clone);
} catch(Exception ex) {
eclipseLog.error(
"Executing AbstractMavenLifecycleParticipant " + participant.getClass().getName() + " failed",
ex);
}
}
}
}

private Collection<MavenProject> getSortedProjects(Collection<MavenProject> projects) {
if(projects.size() <= 1) {
return projects;
Expand Down Expand Up @@ -950,7 +988,6 @@ private MavenExecutionRequest configureExecutionRequest(MavenExecutionRequest re
userProperties.putAll(addProperties);
}


// eclipse workspace repository implements both workspace dependency resolution
// and inter-module dependency resolution for multi-module projects.

Expand Down

0 comments on commit 1cf7ac2

Please sign in to comment.