This is an automated email from the ASF dual-hosted git repository. tibordigana pushed a commit to branch SUREFIRE-1679 in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
commit cae25506a71bbdf196efbe0173d636bb83b7d7e4 Author: Andy Wilkinson <awilkin...@pivotal.io> AuthorDate: Wed Aug 14 08:51:42 2019 +0100 [SUREFIRE-1679] Prevent classpath caching from causing pollution Previously, classpath caching was performed statically. This resulted in the classpath cached by one project for a particular provider being used by a subsequent project. As a result any customizations to the classpath, such as removing duplicate artifacts, would leak out and pollute the classpath used by subsequent projects. This commit prevents the pollution by making the classpath cache instance-scoped so that the cache is only used by a single mojo and, therefore, a single project. --- .../plugin/surefire/AbstractSurefireMojo.java | 41 ++++++++++++++++++---- .../maven/plugin/surefire/ClasspathCache.java | 1 + 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java index 2f179e8..3d03c10 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java @@ -158,6 +158,8 @@ public abstract class AbstractSurefireMojo private static final Platform PLATFORM = new Platform(); private final ProviderDetector providerDetector = new ProviderDetector(); + + private final ClasspathCache classpathCache = new ClasspathCache(); /** * Note: use the legacy system property <em>disableXmlReport</em> set to {@code true} to disable the report. @@ -1787,10 +1789,10 @@ public abstract class AbstractSurefireMojo { Classpath testClasspath = testClasspathWrapper.toClasspath(); - Classpath providerClasspath = ClasspathCache.getCachedClassPath( providerName ); + Classpath providerClasspath = classpathCache.getCachedClassPath( providerName ); if ( providerClasspath == null ) { - providerClasspath = ClasspathCache.setCachedClasspath( providerName, providerArtifacts ); + providerClasspath = classpathCache.setCachedClasspath( providerName, providerArtifacts ); } getConsoleLogger().debug( testClasspath.getLogMessage( "test classpath:" ) ); @@ -1868,10 +1870,10 @@ public abstract class AbstractSurefireMojo { Classpath testClasspath = testClasspathWrapper.toClasspath(); - Classpath providerClasspath = ClasspathCache.getCachedClassPath( providerName ); + Classpath providerClasspath = classpathCache.getCachedClassPath( providerName ); if ( providerClasspath == null ) { - providerClasspath = ClasspathCache.setCachedClasspath( providerName, providerArtifacts ); + providerClasspath = classpathCache.setCachedClasspath( providerName, providerArtifacts ); } ResolvePathsRequest<String> req = ResolvePathsRequest.ofStrings( testClasspath.getClassPath() ) @@ -2629,7 +2631,7 @@ public abstract class AbstractSurefireMojo private Classpath getArtifactClasspath( Artifact surefireArtifact ) { - Classpath existing = ClasspathCache.getCachedClassPath( surefireArtifact.getArtifactId() ); + Classpath existing = classpathCache.getCachedClassPath( surefireArtifact.getArtifactId() ); if ( existing == null ) { List<String> items = new ArrayList<>(); @@ -2643,7 +2645,7 @@ public abstract class AbstractSurefireMojo items.add( artifact.getFile().getAbsolutePath() ); } existing = new Classpath( items ); - ClasspathCache.setCachedClasspath( surefireArtifact.getArtifactId(), existing ); + classpathCache.setCachedClasspath( surefireArtifact.getArtifactId(), existing ); } return existing; } @@ -3839,4 +3841,31 @@ public abstract class AbstractSurefireMojo throw new IllegalArgumentException( "Fork mode " + forkMode + " is not a legal value" ); } } + + private static final class ClasspathCache + { + private final ConcurrentHashMap<String, Classpath> classpaths = new ConcurrentHashMap<>( 4 ); + + private Classpath getCachedClassPath( @Nonnull String artifactId ) + { + return classpaths.get( artifactId ); + } + + private void setCachedClasspath( @Nonnull String key, @Nonnull Classpath classpath ) + { + classpaths.put( key, classpath ); + } + + private Classpath setCachedClasspath( @Nonnull String key, @Nonnull Set<Artifact> artifacts ) + { + Collection<String> files = new ArrayList<>(); + for ( Artifact artifact : artifacts ) + { + files.add( artifact.getFile().getAbsolutePath() ); + } + Classpath classpath = new Classpath( files ); + setCachedClasspath( key, classpath ); + return classpath; + } + } } diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java index 7ba7b54..6dbaf29 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ClasspathCache.java @@ -32,6 +32,7 @@ import javax.annotation.Nonnull; /** * @author Kristian Rosenvold */ +@Deprecated public class ClasspathCache { private static final ConcurrentHashMap<String, Classpath> CLASSPATHS =