This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch enable-to-customize-classworlds.conf
in repository https://gitbox.apache.org/repos/asf/maven-wrapper.git

commit 70289ea81465aa5c4410127833fe906b5f71c3c9
Author: Romain Manni-Bucau <rmannibu...@gmail.com>
AuthorDate: Thu Aug 4 11:15:36 2022 +0200

    Enable to customize classworlds.conf
---
 .../apache/maven/wrapper/BootstrapMainStarter.java | 37 ++++++++++++++++++++--
 .../org/apache/maven/wrapper/WrapperExecutor.java  |  2 +-
 .../apache/maven/wrapper/WrapperExecutorTest.java  |  8 +++--
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git 
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/BootstrapMainStarter.java
 
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/BootstrapMainStarter.java
index 71f4186..f64ca7f 100644
--- 
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/BootstrapMainStarter.java
+++ 
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/BootstrapMainStarter.java
@@ -21,6 +21,7 @@ package org.apache.maven.wrapper;
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -29,6 +30,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Iterator;
 import java.util.Locale;
+import java.util.Properties;
 
 /**
  * Maven starter, from a provided Maven home directory.
@@ -37,22 +39,51 @@ import java.util.Locale;
  */
 public class BootstrapMainStarter
 {
-    public void start( String[] args, Path mavenHome )
+    public void start( String[] args, Path mavenHome, Properties properties )
         throws Exception
     {
         final Path mavenJar = findLauncherJar( mavenHome );
         URLClassLoader contextClassLoader = new URLClassLoader( new URL[] { 
mavenJar.toUri().toURL() },
-                                                                
ClassLoader.getSystemClassLoader().getParent() );
+                ClassLoader.getSystemClassLoader().getParent() );
+
+        // can be useful to leak the classloader with some daemon mojo but 
generally a wrong idea so off by default
+        if ( Boolean.parseBoolean( properties.getProperty( 
getClass().getName() + ".leakClassloader" ) ) )
+        {
+            doStart( args, mavenHome, properties, contextClassLoader );
+            return;
+        }
+
+        try ( final URLClassLoader ref = contextClassLoader )
+        {
+            doStart( args, mavenHome, properties, contextClassLoader );
+        }
+    }
+
+    private void doStart( final String[] args, final Path mavenHome,
+                          final Properties properties,
+                          final URLClassLoader contextClassLoader )
+            throws ClassNotFoundException, NoSuchMethodException, 
IllegalAccessException, InvocationTargetException
+    {
         Thread.currentThread().setContextClassLoader( contextClassLoader );
         Class<?> mainClass = contextClassLoader.loadClass( 
"org.codehaus.plexus.classworlds.launcher.Launcher" );
 
         System.setProperty( "maven.home", 
mavenHome.toAbsolutePath().toString() );
-        System.setProperty( "classworlds.conf", mavenHome.resolve( 
"bin/m2.conf" ).toAbsolutePath().toString() );
+        System.setProperty( "classworlds.conf", getClassworldsConf( 
properties, mavenHome ) );
 
         Method mainMethod = mainClass.getMethod( "main", String[].class );
         mainMethod.invoke( null, new Object[] { args } );
     }
 
+    private String getClassworldsConf( Properties properties, Path mavenHome )
+    {
+        final String override = properties.getProperty( "classworlds.conf" );
+        if ( override != null )
+        {
+            return override;
+        }
+        return mavenHome.resolve( "bin/m2.conf" ).toAbsolutePath().toString();
+    }
+
     private Path findLauncherJar( Path mavenHome )
         throws IOException
     {
diff --git 
a/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java 
b/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
index f69d7cc..f606fba 100644
--- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
+++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/WrapperExecutor.java
@@ -177,7 +177,7 @@ public class WrapperExecutor
         throws Exception
     {
         Path mavenHome = install.createDist( config );
-        bootstrapMainStarter.start( args, mavenHome );
+        bootstrapMainStarter.start( args, mavenHome, properties );
     }
 
     private String getProperty( String propertyName )
diff --git 
a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java 
b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
index fb91f2d..9e874d6 100644
--- 
a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
+++ 
b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java
@@ -138,9 +138,13 @@ public class WrapperExecutorTest
   {
     WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( 
propertiesFile );
 
-    wrapper.execute( new String[] { "arg" }, install, start );
+    final String[] args = { "arg" };
+    wrapper.execute( args, install, start );
     verify( install ).createDist( Mockito.any( WrapperConfiguration.class ) );
-    verify( start ).start( new String[] { "arg" }, mockInstallDir );
+    verify( start ).start(
+            Mockito.eq( args ),
+            Mockito.eq( mockInstallDir ),
+            Mockito.any( Properties.class ) );
   }
 
   @Test( )

Reply via email to