1 package org.apache.maven.wrapper;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.lang.reflect.Method;
25 import java.net.URL;
26 import java.net.URLClassLoader;
27 import java.nio.file.DirectoryStream;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.util.Iterator;
31 import java.util.Locale;
32
33
34
35
36
37
38 public class BootstrapMainStarter
39 {
40 public void start( String[] args, Path mavenHome )
41 throws Exception
42 {
43 final Path mavenJar = findLauncherJar( mavenHome );
44 URLClassLoader contextClassLoader = new URLClassLoader( new URL[] { mavenJar.toUri().toURL() },
45 ClassLoader.getSystemClassLoader().getParent() );
46 Thread.currentThread().setContextClassLoader( contextClassLoader );
47 Class<?> mainClass = contextClassLoader.loadClass( "org.codehaus.plexus.classworlds.launcher.Launcher" );
48
49 System.setProperty( "maven.home", mavenHome.toAbsolutePath().toString() );
50 System.setProperty( "classworlds.conf", mavenHome.resolve( "bin/m2.conf" ).toAbsolutePath().toString() );
51
52 Method mainMethod = mainClass.getMethod( "main", String[].class );
53 mainMethod.invoke( null, new Object[] { args } );
54 }
55
56 private Path findLauncherJar( Path mavenHome )
57 throws IOException
58 {
59 final Path mavenBoot = mavenHome.resolve( "boot" );
60 if ( Files.isDirectory( mavenBoot ) )
61 {
62 try ( DirectoryStream<Path> ds = Files.newDirectoryStream( mavenBoot, "plexus-classworlds-*.jar" ) )
63 {
64 Iterator<Path> iterator = ds.iterator();
65 if ( iterator.hasNext() )
66 {
67 return iterator.next();
68 }
69 }
70 }
71 throw new FileNotFoundException( String.format( Locale.ROOT, "Could not locate the Maven launcher JAR"
72 + " in Maven distribution '%s'.", mavenHome ) );
73 }
74 }