View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.wrapper;
20  
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.lang.reflect.Method;
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  import java.nio.file.DirectoryStream;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.util.Iterator;
30  import java.util.Locale;
31  
32  /**
33   * Maven starter, from a provided Maven home directory.
34   *
35   * @author Hans Dockter
36   */
37  public class BootstrapMainStarter {
38      public void start(String[] args, Path mavenHome) throws Exception {
39          final Path mavenJar = findLauncherJar(mavenHome);
40          URLClassLoader contextClassLoader = new URLClassLoader(
41                  new URL[] {mavenJar.toUri().toURL()},
42                  ClassLoader.getSystemClassLoader().getParent());
43          Thread.currentThread().setContextClassLoader(contextClassLoader);
44          Class<?> mainClass = contextClassLoader.loadClass("org.codehaus.plexus.classworlds.launcher.Launcher");
45  
46          System.setProperty("maven.home", mavenHome.toAbsolutePath().toString());
47          System.setProperty(
48                  "classworlds.conf",
49                  mavenHome.resolve("bin/m2.conf").toAbsolutePath().toString());
50  
51          Method mainMethod = mainClass.getMethod("main", String[].class);
52          mainMethod.invoke(null, new Object[] {args});
53      }
54  
55      private Path findLauncherJar(Path mavenHome) throws IOException {
56          final Path mavenBoot = mavenHome.resolve("boot");
57          if (Files.isDirectory(mavenBoot)) {
58              try (DirectoryStream<Path> ds = Files.newDirectoryStream(mavenBoot, "plexus-classworlds-*.jar")) {
59                  Iterator<Path> iterator = ds.iterator();
60                  if (iterator.hasNext()) {
61                      return iterator.next();
62                  }
63              }
64          }
65          throw new FileNotFoundException(String.format(
66                  Locale.ROOT, "Could not locate the Maven launcher JAR" + " in Maven distribution '%s'.", mavenHome));
67      }
68  }