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