Skip to content

Commit

Permalink
Fixes #8: m2eclipse 1.x and 2.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed Nov 29, 2024
1 parent a12e389 commit 80d0cc7
Showing 1 changed file with 12 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.google.eclipse.protobuf.ui.scoping;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
Expand All @@ -19,52 +20,21 @@ public final class MavenResolverHelper {

public static List<DirectoryPath> resolveMavenDependencies(IProject aProject) throws CoreException {
if (aProject.isOpen() && aProject.hasNature(IMavenConstants.NATURE_ID)) {
final IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(aProject);

final IMavenExecutionContext context;
final IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(aProject);

try {
Class<?> toolboxClass = MavenResolverHelper.class.getClassLoader()
.loadClass("org.eclipse.m2e.core.internal.IMavenToolbox");

// If the class can be loaded, m2e 2.x is installed. We compile against 1.x (because we compile
// against Neon, which is the minimum necessary for this plugin) so we must implement the slight
// breaking changes in the dependency resolution via reflection.
try {
context = (IMavenExecutionContext) facade.getClass().getMethod("createExecutionContext", new Class[0])
.invoke(facade, new Object[0]);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e);
}
context.execute((ctx, mon) -> {
ProjectBuildingRequest configuration = ctx.newProjectBuildingRequest();
configuration.setProject(facade.getMavenProject());
configuration.setResolveDependencies(true);
try {
Object toolbox = toolboxClass.getMethod("of", new Class[] { IMavenExecutionContext.class })
.invoke(null, new Object[] { ctx });
return toolboxClass
.getMethod("readMavenProject",
new Class[] { java.io.File.class, ProjectBuildingRequest.class })
.invoke(toolbox, new Object[] { facade.getPomFile(), configuration });
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException exc) {
throw new RuntimeException(exc);
}
}, null);

} catch(ClassNotFoundException exc) {
// Expected in case of m2e 1.x which does not yet have the IMavenToolbox.
// We can directly invoke it here.
ProjectBuildingRequest configuration = MavenPlugin.getMaven().getExecutionContext().newProjectBuildingRequest();
final IMaven maven = MavenPlugin.getMaven();
final IMavenExecutionContext context = maven.createExecutionContext();
context.execute((ctx, mon) -> {
final ProjectBuildingRequest configuration = ctx.newProjectBuildingRequest();
configuration.setProject(facade.getMavenProject());
configuration.setResolveDependencies(true);
MavenPlugin.getMaven().readMavenProject(facade.getPomFile(), configuration);
}
return MavenPlugin.getMaven().readMavenProject(facade.getPomFile(), configuration);
}, null);

final MavenProject mavenProject = facade.getMavenProject(null);

ArrayList<DirectoryPath> paths = new ArrayList<>();
for (Artifact artifact : facade.getMavenProject().getArtifacts()) {
for (Artifact artifact : mavenProject.getArtifacts()) {
if (artifact.getFile() != null) {
paths.add(DirectoryPath.parse(artifact.getFile().getAbsolutePath(), aProject));
artifact.getFile();
Expand Down

0 comments on commit 80d0cc7

Please sign in to comment.