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.IOException;
22  import java.io.InputStream;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.Locale;
28  import java.util.Map;
29  import java.util.Objects;
30  import java.util.Properties;
31  
32  import org.apache.maven.wrapper.cli.CommandLineParser;
33  import org.apache.maven.wrapper.cli.SystemPropertiesCommandLineConverter;
34  
35  /**
36   * Main entry point for the Maven Wrapper, delegating wrapper execution to {@link WrapperExecutor}.
37   *
38   * @author Hans Dockter
39   */
40  public class MavenWrapperMain {
41      private static final String POM_PROPERTIES =
42              "/META-INF/maven/org.apache.maven.wrapper/maven-wrapper/pom.properties";
43  
44      private static final Path DEFAULT_MAVEN_USER_HOME =
45              Paths.get(System.getProperty("user.home")).resolve(".m2");
46  
47      public static final String MAVEN_USER_HOME_PROPERTY_KEY = "maven.user.home";
48  
49      public static final String MAVEN_USER_HOME_ENV_KEY = "MAVEN_USER_HOME";
50  
51      public static final String MVNW_VERBOSE = "MVNW_VERBOSE";
52  
53      public static final String MVNW_USERNAME = "MVNW_USERNAME";
54  
55      public static final String MVNW_PASSWORD = "MVNW_PASSWORD";
56  
57      public static final String MVNW_REPOURL = "MVNW_REPOURL";
58  
59      public static void main(String[] args) throws Exception {
60          Path wrapperJar = wrapperJar();
61          Path propertiesFile = wrapperProperties(wrapperJar);
62          Path rootDir = rootDir(wrapperJar);
63  
64          String wrapperVersion = wrapperVersion();
65          Logger.info("Apache Maven Wrapper " + wrapperVersion);
66  
67          Properties systemProperties = System.getProperties();
68          systemProperties.putAll(parseSystemPropertiesFromArgs(args));
69  
70          addSystemProperties(rootDir);
71  
72          WrapperExecutor wrapperExecutor = WrapperExecutor.forWrapperPropertiesFile(propertiesFile);
73          wrapperExecutor.execute(
74                  args,
75                  new Installer(
76                          new DefaultDownloader("mvnw", wrapperVersion),
77                          new HashAlgorithmVerifier(),
78                          new PathAssembler(mavenUserHome())),
79                  new BootstrapMainStarter());
80      }
81  
82      private static Map<String, String> parseSystemPropertiesFromArgs(String[] args) {
83          SystemPropertiesCommandLineConverter converter = new SystemPropertiesCommandLineConverter();
84          CommandLineParser commandLineParser = new CommandLineParser();
85          converter.configure(commandLineParser);
86          commandLineParser.allowUnknownOptions();
87          return converter.convert(commandLineParser.parse(args));
88      }
89  
90      private static void addSystemProperties(Path rootDir) {
91          System.getProperties()
92                  .putAll(SystemPropertiesHandler.getSystemProperties(
93                          mavenUserHome().resolve("maven.properties")));
94          System.getProperties().putAll(SystemPropertiesHandler.getSystemProperties(rootDir.resolve("maven.properties")));
95      }
96  
97      private static Path rootDir(Path wrapperJar) {
98          return wrapperJar.getParent().getParent().getParent();
99      }
100 
101     private static Path wrapperProperties(Path wrapperJar) {
102         return wrapperJar.resolveSibling(wrapperJar.getFileName().toString().replaceFirst("\\.jar$", ".properties"));
103     }
104 
105     private static Path wrapperJar() {
106         URI location;
107         try {
108             location = MavenWrapperMain.class
109                     .getProtectionDomain()
110                     .getCodeSource()
111                     .getLocation()
112                     .toURI();
113         } catch (URISyntaxException e) {
114             throw new RuntimeException(e);
115         }
116         if (!"file".equals(location.getScheme())) {
117             throw new RuntimeException(String.format(
118                     Locale.ROOT, "Cannot determine classpath for wrapper Jar from codebase '%s'.", location));
119         }
120         return Paths.get(location);
121     }
122 
123     static String wrapperVersion() {
124         try (InputStream resourceAsStream = MavenWrapperMain.class.getResourceAsStream(POM_PROPERTIES)) {
125             if (resourceAsStream == null) {
126                 throw new IllegalStateException(POM_PROPERTIES + " not found.");
127             }
128             Properties mavenProperties = new Properties();
129             mavenProperties.load(resourceAsStream);
130             String version = mavenProperties.getProperty("version");
131             Objects.requireNonNull(version, "No version specified in " + POM_PROPERTIES);
132             return version;
133         } catch (IOException e) {
134             throw new RuntimeException("Could not determine wrapper version.", e);
135         }
136     }
137 
138     private static Path mavenUserHome() {
139         String mavenUserHome = System.getProperty(MAVEN_USER_HOME_PROPERTY_KEY);
140         if (mavenUserHome == null) {
141             mavenUserHome = System.getenv(MAVEN_USER_HOME_ENV_KEY);
142         }
143 
144         return mavenUserHome == null ? DEFAULT_MAVEN_USER_HOME : Paths.get(mavenUserHome);
145     }
146 }