diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 7e18f1ef3c..616dad1218 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -56,12 +56,14 @@ import org.apache.maven.model.building.ModelBuilder; import org.apache.maven.model.building.ModelBuildingException; import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingResult; +import org.apache.maven.model.building.ModelCache; import org.apache.maven.model.building.ModelProblem; import org.apache.maven.model.building.ModelProcessor; import org.apache.maven.model.building.ModelSource; import org.apache.maven.model.building.StringModelSource; import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.repository.internal.ArtifactDescriptorUtils; +import org.apache.maven.repository.internal.ModelCacheFactory; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.Logger; @@ -111,7 +113,8 @@ public class DefaultProjectBuilder @Requirement private ProjectDependenciesResolver dependencyResolver; - private final ReactorModelCache modelCache = new ReactorModelCache(); + @Requirement + private ModelCacheFactory modelCacheFactory; // ---------------------------------------------------------------------- // MavenProjectBuilder Implementation @@ -122,7 +125,8 @@ public class DefaultProjectBuilder throws ProjectBuildingException { return build( pomFile, new FileModelSource( pomFile ), - new InternalConfig( request, null, useGlobalModelCache() ? getModelCache() : null ) ); + new InternalConfig( request, null, + useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : null ) ); } private boolean useGlobalModelCache() @@ -135,7 +139,8 @@ public class DefaultProjectBuilder throws ProjectBuildingException { return build( null, modelSource, - new InternalConfig( request, null, useGlobalModelCache() ? getModelCache() : null ) ); + new InternalConfig( request, null, + useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : null ) ); } private ProjectBuildingResult build( File pomFile, ModelSource modelSource, InternalConfig config ) @@ -306,7 +311,8 @@ public class DefaultProjectBuilder org.eclipse.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact( artifact ); pomArtifact = ArtifactDescriptorUtils.toPomArtifact( pomArtifact ); - InternalConfig config = new InternalConfig( request, null, useGlobalModelCache() ? getModelCache() : null ); + InternalConfig config = new InternalConfig( request, null, + useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : null ); boolean localProject; @@ -369,7 +375,7 @@ public class DefaultProjectBuilder ReactorModelPool modelPool = new ReactorModelPool(); InternalConfig config = new InternalConfig( request, modelPool, - useGlobalModelCache() ? getModelCache() : new ReactorModelCache() ); + useGlobalModelCache() ? createModelCache( request.getRepositorySession() ) : new ReactorModelCache() ); Map projectIndex = new HashMap<>( 256 ); @@ -1058,9 +1064,9 @@ public class DefaultProjectBuilder private final ReactorModelPool modelPool; - private final ReactorModelCache modelCache; + private final ModelCache modelCache; - InternalConfig( ProjectBuildingRequest request, ReactorModelPool modelPool, ReactorModelCache modelCache ) + InternalConfig( ProjectBuildingRequest request, ReactorModelPool modelPool, ModelCache modelCache ) { this.request = request; this.modelPool = modelPool; @@ -1073,9 +1079,9 @@ public class DefaultProjectBuilder } - private ReactorModelCache getModelCache() + private ModelCache createModelCache( RepositorySystemSession session ) { - return this.modelCache; + return modelCacheFactory.createCache( session ); } } diff --git a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java index 2feeae1a12..db7a17f8ec 100644 --- a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java @@ -146,6 +146,8 @@ public abstract class AbstractCoreMavenComponentTestCase .setSystemProperties( executionProperties ) .setUserProperties( new Properties() ); + initRepoSession( configuration ); + List projects = new ArrayList<>(); if ( pom != null ) @@ -174,8 +176,6 @@ public abstract class AbstractCoreMavenComponentTestCase projects.add( project ); } - initRepoSession( configuration ); - MavenSession session = new MavenSession( getContainer(), configuration.getRepositorySession(), request, new DefaultMavenExecutionResult() ); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java index 8d57b60867..9e56b03b7c 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java @@ -89,6 +89,8 @@ public class DefaultArtifactDescriptorReader private ModelBuilder modelBuilder; + private ModelCacheFactory modelCacheFactory; + public DefaultArtifactDescriptorReader() { // enable no-arg constructor @@ -97,7 +99,8 @@ public class DefaultArtifactDescriptorReader @Inject DefaultArtifactDescriptorReader( RemoteRepositoryManager remoteRepositoryManager, VersionResolver versionResolver, VersionRangeResolver versionRangeResolver, ArtifactResolver artifactResolver, - ModelBuilder modelBuilder, RepositoryEventDispatcher repositoryEventDispatcher ) + ModelBuilder modelBuilder, RepositoryEventDispatcher repositoryEventDispatcher, + ModelCacheFactory modelCacheFactory ) { setRemoteRepositoryManager( remoteRepositoryManager ); setVersionResolver( versionResolver ); @@ -105,6 +108,7 @@ public class DefaultArtifactDescriptorReader setArtifactResolver( artifactResolver ); setModelBuilder( modelBuilder ); setRepositoryEventDispatcher( repositoryEventDispatcher ); + setModelCacheFactory( modelCacheFactory ); } public void initService( ServiceLocator locator ) @@ -119,6 +123,7 @@ public class DefaultArtifactDescriptorReader setModelBuilder( new DefaultModelBuilderFactory().newInstance() ); } setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) ); + setModelCacheFactory( locator.getService( ModelCacheFactory.class ) ); } public DefaultArtifactDescriptorReader setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager ) @@ -162,6 +167,13 @@ public class DefaultArtifactDescriptorReader return this; } + public DefaultArtifactDescriptorReader setModelCacheFactory( ModelCacheFactory modelCacheFactory ) + { + this.modelCacheFactory = Objects.requireNonNull( modelCacheFactory, + "modelCacheFactory cannot be null" ); + return this; + } + public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession session, ArtifactDescriptorRequest request ) throws ArtifactDescriptorException @@ -276,7 +288,7 @@ public class DefaultArtifactDescriptorReader modelRequest.setTwoPhaseBuilding( false ); modelRequest.setSystemProperties( toProperties( session.getSystemProperties() ) ); modelRequest.setUserProperties( toProperties( session.getUserProperties() ) ); - modelRequest.setModelCache( DefaultModelCache.newInstance( session ) ); + modelRequest.setModelCache( modelCacheFactory.createCache( session ) ); modelRequest.setModelResolver( new DefaultModelResolver( session, trace.newChild( modelRequest ), request.getRequestContext(), artifactResolver, versionRangeResolver, remoteRepositoryManager, diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelCacheFactory.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelCacheFactory.java new file mode 100644 index 0000000000..785a4e0235 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelCacheFactory.java @@ -0,0 +1,41 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.model.building.ModelCache; +import org.eclipse.aether.RepositorySystemSession; + +/** + * Default implementation of {@link ModelCacheFactory}. + */ +@Singleton +@Named +public class DefaultModelCacheFactory implements ModelCacheFactory +{ + @Override + public ModelCache createCache( RepositorySystemSession session ) + { + return DefaultModelCache.newInstance( session ); + } + +} diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenRepositorySystemUtils.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenRepositorySystemUtils.java index b073e2d1e5..8fa3767f7a 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenRepositorySystemUtils.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenRepositorySystemUtils.java @@ -79,6 +79,7 @@ public final class MavenRepositorySystemUtils locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class ); locator.addService( MetadataGeneratorFactory.class, SnapshotMetadataGeneratorFactory.class ); locator.addService( MetadataGeneratorFactory.class, VersionsMetadataGeneratorFactory.class ); + locator.addService( ModelCacheFactory.class, DefaultModelCacheFactory.class ); return locator; } diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenResolverModule.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenResolverModule.java index 1e49bfce35..c7120c99a4 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenResolverModule.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenResolverModule.java @@ -56,6 +56,7 @@ public final class MavenResolverModule .to( VersionsMetadataGeneratorFactory.class ).in( Singleton.class ); bind( ModelBuilder.class ).toInstance( new DefaultModelBuilderFactory().newInstance() ); + bind( ModelCacheFactory.class ).to( DefaultModelCacheFactory.class ).in( Singleton.class ); } @Provides diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ModelCacheFactory.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ModelCacheFactory.java new file mode 100644 index 0000000000..0e4299042e --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ModelCacheFactory.java @@ -0,0 +1,33 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.model.building.ModelCache; +import org.eclipse.aether.RepositorySystemSession; + +/** + * Factory for {@link ModelCache} objects. + */ +public interface ModelCacheFactory +{ + + ModelCache createCache( RepositorySystemSession session ); + +}