Repository: quarkusio/quarkus
Issue: Memory leak via MojoLogger.logSupplier

`MojoLogger.logSupplier` is a static field holding a reference to the following chain of refs: 

`BuildMojo` -> `BuildMojo.repoSession` -> `DefaultRepositorySystemSession.cache`

When building Camel Quarkus with [mvnd](https://github.com/mvndaemon/mvnd), this prevents the cache (900+ MB on the heap) from being garbage collected after the build.

A PR follows.

Changed files:
devtools/maven/src/main/java/io/quarkus/maven/BuildMojo.java, devtools/maven/src/main/java/io/quarkus/maven/GenerateConfigMojo.java, devtools/maven/src/main/java/io/quarkus/maven/NativeImageMojo.java, devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java, devtools/maven/src/main/java/io/quarkus/maven/MojoLogger.java

diff --git a/devtools/maven/src/main/java/io/quarkus/maven/BuildMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/BuildMojo.java
index fade91e4779..34d4afebd31 100644
--- a/devtools/maven/src/main/java/io/quarkus/maven/BuildMojo.java
+++ b/devtools/maven/src/main/java/io/quarkus/maven/BuildMojo.java
@@ -9,6 +9,7 @@
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
@@ -120,4 +121,11 @@ protected void doExecute() throws MojoExecutionException {
             }
         }
     }
+
+    @Override
+    public void setLog(Log log) {
+        super.setLog(log);
+        MojoLogger.delegate = log;
+    }
+
 }
diff --git a/devtools/maven/src/main/java/io/quarkus/maven/GenerateConfigMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/GenerateConfigMojo.java
index 20a22c8e99c..95ea468a7ca 100644
--- a/devtools/maven/src/main/java/io/quarkus/maven/GenerateConfigMojo.java
+++ b/devtools/maven/src/main/java/io/quarkus/maven/GenerateConfigMojo.java
@@ -12,6 +12,7 @@
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
@@ -86,10 +87,6 @@ public class GenerateConfigMojo extends AbstractMojo {
     @Parameter(defaultValue = "${file}")
     private String file;
 
-    public GenerateConfigMojo() {
-        MojoLogger.logSupplier = this::getLog;
-    }
-
     @Override
     public void execute() throws MojoExecutionException {
 
@@ -152,4 +149,10 @@ public void execute() throws MojoExecutionException {
             throw new MojoExecutionException("Failed to generate config file", e);
         }
     }
+
+    @Override
+    public void setLog(Log log) {
+        super.setLog(log);
+        MojoLogger.delegate = log;
+    }
 }
diff --git a/devtools/maven/src/main/java/io/quarkus/maven/MojoLogger.java b/devtools/maven/src/main/java/io/quarkus/maven/MojoLogger.java
index a903de27547..dd03ff05971 100644
--- a/devtools/maven/src/main/java/io/quarkus/maven/MojoLogger.java
+++ b/devtools/maven/src/main/java/io/quarkus/maven/MojoLogger.java
@@ -3,7 +3,6 @@
 import java.text.MessageFormat;
 import java.util.Collections;
 import java.util.Map;
-import java.util.function.Supplier;
 
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.shared.utils.logging.MessageBuilder;
@@ -17,7 +16,7 @@
 public class MojoLogger implements LoggerProvider {
     static final Object[] NO_PARAMS = new Object[0];
 
-    public static volatile Supplier<Log> logSupplier;
+    public static volatile Log delegate;
 
     @Override
     public Logger getLogger(final String name) {
@@ -25,9 +24,8 @@ public Logger getLogger(final String name) {
             @Override
             protected void doLog(final Level level, final String loggerClassName, final Object message,
                     final Object[] parameters, final Throwable thrown) {
-                final Supplier<Log> logSupplier = MojoLogger.logSupplier;
-                if (logSupplier != null) {
-                    Log log = logSupplier.get();
+                final Log log = delegate;
+                if (log != null) {
                     String text;
                     if (parameters == null || parameters.length == 0) {
                         text = String.valueOf(message);
@@ -47,9 +45,8 @@ protected void doLog(final Level level, final String loggerClassName, final Obje
             @Override
             protected void doLogf(final Level level, final String loggerClassName, final String format,
                     final Object[] parameters, final Throwable thrown) {
-                final Supplier<Log> logSupplier = MojoLogger.logSupplier;
-                if (logSupplier != null) {
-                    Log log = logSupplier.get();
+                final Log log = delegate;
+                if (log != null) {
                     String text;
                     if (parameters == null) {
                         try {
@@ -73,10 +70,9 @@ protected void doLogf(final Level level, final String loggerClassName, final Str
 
             @Override
             public boolean isEnabled(final Level level) {
-                final Supplier<Log> logSupplier = MojoLogger.logSupplier;
-                if (logSupplier == null)
+                final Log log = delegate;
+                if (log == null)
                     return false;
-                Log log = logSupplier.get();
                 switch (level) {
                     case FATAL:
                     case ERROR:
diff --git a/devtools/maven/src/main/java/io/quarkus/maven/NativeImageMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/NativeImageMojo.java
index cab6e5c7bc7..1cf0e37c589 100644
--- a/devtools/maven/src/main/java/io/quarkus/maven/NativeImageMojo.java
+++ b/devtools/maven/src/main/java/io/quarkus/maven/NativeImageMojo.java
@@ -12,6 +12,7 @@
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
@@ -176,10 +177,6 @@ public class NativeImageMojo extends AbstractMojo {
     @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
     private List<RemoteRepository> repos;
 
-    public NativeImageMojo() {
-        MojoLogger.logSupplier = this::getLog;
-    }
-
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
 
@@ -415,4 +412,10 @@ private Map<String, String> createCustomConfig() {
 
     }
 
+    @Override
+    public void setLog(Log log) {
+        super.setLog(log);
+        MojoLogger.delegate = log;
+    }
+
 }
diff --git a/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java
index 37833fafa9f..bf6d0ed94b8 100644
--- a/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java
+++ b/devtools/maven/src/main/java/io/quarkus/maven/QuarkusBootstrapMojo.java
@@ -6,6 +6,7 @@
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
@@ -77,10 +78,6 @@ public abstract class QuarkusBootstrapMojo extends AbstractMojo {
     private AppArtifactKey projectId;
     private boolean clearUberJarProp;
 
-    protected QuarkusBootstrapMojo() {
-        MojoLogger.logSupplier = this::getLog;
-    }
-
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
         if (!beforeExecute()) {
@@ -96,6 +93,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
         }
     }
 
+    @Override
+    public void setLog(Log log) {
+        super.setLog(log);
+        MojoLogger.delegate = log;
+    }
+
     protected void clearUberJarProp() {
         this.clearUberJarProp = true;
     }