diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java index c2f0c89e862e..cc6d0ece92bb 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java @@ -55,7 +55,7 @@ public class MavenSession private Properties executionProperties; - private MavenProject currentProject; + private ThreadLocal currentProject = new ThreadLocal<>(); /** * These projects have already been topologically sorted in the {@link org.apache.maven.Maven} component before @@ -90,20 +90,21 @@ public void setProjects( List projects ) { if ( !projects.isEmpty() ) { - this.currentProject = projects.get( 0 ); - this.topLevelProject = currentProject; + MavenProject first = projects.get( 0 ); + this.topLevelProject = first; + this.currentProject = ThreadLocal.withInitial( () -> first ); for ( MavenProject project : projects ) { if ( project.isExecutionRoot() ) { - topLevelProject = project; + this.topLevelProject = project; break; } } } else { - this.currentProject = null; + this.currentProject = new ThreadLocal<>(); this.topLevelProject = null; } this.projects = projects; @@ -164,12 +165,12 @@ public MavenExecutionRequest getRequest() public void setCurrentProject( MavenProject currentProject ) { - this.currentProject = currentProject; + this.currentProject.set( currentProject ); } public MavenProject getCurrentProject() { - return currentProject; + return currentProject.get(); } public ProjectBuildingRequest getProjectBuildingRequest() @@ -240,7 +241,12 @@ public MavenSession clone() { try { - return (MavenSession) super.clone(); + MavenSession clone = (MavenSession) super.clone(); + // the default must become the current project of the thread that clones this + MavenProject current = getCurrentProject(); + // we replace the thread local of the clone to prevent write through and enforce the new default value + clone.currentProject = ThreadLocal.withInitial( () -> current ); + return clone; } catch ( CloneNotSupportedException e ) { diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/BuildListCalculator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/BuildListCalculator.java index 9000c9301eae..d61279d7f969 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/BuildListCalculator.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/BuildListCalculator.java @@ -61,9 +61,16 @@ public ProjectBuildList calculateProjectBuilds( MavenSession session, List