diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultModelBuildingListener.java b/maven-core/src/main/java/org/apache/maven/project/DefaultModelBuildingListener.java index 0a21ec72d8..a1e5ec1661 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultModelBuildingListener.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultModelBuildingListener.java @@ -97,12 +97,15 @@ class DefaultModelBuildingListener projectBuildingHelper.createProjectRealm( model, projectBuildingRequest.getLocalRepository(), pluginRepositories ); - /* - * Update the context class loader so that the container will search the project realm when the model - * builder injects the lifecycle bindings from the packaging next. The class loader is reset by the project - * builder when the project is fully assembled. - */ - Thread.currentThread().setContextClassLoader( projectRealm ); + if ( projectRealm != null ) + { + /* + * Update the context class loader such that the container will search the project realm when the model + * builder injects the lifecycle bindings from the packaging in the next step. The context class loader + * will be reset by the project builder when the project is fully assembled. + */ + Thread.currentThread().setContextClassLoader( projectRealm ); + } } } 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 b5841ecf15..21080b99f7 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 @@ -302,7 +302,23 @@ public class DefaultProjectBuilder .setRemoteRepostories( project.getRemoteArtifactRepositories() ) .setManagedVersionMap( project.getManagedVersionMap() ); - ArtifactResolutionResult result = repositorySystem.resolve( artifactRequest ); + ArtifactResolutionResult result; + + ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); + + try + { + if ( project.getClassRealm() != null ) + { + Thread.currentThread().setContextClassLoader( project.getClassRealm() ); + } + + result = repositorySystem.resolve( artifactRequest ); + } + finally + { + Thread.currentThread().setContextClassLoader( oldContextClassLoader ); + } if ( result.hasExceptions() ) { diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java index baad3054b0..3e1c51ad81 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java @@ -98,7 +98,7 @@ public class DefaultProjectBuildingHelper List remoteRepositories ) throws ArtifactResolutionException { - ClassRealm projectRealm = container.getContainerRealm(); + ClassRealm projectRealm = null; Build build = model.getBuild(); diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingHelper.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingHelper.java index 2a36232719..02f1f86dba 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingHelper.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingHelper.java @@ -55,7 +55,7 @@ public interface ProjectBuildingHelper * @param model The model to create the project realm for, must not be {@code null} * @param localRepository The local repository to use for artifact resolution, must not be {@code null}. * @param remoteRepositories The remote repositories to use for artifact resolution, must not be {@code null}. - * @return The project realm, never {@code null}. + * @return The project realm or {@code null} if the project uses no extensions. * @throws ArtifactResolutionException If any build extension could not be resolved. */ ClassRealm createProjectRealm( Model model, ArtifactRepository localRepository,