diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ContainerCapsule.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ContainerCapsule.java index a8c80ed983..9e57961b10 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ContainerCapsule.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ContainerCapsule.java @@ -28,6 +28,11 @@ import org.apache.maven.api.services.Lookup; * Container capsule. */ public interface ContainerCapsule extends AutoCloseable { + /** + * Updates the existing capsule logging setup. + */ + void updateLogging(LookupContext context); + /** * The {@link Lookup} service backed by container in this capsule. */ diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java index 05d53272d3..ee4ae96b8c 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsule.java @@ -22,22 +22,34 @@ import java.util.Optional; import org.apache.maven.api.services.Lookup; import org.apache.maven.internal.impl.DefaultLookup; -import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.DefaultPlexusContainer; import static java.util.Objects.requireNonNull; +import static org.apache.maven.cling.invoker.Utils.toPlexusLoggingLevel; /** * Container capsule backed by Plexus Container. */ public class PlexusContainerCapsule implements ContainerCapsule { private final ClassLoader previousClassLoader; - private final PlexusContainer plexusContainer; + private final DefaultPlexusContainer plexusContainer; private final Lookup lookup; - public PlexusContainerCapsule(ClassLoader previousClassLoader, PlexusContainer plexusContainer) { + public PlexusContainerCapsule( + LookupContext context, ClassLoader previousClassLoader, DefaultPlexusContainer plexusContainer) { this.previousClassLoader = requireNonNull(previousClassLoader, "previousClassLoader"); this.plexusContainer = requireNonNull(plexusContainer, "plexusContainer"); this.lookup = new DefaultLookup(plexusContainer); + updateLogging(context); + } + + @Override + public void updateLogging(LookupContext context) { + plexusContainer.getLoggerManager().setThresholds(toPlexusLoggingLevel(context.loggerLevel)); + org.slf4j.Logger l = context.loggerFactory.getLogger(this.getClass().getName()); + context.logger = (level, message, error) -> l.atLevel(org.slf4j.event.Level.valueOf(level.name())) + .setCause(error) + .log(message); } @Override diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java index 14673ef88d..9b4d5a40a1 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/PlexusContainerCapsuleFactory.java @@ -73,13 +73,13 @@ public class PlexusContainerCapsuleFactory implements C public ContainerCapsule createContainerCapsule(LookupInvoker invoker, C context) throws InvokerException { try { return new PlexusContainerCapsule( - Thread.currentThread().getContextClassLoader(), container(invoker, context)); + context, Thread.currentThread().getContextClassLoader(), container(invoker, context)); } catch (Exception e) { throw new InvokerException("Failed to create plexus container capsule", e); } } - protected PlexusContainer container(LookupInvoker invoker, C context) throws Exception { + protected DefaultPlexusContainer container(LookupInvoker invoker, C context) throws Exception { ClassWorld classWorld = invoker.protoLookup.lookup(ClassWorld.class); ClassRealm coreRealm = classWorld.getClassRealm("plexus.core"); List extClassPath = parseExtClasspath(context); @@ -139,12 +139,6 @@ public class PlexusContainerCapsuleFactory implements C container.getLoggerManager().setThresholds(toPlexusLoggingLevel(context.loggerLevel)); customizeContainer(context, container); - // refresh logger in case container got customized by spy - org.slf4j.Logger l = context.loggerFactory.getLogger(this.getClass().getName()); - context.logger = (level, message, error) -> l.atLevel(org.slf4j.event.Level.valueOf(level.name())) - .setCause(error) - .log(message); - return container; } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvoker.java index 95132cdcc9..41d57395e7 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvoker.java @@ -74,6 +74,8 @@ public class ResidentMavenInvoker extends MavenInvoker { protected void container(ResidentMavenContext context) throws Exception { if (context.containerCapsule == null) { super.container(context); + } else { + context.containerCapsule.updateLogging(context); } }