View Javadoc
1   package org.apache.maven.wrapper;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Maven starter, from a provided Maven home directory.
35   *
36   * @author Hans Dockter
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  }