1 package org.apache.maven.wrapper;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
33
34
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
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;
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
135
136
137
138 public URI getDistribution()
139 {
140 return config.getDistribution();
141 }
142
143
144
145
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 }