Skip to content

Commit 5c36bf5

Browse files
committed
[MNG-7312] Revert ThreadLocal approach from MNG-6843 and MNG-7251
Revert "[MNG-7251] Fix threadLocalArtifactsHolder leaking into cloned project" This reverts commit 4e5b3d5. Revert "[MNG-6843] Parallel build fails due to missing JAR artifacts in compilePath" This reverts commit 76d5f0d. === This closes #595
1 parent fb5f3f5 commit 5c36bf5

File tree

2 files changed

+48
-97
lines changed

2 files changed

+48
-97
lines changed

maven-core/src/main/java/org/apache/maven/project/MavenProject.java

+48-54
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@
9292
public class MavenProject
9393
implements Cloneable
9494
{
95+
9596
private static final Logger LOGGER = LoggerFactory.getLogger( MavenProject.class );
97+
9698
public static final String EMPTY_PROJECT_GROUP_ID = "unknown";
99+
97100
public static final String EMPTY_PROJECT_ARTIFACT_ID = "empty-project";
101+
98102
public static final String EMPTY_PROJECT_VERSION = "0";
99103

100104
private Model model;
@@ -107,6 +111,10 @@ public class MavenProject
107111

108112
private Set<Artifact> resolvedArtifacts;
109113

114+
private ArtifactFilter artifactFilter;
115+
116+
private Set<Artifact> artifacts;
117+
110118
private Artifact parentArtifact;
111119

112120
private Set<Artifact> pluginArtifacts;
@@ -143,7 +151,8 @@ public class MavenProject
143151

144152
private Artifact artifact;
145153

146-
private ThreadLocal<ArtifactsHolder> threadLocalArtifactsHolder = newThreadLocalArtifactsHolder();
154+
// calculated.
155+
private Map<String, Artifact> artifactMap;
147156

148157
private Model originalModel;
149158

@@ -176,21 +185,12 @@ public class MavenProject
176185
public MavenProject()
177186
{
178187
Model model = new Model();
188+
179189
model.setGroupId( EMPTY_PROJECT_GROUP_ID );
180190
model.setArtifactId( EMPTY_PROJECT_ARTIFACT_ID );
181191
model.setVersion( EMPTY_PROJECT_VERSION );
182-
setModel( model );
183-
}
184192

185-
private static ThreadLocal<ArtifactsHolder> newThreadLocalArtifactsHolder()
186-
{
187-
return new ThreadLocal<ArtifactsHolder>()
188-
{
189-
protected ArtifactsHolder initialValue()
190-
{
191-
return new ArtifactsHolder();
192-
}
193-
};
193+
setModel( model );
194194
}
195195

196196
public MavenProject( Model model )
@@ -695,11 +695,10 @@ public void addLicense( License license )
695695

696696
public void setArtifacts( Set<Artifact> artifacts )
697697
{
698-
ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get();
699-
artifactsHolder.artifacts = artifacts;
698+
this.artifacts = artifacts;
700699

701700
// flush the calculated artifactMap
702-
artifactsHolder.artifactMap = null;
701+
artifactMap = null;
703702
}
704703

705704
/**
@@ -712,36 +711,34 @@ public void setArtifacts( Set<Artifact> artifacts )
712711
*/
713712
public Set<Artifact> getArtifacts()
714713
{
715-
ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get();
716-
if ( artifactsHolder.artifacts == null )
714+
if ( artifacts == null )
717715
{
718-
if ( artifactsHolder.artifactFilter == null || resolvedArtifacts == null )
716+
if ( artifactFilter == null || resolvedArtifacts == null )
719717
{
720-
artifactsHolder.artifacts = new LinkedHashSet<>();
718+
artifacts = new LinkedHashSet<>();
721719
}
722720
else
723721
{
724-
artifactsHolder.artifacts = new LinkedHashSet<>( resolvedArtifacts.size() * 2 );
722+
artifacts = new LinkedHashSet<>( resolvedArtifacts.size() * 2 );
725723
for ( Artifact artifact : resolvedArtifacts )
726724
{
727-
if ( artifactsHolder.artifactFilter.include( artifact ) )
725+
if ( artifactFilter.include( artifact ) )
728726
{
729-
artifactsHolder.artifacts.add( artifact );
727+
artifacts.add( artifact );
730728
}
731729
}
732730
}
733731
}
734-
return artifactsHolder.artifacts;
732+
return artifacts;
735733
}
736734

737735
public Map<String, Artifact> getArtifactMap()
738736
{
739-
ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get();
740-
if ( artifactsHolder.artifactMap == null )
737+
if ( artifactMap == null )
741738
{
742-
artifactsHolder.artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() );
739+
artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() );
743740
}
744-
return artifactsHolder.artifactMap;
741+
return artifactMap;
745742
}
746743

747744
public void setPluginArtifacts( Set<Artifact> pluginArtifacts )
@@ -1186,8 +1183,7 @@ public MavenProject clone()
11861183
{
11871184
throw new UnsupportedOperationException( e );
11881185
}
1189-
// clone must have it's own TL, otherwise the artifacts are intermingled!
1190-
clone.threadLocalArtifactsHolder = newThreadLocalArtifactsHolder();
1186+
11911187
clone.deepCopy( this );
11921188

11931189
return clone;
@@ -1230,7 +1226,6 @@ private void deepCopy( MavenProject project )
12301226
// copy fields
12311227
file = project.file;
12321228
basedir = project.basedir;
1233-
threadLocalArtifactsHolder.set( project.threadLocalArtifactsHolder.get().copy() );
12341229

12351230
// don't need a deep copy, they don't get modified or added/removed to/from - but make them unmodifiable to be
12361231
// sure!
@@ -1239,6 +1234,11 @@ private void deepCopy( MavenProject project )
12391234
setDependencyArtifacts( Collections.unmodifiableSet( project.getDependencyArtifacts() ) );
12401235
}
12411236

1237+
if ( project.getArtifacts() != null )
1238+
{
1239+
setArtifacts( Collections.unmodifiableSet( project.getArtifacts() ) );
1240+
}
1241+
12421242
if ( project.getParentFile() != null )
12431243
{
12441244
parentFile = new File( project.getParentFile().getAbsolutePath() );
@@ -1433,9 +1433,8 @@ public DependencyFilter getExtensionDependencyFilter()
14331433
public void setResolvedArtifacts( Set<Artifact> artifacts )
14341434
{
14351435
this.resolvedArtifacts = ( artifacts != null ) ? artifacts : Collections.<Artifact>emptySet();
1436-
ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get();
1437-
artifactsHolder.artifacts = null;
1438-
artifactsHolder.artifactMap = null;
1436+
this.artifacts = null;
1437+
this.artifactMap = null;
14391438
}
14401439

14411440
/**
@@ -1448,10 +1447,9 @@ public void setResolvedArtifacts( Set<Artifact> artifacts )
14481447
*/
14491448
public void setArtifactFilter( ArtifactFilter artifactFilter )
14501449
{
1451-
ArtifactsHolder artifactsHolder = threadLocalArtifactsHolder.get();
1452-
artifactsHolder.artifactFilter = artifactFilter;
1453-
artifactsHolder.artifacts = null;
1454-
artifactsHolder.artifactMap = null;
1450+
this.artifactFilter = artifactFilter;
1451+
this.artifacts = null;
1452+
this.artifactMap = null;
14551453
}
14561454

14571455
/**
@@ -1481,7 +1479,13 @@ public void addLifecyclePhase( String lifecyclePhase )
14811479

14821480
// ----------------------------------------------------------------------------------------------------------------
14831481
//
1484-
// D E P R E C A T E D - Everything below will be removed for Maven 4.0.0
1482+
//
1483+
// D E P R E C A T E D
1484+
//
1485+
//
1486+
// ----------------------------------------------------------------------------------------------------------------
1487+
//
1488+
// Everything below will be removed for Maven 4.0.0
14851489
//
14861490
// ----------------------------------------------------------------------------------------------------------------
14871491

@@ -1502,6 +1506,7 @@ public String getModulePathAdjustment( MavenProject moduleProject )
15021506
if ( moduleFile != null )
15031507
{
15041508
File moduleDir = moduleFile.getCanonicalFile().getParentFile();
1509+
15051510
module = moduleDir.getName();
15061511
}
15071512

@@ -1822,6 +1827,7 @@ public Reporting getReporting()
18221827
public void setReportArtifacts( Set<Artifact> reportArtifacts )
18231828
{
18241829
this.reportArtifacts = reportArtifacts;
1830+
18251831
reportArtifactMap = null;
18261832
}
18271833

@@ -1838,13 +1844,15 @@ public Map<String, Artifact> getReportArtifactMap()
18381844
{
18391845
reportArtifactMap = ArtifactUtils.artifactMapByVersionlessId( getReportArtifacts() );
18401846
}
1847+
18411848
return reportArtifactMap;
18421849
}
18431850

18441851
@Deprecated
18451852
public void setExtensionArtifacts( Set<Artifact> extensionArtifacts )
18461853
{
18471854
this.extensionArtifacts = extensionArtifacts;
1855+
18481856
extensionArtifactMap = null;
18491857
}
18501858

@@ -1861,6 +1869,7 @@ public Map<String, Artifact> getExtensionArtifactMap()
18611869
{
18621870
extensionArtifactMap = ArtifactUtils.artifactMapByVersionlessId( getExtensionArtifacts() );
18631871
}
1872+
18641873
return extensionArtifactMap;
18651874
}
18661875

@@ -1878,6 +1887,7 @@ public List<ReportPlugin> getReportPlugins()
18781887
public Xpp3Dom getReportConfiguration( String pluginGroupId, String pluginArtifactId, String reportSetId )
18791888
{
18801889
Xpp3Dom dom = null;
1890+
18811891
// ----------------------------------------------------------------------
18821892
// I would like to be able to lookup the Mojo object using a key but
18831893
// we have a limitation in modello that will be remedied shortly. So
@@ -1981,20 +1991,4 @@ public void setProjectBuildingRequest( ProjectBuildingRequest projectBuildingReq
19811991
{
19821992
this.projectBuilderConfiguration = projectBuildingRequest;
19831993
}
1984-
1985-
private static class ArtifactsHolder
1986-
{
1987-
private ArtifactFilter artifactFilter;
1988-
private Set<Artifact> artifacts;
1989-
private Map<String, Artifact> artifactMap;
1990-
1991-
ArtifactsHolder copy()
1992-
{
1993-
ArtifactsHolder copy = new ArtifactsHolder();
1994-
copy.artifactFilter = artifactFilter;
1995-
copy.artifacts = artifacts != null ? new LinkedHashSet<>( artifacts ) : null;
1996-
copy.artifactMap = artifactMap != null ? new LinkedHashMap<>( artifactMap ) : null;
1997-
return copy;
1998-
}
1999-
}
20001994
}

maven-core/src/test/java/org/apache/maven/project/MavenProjectTest.java

-43
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,13 @@
2121

2222
import java.io.File;
2323
import java.io.IOException;
24-
import java.util.Collections;
2524
import java.util.List;
2625
import java.util.Map;
27-
import java.util.Set;
28-
import java.util.concurrent.atomic.AtomicReference;
2926

30-
import org.apache.maven.artifact.Artifact;
3127
import org.apache.maven.model.DependencyManagement;
3228
import org.apache.maven.model.Model;
3329
import org.apache.maven.model.Parent;
3430
import org.apache.maven.model.Profile;
35-
import org.mockito.Mockito;
3631

3732
public class MavenProjectTest
3833
extends AbstractMavenProjectTestCase
@@ -193,44 +188,6 @@ public void testCloneWithBaseDir()
193188
assertEquals( "Base directory is preserved across clone", projectToClone.getBasedir(), clonedProject.getBasedir() );
194189
}
195190

196-
public void testCloneWithArtifacts()
197-
throws InterruptedException
198-
{
199-
Artifact initialArtifact = Mockito.mock( Artifact.class, "initialArtifact" );
200-
MavenProject originalProject = new MavenProject();
201-
originalProject.setArtifacts( Collections.singleton( initialArtifact ) );
202-
assertEquals( "Sanity check: originalProject returns artifact that has just been set",
203-
Collections.singleton( initialArtifact ), originalProject.getArtifacts() );
204-
205-
final MavenProject clonedProject = originalProject.clone();
206-
207-
assertEquals( "Cloned project returns the artifact that was set for the original project",
208-
Collections.singleton( initialArtifact ), clonedProject.getArtifacts() );
209-
210-
Artifact anotherArtifact = Mockito.mock( Artifact.class, "anotherArtifact" );
211-
clonedProject.setArtifacts( Collections.singleton( anotherArtifact ) );
212-
assertEquals( "Sanity check: clonedProject returns artifact that has just been set",
213-
Collections.singleton( anotherArtifact ), clonedProject.getArtifacts() );
214-
215-
assertEquals( "Original project returns the artifact that was set initially (not the one for clonedProject)",
216-
Collections.singleton( initialArtifact ), originalProject.getArtifacts() );
217-
218-
final AtomicReference<Set<Artifact>> artifactsFromThread = new AtomicReference<>();
219-
Thread thread = new Thread( new Runnable()
220-
{
221-
@Override
222-
public void run()
223-
{
224-
artifactsFromThread.set( clonedProject.getArtifacts() );
225-
}
226-
} );
227-
thread.start();
228-
thread.join();
229-
230-
assertEquals( "Another thread does not see the same artifacts",
231-
Collections.emptySet(), artifactsFromThread.get() );
232-
}
233-
234191
public void testUndefinedOutputDirectory()
235192
throws Exception
236193
{

0 commit comments

Comments
 (0)