Skip to content

Commit

Permalink
Setup a fresh execution to account for the current context
Browse files Browse the repository at this point in the history
Fix #1150
  • Loading branch information
laeubi committed Dec 31, 2022
1 parent be46e68 commit 82245eb
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.internal.DependencyContext;
import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
import org.apache.maven.lifecycle.internal.MojoExecutor;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.LegacySupport;
Expand Down Expand Up @@ -325,14 +326,18 @@ private static void executeMojo(MavenSession session, MojoExecution execution, I
artifacts.put(project, new LinkedHashSet<>(project.getArtifacts()));
snapshots.put(project, MavenProjectMutableState.takeSnapshot(project));
}
MojoExecution clone = new MojoExecution(execution.getPlugin(), execution.getGoal(), execution.getExecutionId());
try {
MavenProject currentProject = session.getCurrentProject();
LifecycleExecutionPlanCalculator executionPlanCalculator = lookup.lookup(LifecycleExecutionPlanCalculator.class);
executionPlanCalculator.setupMojoExecution(session, currentProject, clone);
MojoExecutor mojoExecutor = lookup.lookup(MojoExecutor.class);
DependencyContext dependencyContext = mojoExecutor.newDependencyContext(session, List.of(execution));
mojoExecutor.ensureDependenciesAreResolved(execution.getMojoDescriptor(), session, dependencyContext);
lookup.lookup(BuildPluginManager.class).executeMojo(session, execution);
DependencyContext dependencyContext = mojoExecutor.newDependencyContext(session, List.of(clone));
mojoExecutor.ensureDependenciesAreResolved(clone.getMojoDescriptor(), session, dependencyContext);
BuildPluginManager buildPluginManager = lookup.lookup(BuildPluginManager.class);
buildPluginManager.executeMojo(session, clone);
} catch(Exception ex) {
session.getResult().addException(ex);
throw new CoreException(Status.error("Failed to execute mojo", ex));
throw new CoreException(Status.error("Failed to execute mojo " + clone, ex));
} finally {
for(MavenProject project : session.getProjects()) {
project.setArtifactFilter(null);
Expand Down

0 comments on commit 82245eb

Please sign in to comment.