From 9e65a7a761cd064e274931197533326c48594f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sat, 29 Jan 2022 12:08:50 +0100 Subject: [PATCH 1/2] MNG-7401 - Make MavenSession#getCurrentProject() using a thread local This allows multiple threads to set/get the current project on the same session object while still having clones maintain there own state. --- .../apache/maven/execution/MavenSession.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) 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 ) { From 49365a657c6e926c53e58e95d0786c1d30c59570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sat, 29 Jan 2022 12:50:51 +0100 Subject: [PATCH 2/2] No more need to clone the session --- .../lifecycle/internal/BuildListCalculator.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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