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.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  import java.util.Properties;
29  
30  /**
31   * Wrapper executor, running {@link Installer} to get a Maven distribution ready, followed by
32   * {@link BootstrapMainStarter} to launch the Maven bootstrap.
33   * 
34   * @author Hans Dockter
35   */
36  public class WrapperExecutor
37  {
38      public static final String DISTRIBUTION_URL_PROPERTY = "distributionUrl";
39  
40      public static final String DISTRIBUTION_BASE_PROPERTY = "distributionBase";
41  
42      public static final String ZIP_STORE_BASE_PROPERTY = "zipStoreBase";
43  
44      public static final String DISTRIBUTION_PATH_PROPERTY = "distributionPath";
45  
46      public static final String ZIP_STORE_PATH_PROPERTY = "zipStorePath";
47  
48      private final Properties properties;
49  
50      private final File propertiesFile;
51  
52      private final Appendable warningOutput;
53  
54      private final WrapperConfiguration config = new WrapperConfiguration();
55  
56      public static WrapperExecutor forProjectDirectory( File projectDir, Appendable warningOutput )
57      {
58          return new WrapperExecutor( new File( projectDir, "maven/wrapper/maven-wrapper.properties" ), new Properties(),
59                                      warningOutput );
60      }
61  
62      public static WrapperExecutor forWrapperPropertiesFile( File propertiesFile, Appendable warningOutput )
63      {
64          if ( !propertiesFile.exists() )
65          {
66              throw new RuntimeException( String.format( "Wrapper properties file '%s' does not exist.",
67                                                         propertiesFile ) );
68          }
69          return new WrapperExecutor( propertiesFile, new Properties(), warningOutput );
70      }
71  
72      WrapperExecutor( File propertiesFile, Properties properties, Appendable warningOutput )
73      {
74          this.properties = properties;
75          this.propertiesFile = propertiesFile;
76          this.warningOutput = warningOutput;
77          if ( propertiesFile.exists() )
78          {
79              try
80              {
81                  loadProperties( propertiesFile, properties );
82                  config.setDistribution( prepareDistributionUri() );
83                  config.setDistributionBase( getProperty( DISTRIBUTION_BASE_PROPERTY, config.getDistributionBase() ) );
84                  config.setDistributionPath( getProperty( DISTRIBUTION_PATH_PROPERTY, config.getDistributionPath() ) );
85                  config.setZipBase( getProperty( ZIP_STORE_BASE_PROPERTY, config.getZipBase() ) );
86                  config.setZipPath( getProperty( ZIP_STORE_PATH_PROPERTY, config.getZipPath() ) );
87              }
88              catch ( Exception e )
89              {
90                  throw new RuntimeException( String.format( "Could not load wrapper properties from '%s'.",
91                                                             propertiesFile ),
92                                              e );
93              }
94          }
95      }
96  
97      private URI prepareDistributionUri()
98          throws URISyntaxException
99      {
100         URI source = readDistroUrl();
101         if ( source.getScheme() == null )
102         {
103             // no scheme means someone passed a relative url. In our context only file relative urls make sense.
104             return new File( propertiesFile.getParentFile(), source.getSchemeSpecificPart() ).toURI();
105         }
106         else
107         {
108             return source;
109         }
110     }
111 
112     private URI readDistroUrl()
113         throws URISyntaxException
114     {
115         if ( properties.getProperty( DISTRIBUTION_URL_PROPERTY ) != null )
116         {
117             return new URI( getProperty( DISTRIBUTION_URL_PROPERTY ) );
118         }
119 
120         reportMissingProperty( DISTRIBUTION_URL_PROPERTY );
121         return null; // previous line will fail
122     }
123 
124     private static void loadProperties( File propertiesFile, Properties properties )
125         throws IOException
126     {
127         try ( InputStream inStream = new FileInputStream( propertiesFile ) )
128         {
129             properties.load( inStream );
130         }
131     }
132 
133     /**
134      * Returns the Maven distribution which this wrapper will use. Returns null if no wrapper meta-data was found in the
135      * specified project directory.
136      * @return the Maven distribution which this wrapper will use
137      */
138     public URI getDistribution()
139     {
140         return config.getDistribution();
141     }
142 
143     /**
144      * Returns the configuration for this wrapper.
145      * @return the configuration for this wrapper
146      */
147     public WrapperConfiguration getConfiguration()
148     {
149         return config;
150     }
151 
152     public void execute( String[] args, Installer install, BootstrapMainStarter bootstrapMainStarter )
153         throws Exception
154     {
155         File mavenHome = install.createDist( config );
156         bootstrapMainStarter.start( args, mavenHome );
157     }
158 
159     private String getProperty( String propertyName )
160     {
161         return getProperty( propertyName, null );
162     }
163 
164     private String getProperty( String propertyName, String defaultValue )
165     {
166         String value = properties.getProperty( propertyName );
167         if ( value != null )
168         {
169             return value;
170         }
171         if ( defaultValue != null )
172         {
173             return defaultValue;
174         }
175         return reportMissingProperty( propertyName );
176     }
177 
178     private String reportMissingProperty( String propertyName )
179     {
180         throw new RuntimeException( String.format( "No value with key '%s' specified in wrapper properties file '%s'.",
181                                                    propertyName, propertiesFile ) );
182     }
183 }