From 76d5f0d942f52650d3bdf775b6af42d23d69066b Mon Sep 17 00:00:00 2001 From: Falko Modler Date: Fri, 25 Jun 2021 19:28:40 +0200 Subject: [PATCH] [MNG-6843] Parallel build fails due to missing JAR artifacts in compilePath Signed-off-by: rfscholte (cherry picked from commit 73e00ed85df84ba0c557dd020740812b2453f2d3) This closes #482 --- .../apache/maven/project/MavenProject.java | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index db0f4a90116f..157b7a0f0a55 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -92,13 +92,9 @@ public class MavenProject implements Cloneable { - private static final Logger LOGGER = LoggerFactory.getLogger( MavenProject.class ); - public static final String EMPTY_PROJECT_GROUP_ID = "unknown"; - public static final String EMPTY_PROJECT_ARTIFACT_ID = "empty-project"; - public static final String EMPTY_PROJECT_VERSION = "0"; private Model model; @@ -111,10 +107,6 @@ public class MavenProject private Set resolvedArtifacts; - private ArtifactFilter artifactFilter; - - private Set artifacts; - private Artifact parentArtifact; private Set pluginArtifacts; @@ -151,8 +143,13 @@ public class MavenProject private Artifact artifact; - // calculated. - private Map artifactMap; + private final ThreadLocal threadLocalArtifactsHolder = new ThreadLocal() + { + protected ArtifactsHolder initialValue() + { + return new ArtifactsHolder(); + } + }; private Model originalModel; @@ -185,11 +182,9 @@ public class MavenProject public MavenProject() { Model model = new Model(); - model.setGroupId( EMPTY_PROJECT_GROUP_ID ); model.setArtifactId( EMPTY_PROJECT_ARTIFACT_ID ); model.setVersion( EMPTY_PROJECT_VERSION ); - setModel( model ); } @@ -695,10 +690,11 @@ public void addLicense( License license ) public void setArtifacts( Set artifacts ) { - this.artifacts = artifacts; + ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get(); + artifactsHolder.artifacts = artifacts; // flush the calculated artifactMap - artifactMap = null; + artifactsHolder.artifactMap = null; } /** @@ -711,34 +707,36 @@ public void setArtifacts( Set artifacts ) */ public Set getArtifacts() { - if ( artifacts == null ) + ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get(); + if ( artifactsHolder.artifacts == null ) { - if ( artifactFilter == null || resolvedArtifacts == null ) + if ( artifactsHolder.artifactFilter == null || resolvedArtifacts == null ) { - artifacts = new LinkedHashSet<>(); + artifactsHolder.artifacts = new LinkedHashSet<>(); } else { - artifacts = new LinkedHashSet<>( resolvedArtifacts.size() * 2 ); + artifactsHolder.artifacts = new LinkedHashSet<>( resolvedArtifacts.size() * 2 ); for ( Artifact artifact : resolvedArtifacts ) { - if ( artifactFilter.include( artifact ) ) + if ( artifactsHolder.artifactFilter.include( artifact ) ) { - artifacts.add( artifact ); + artifactsHolder.artifacts.add( artifact ); } } } } - return artifacts; + return artifactsHolder.artifacts; } public Map getArtifactMap() { - if ( artifactMap == null ) + ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get(); + if ( artifactsHolder.artifactMap == null ) { - artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() ); + artifactsHolder.artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() ); } - return artifactMap; + return artifactsHolder.artifactMap; } public void setPluginArtifacts( Set pluginArtifacts ) @@ -1433,8 +1431,9 @@ public DependencyFilter getExtensionDependencyFilter() public void setResolvedArtifacts( Set artifacts ) { this.resolvedArtifacts = ( artifacts != null ) ? artifacts : Collections.emptySet(); - this.artifacts = null; - this.artifactMap = null; + ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get(); + artifactsHolder.artifacts = null; + artifactsHolder.artifactMap = null; } /** @@ -1447,9 +1446,10 @@ public void setResolvedArtifacts( Set artifacts ) */ public void setArtifactFilter( ArtifactFilter artifactFilter ) { - this.artifactFilter = artifactFilter; - this.artifacts = null; - this.artifactMap = null; + ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get(); + artifactsHolder.artifactFilter = artifactFilter; + artifactsHolder.artifacts = null; + artifactsHolder.artifactMap = null; } /** @@ -1844,7 +1844,6 @@ public Map getReportArtifactMap() { reportArtifactMap = ArtifactUtils.artifactMapByVersionlessId( getReportArtifacts() ); } - return reportArtifactMap; } @@ -1869,7 +1868,6 @@ public Map getExtensionArtifactMap() { extensionArtifactMap = ArtifactUtils.artifactMapByVersionlessId( getExtensionArtifacts() ); } - return extensionArtifactMap; } @@ -1991,4 +1989,11 @@ public void setProjectBuildingRequest( ProjectBuildingRequest projectBuildingReq { this.projectBuilderConfiguration = projectBuildingRequest; } + + private static class ArtifactsHolder + { + private ArtifactFilter artifactFilter; + private Set artifacts; + private Map artifactMap; + } }