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