21
21
import java .io .InputStream ;
22
22
import java .net .Authenticator ;
23
23
import java .net .PasswordAuthentication ;
24
+ import java .net .URI ;
25
+ import java .net .URISyntaxException ;
24
26
import java .net .URL ;
25
27
import java .nio .file .Files ;
26
28
import java .nio .file .Path ;
29
31
import java .security .MessageDigest ;
30
32
import java .util .Arrays ;
31
33
import java .util .Properties ;
34
+ import java .util .jar .JarFile ;
35
+ import java .util .zip .ZipFile ;
32
36
33
37
public final class MavenWrapperHelper {
34
38
35
- private static final String WRAPPER_VERSION = "3.2.0" ;
36
-
37
- private static final boolean VERBOSE = Boolean .parseBoolean (System .getenv ("MVNW_VERBOSE" ));
38
-
39
- public static void main (String [] args ) throws Exception {
40
- String action = args [0 ].toLowerCase ();
41
- String [] actionArgs = Arrays .copyOfRange (args , 1 , args .length );
42
- switch (action ) {
43
- case "download" :
44
- log ("Apache Maven Wrapper Downloader " + WRAPPER_VERSION );
45
- if (actionArgs .length != 2 ) {
46
- System .err .println (" - ERROR wrapperUrl or wrapperJarPath parameter missing" );
47
- System .exit (1 );
48
- }
49
- try {
50
- log (" - Downloader started" );
51
- final URL wrapperUrl = new URL (actionArgs [0 ]);
52
- final String jarPath = actionArgs [1 ].replace ("src/main" , "" ); // Sanitize path
53
- final Path wrapperJarPath = Paths .get (jarPath ).toAbsolutePath ().normalize ();
54
- downloadFileFromURL (wrapperUrl , wrapperJarPath );
55
- log ("Done" );
56
- } catch (IOException e ) {
57
- System .err .println ("- Error downloading: " + e .getMessage ());
58
- if (VERBOSE ) {
59
- e .printStackTrace ();
60
- }
61
- System .exit (1 );
62
- }
63
- break ;
64
- case "verify" :
65
- String wrapperJar = actionArgs [0 ];
66
- String propertiesPath = actionArgs [1 ];
67
- log ("maven-wrapper file checking: " + wrapperJar );
68
- Properties properties = new Properties ();
69
- properties .load (Files .newInputStream (new File (propertiesPath ).toPath ()));
70
- String wrapperMd5 = properties .getProperty ("wrapperMd5" );
71
- if (wrapperMd5 == null ) {
72
- System .err .println ("wrapperMd5 not in " + propertiesPath );
73
- System .exit (1 );
74
- }
75
- String fileMd5 = getFileMd5 (wrapperJar );
76
- System .exit (wrapperMd5 .equals (fileMd5 ) ? 0 : 1 );
77
- default :
78
- throw new UnsupportedOperationException ("Unknown action \" " + action + "\" ." );
79
- }
80
- }
39
+ public static final String PROJECT_STRING = "PROJECT" ;
40
+
41
+ private static final String WRAPPER_VERSION = "3.2.0" ;
42
+
43
+ public static final String DISTRIBUTION_BASE_PROPERTY = "distributionBase" ;
44
+
45
+ public static final String DISTRIBUTION_PATH_PROPERTY = "distributionPath" ;
46
+
47
+ public static final String MAVEN_USER_HOME_ENV_KEY = "MAVEN_USER_HOME" ;
48
+
49
+ public static final String MAVEN_USER_HOME_STRING = "MAVEN_USER_HOME" ;
50
+
51
+ public static final String MAVEN_USER_HOME_PROPERTY_KEY = "maven.user.home" ;
52
+
53
+ public static final String ZIP_STORE_BASE_PROPERTY = "zipStoreBase" ;
54
+
55
+ public static final String ZIP_STORE_PATH_PROPERTY = "zipStorePath" ;
56
+
57
+ public static final Path DEFAULT_DISTRIBUTION_PATH = Paths .get ("wrapper" , "dists" );
58
+
59
+ private static final Path DEFAULT_MAVEN_USER_HOME =
60
+ Paths .get (System .getProperty ("user.home" )).resolve (".m2" );
61
+
62
+ private static final boolean VERBOSE = Boolean .parseBoolean (System .getenv ("MVNW_VERBOSE" ));
63
+
64
+ public static void main (String [] args ) throws Exception {
65
+ String action = args [0 ].toLowerCase ();
66
+ String [] actionArgs = Arrays .copyOfRange (args , 1 , args .length );
67
+ Properties properties ;
81
68
82
- private static void downloadFileFromURL (URL wrapperUrl , Path wrapperJarPath ) throws IOException {
83
- log (" - Downloading to: " + wrapperJarPath );
84
- if (System .getenv ("MVNW_USERNAME" ) != null && System .getenv ("MVNW_PASSWORD" ) != null ) {
85
- final String username = System .getenv ("MVNW_USERNAME" );
86
- final char [] password = System .getenv ("MVNW_PASSWORD" ).toCharArray ();
87
- Authenticator .setDefault (
88
- new Authenticator () {
89
- @ Override
90
- protected PasswordAuthentication getPasswordAuthentication () {
91
- return new PasswordAuthentication (username , password );
92
- }
93
- });
69
+ switch (action ) {
70
+ case "download" :
71
+ log ("Apache Maven Wrapper Downloader " + WRAPPER_VERSION );
72
+ if (actionArgs .length != 2 ) {
73
+ System .err .println (" - ERROR wrapperUrl or wrapperJarPath parameter missing" );
74
+ System .exit (1 );
94
75
}
95
- try (InputStream inStream = wrapperUrl .openStream ()) {
96
- Files .copy (inStream , wrapperJarPath , StandardCopyOption .REPLACE_EXISTING );
76
+ try {
77
+ log (" - Downloader started" );
78
+ final URL wrapperUrl = new URL (actionArgs [0 ]);
79
+ final String jarPath = actionArgs [1 ].replace ("src/main" , "" ); // Sanitize path
80
+ final Path wrapperJarPath = Paths .get (jarPath ).toAbsolutePath ().normalize ();
81
+ downloadFileFromURL (wrapperUrl , wrapperJarPath );
82
+ log ("Done" );
83
+ } catch (IOException e ) {
84
+ System .err .println ("- Error downloading: " + e .getMessage ());
85
+ if (VERBOSE ) {
86
+ e .printStackTrace ();
87
+ }
88
+ System .exit (1 );
97
89
}
98
- log (" - Downloader complete" );
99
- }
90
+ break ;
91
+
92
+ case "verify_wrapper" :
93
+ String wrapperJar = actionArgs [0 ];
94
+ properties = getProperties (actionArgs [1 ]);
95
+ String wrapperMd5 = properties .getProperty ("wrapperMd5" );
96
+ if (wrapperMd5 != null ) {
97
+ String fileMd5 = getFileMd5 (wrapperJar );
98
+ if (!wrapperMd5 .equals (fileMd5 )) {
99
+ System .exit (1 );
100
+ }
101
+ } else {
102
+ try (JarFile ignored = new JarFile (wrapperJar , true )) {
103
+ } catch (Exception e ) {
104
+ System .exit (1 );
105
+ }
106
+ }
107
+ break ;
108
+
109
+ case "verify_dist" :
110
+ properties = getProperties (actionArgs [0 ]);
111
+ LocalDistribution distribution = getLocalDistribution (properties );
100
112
101
- private static String getFileMd5 (String path ) throws Exception {
102
- MessageDigest md5 = MessageDigest .getInstance ("MD5" );
103
- try (FileInputStream inputStream = new FileInputStream (path )) {
104
- byte [] buffer = new byte [1024 ];
105
- int len ;
106
- while ((len = inputStream .read (buffer )) != -1 ) {
107
- md5 .update (buffer , 0 , len );
113
+ File zipFile = distribution .getZipFile ();
114
+ File unzipFile = distribution .getDistributionDir ();
115
+ if (unzipFile .exists ()) {
116
+ // 1) check distribution unzip files
117
+ String cmd = getMavenCheckCMD (actionArgs );
118
+ boolean success ;
119
+ try {
120
+ Process process = Runtime .getRuntime ().exec (cmd );
121
+ process .waitFor ();
122
+ success = process .exitValue () == 0 ;
123
+ } catch (Exception e ) {
124
+ success = false ;
125
+ }
126
+ if (!success ) {
127
+ deleteDir (unzipFile );
128
+ if (unzipFile .exists ()) {
129
+ System .out .println (unzipFile .getAbsolutePath ());
130
+ System .exit (1 );
108
131
}
109
- byte [] byteArray = md5 .digest ();
110
- StringBuilder sb = new StringBuilder ();
111
- for (byte b : byteArray ) {
112
- sb .append (String .format ("%02x" , b ));
132
+ }
133
+ } else if (zipFile .exists ()) {
134
+ // 2) check distribution zip file
135
+ String distributionPath = zipFile .getAbsolutePath ();
136
+ String distributionMd5 = properties .getProperty ("distributionMd5" );
137
+ if (distributionMd5 != null ) {
138
+ String fileMd5 = getFileMd5 (distributionPath );
139
+ if (!distributionMd5 .equals (fileMd5 )) {
140
+ boolean success = deleteDistribution (distribution );
141
+ if (!success ) {
142
+ System .out .println (zipFile .getAbsolutePath ());
143
+ System .exit (1 );
144
+ }
113
145
}
114
- return sb .toString ();
146
+ } else {
147
+ try (ZipFile ignored = new ZipFile (zipFile )) {
148
+ } catch (Exception e ) {
149
+ boolean success = deleteDistribution (distribution );
150
+ if (!success ) {
151
+ System .out .println (zipFile .getAbsolutePath ());
152
+ System .exit (1 );
153
+ }
154
+ }
155
+ }
115
156
}
157
+ break ;
158
+ default :
159
+ System .out .println ("Unknown action" );
160
+ System .exit (2 );
161
+ }
162
+ }
163
+
164
+ private static Properties getProperties (String path ) throws IOException {
165
+ Properties properties = new Properties ();
166
+ properties .load (Files .newInputStream (new File (path ).toPath ()));
167
+ return properties ;
168
+ }
169
+
170
+ private static String getMavenCheckCMD (String [] actionArgs ) {
171
+ String javaCMD = actionArgs [1 ];
172
+ String mvnWrapperHome = actionArgs [2 ];
173
+ String format =
174
+ "%s -classpath %s/.mvn/wrapper/maven-wrapper.jar "
175
+ + "-Dmaven.multiModuleProjectDirectory=%s "
176
+ + "org.apache.maven.wrapper.MavenWrapperMain -h" ;
177
+ return String .format (format , javaCMD , mvnWrapperHome , mvnWrapperHome );
178
+ }
179
+
180
+ private static boolean deleteDistribution (LocalDistribution distribution ) {
181
+ String distPath = distribution .getZipFile ().getAbsolutePath ();
182
+ System .err .println ("- check distribution error: " + distPath + "is invalid" );
183
+ try {
184
+ return distribution .getZipFile ().delete ();
185
+ } catch (Exception e ) {
186
+ System .err .println ("- delete distribution error: " + distPath );
187
+ return false ;
188
+ }
189
+ }
190
+
191
+ private static LocalDistribution getLocalDistribution (Properties properties )
192
+ throws URISyntaxException {
193
+ String distributionUrl = properties .getProperty ("distributionUrl" );
194
+ URI uri = new URI (distributionUrl );
195
+ String baseName = getBaseName (uri );
196
+ String distName = removeExtension (baseName );
197
+ Path rootDirName = rootDirName (distName , uri );
198
+
199
+ String distributionBase =
200
+ properties .getProperty (DISTRIBUTION_BASE_PROPERTY , MAVEN_USER_HOME_STRING );
201
+
202
+ Path distributionPath =
203
+ Paths .get (
204
+ properties .getProperty (
205
+ DISTRIBUTION_PATH_PROPERTY , DEFAULT_DISTRIBUTION_PATH .toString ()));
206
+
207
+ String zipBase = properties .getProperty (ZIP_STORE_BASE_PROPERTY , MAVEN_USER_HOME_STRING );
208
+
209
+ Path zipPath =
210
+ Paths .get (
211
+ properties .getProperty (ZIP_STORE_PATH_PROPERTY , DEFAULT_DISTRIBUTION_PATH .toString ()));
212
+
213
+ Path distDir = getBaseDir (distributionBase ).resolve (distributionPath ).resolve (rootDirName );
214
+
215
+ Path distZip = getBaseDir (zipBase ).resolve (zipPath ).resolve (rootDirName ).resolve (baseName );
216
+
217
+ return new LocalDistribution (distDir .toFile (), distZip .toFile ());
218
+ }
219
+
220
+ private static void downloadFileFromURL (URL wrapperUrl , Path wrapperJarPath ) throws IOException {
221
+ log (" - Downloading to: " + wrapperJarPath );
222
+ if (System .getenv ("MVNW_USERNAME" ) != null && System .getenv ("MVNW_PASSWORD" ) != null ) {
223
+ final String username = System .getenv ("MVNW_USERNAME" );
224
+ final char [] password = System .getenv ("MVNW_PASSWORD" ).toCharArray ();
225
+ Authenticator .setDefault (
226
+ new Authenticator () {
227
+ @ Override
228
+ protected PasswordAuthentication getPasswordAuthentication () {
229
+ return new PasswordAuthentication (username , password );
230
+ }
231
+ });
232
+ }
233
+ try (InputStream inStream = wrapperUrl .openStream ()) {
234
+ Files .copy (inStream , wrapperJarPath , StandardCopyOption .REPLACE_EXISTING );
235
+ }
236
+ log (" - Downloader complete" );
237
+ }
238
+
239
+ private static String getFileMd5 (String path ) throws Exception {
240
+ MessageDigest md5 = MessageDigest .getInstance ("MD5" );
241
+ try (FileInputStream inputStream = new FileInputStream (path )) {
242
+ byte [] buffer = new byte [1024 ];
243
+ int len ;
244
+ while ((len = inputStream .read (buffer )) != -1 ) {
245
+ md5 .update (buffer , 0 , len );
246
+ }
247
+ byte [] byteArray = md5 .digest ();
248
+ StringBuilder sb = new StringBuilder ();
249
+ for (byte b : byteArray ) {
250
+ sb .append (String .format ("%02x" , b ));
251
+ }
252
+ return sb .toString ();
253
+ }
254
+ }
255
+
256
+ private static void log (String msg ) {
257
+ if (VERBOSE ) {
258
+ System .out .println (msg );
259
+ }
260
+ }
261
+
262
+ private static String getBaseName (URI uri ) {
263
+ return Paths .get (uri .getPath ()).getFileName ().toString ();
264
+ }
265
+
266
+ private static String removeExtension (String name ) {
267
+ int dot = name .lastIndexOf ("." );
268
+ return dot > 0 ? name .substring (0 , dot ) : name ;
269
+ }
270
+
271
+ private static Path rootDirName (String distName , URI uri ) {
272
+ String urlHash = getHash (uri );
273
+ return Paths .get (distName , urlHash );
274
+ }
275
+
276
+ private static String getHash (URI path ) {
277
+ return Integer .toHexString (path .hashCode ());
278
+ }
279
+
280
+ private static Path getBaseDir (String base ) {
281
+ if (MAVEN_USER_HOME_STRING .equals (base )) {
282
+ return mavenUserHome ();
283
+ } else if (PROJECT_STRING .equals (base )) {
284
+ return Paths .get (System .getProperty ("user.dir" ));
285
+ } else {
286
+ throw new RuntimeException ("Base: " + base + " is unknown" );
116
287
}
288
+ }
117
289
118
- private static void log (String msg ) {
119
- if (VERBOSE ) {
120
- System .out .println (msg );
290
+ private static Path mavenUserHome () {
291
+ String mavenUserHome = System .getProperty (MAVEN_USER_HOME_PROPERTY_KEY );
292
+ if (mavenUserHome == null ) {
293
+ mavenUserHome = System .getenv (MAVEN_USER_HOME_ENV_KEY );
294
+ }
295
+ return mavenUserHome == null ? DEFAULT_MAVEN_USER_HOME : Paths .get (mavenUserHome );
296
+ }
297
+
298
+ private static void deleteDir (File directory ) {
299
+ if (!directory .exists ()) {
300
+ return ;
301
+ }
302
+ File [] files = directory .listFiles ();
303
+ if (files != null ) {
304
+ for (File file : files ) {
305
+ if (file .isDirectory ()) {
306
+ deleteDir (file );
307
+ } else {
308
+ file .delete ();
121
309
}
310
+ }
311
+ }
312
+ directory .delete ();
313
+ }
314
+
315
+ public static class LocalDistribution {
316
+ private final File distZip ;
317
+
318
+ private final File distDir ;
319
+
320
+ public LocalDistribution (File distDir , File distZip ) {
321
+ this .distDir = distDir ;
322
+ this .distZip = distZip ;
323
+ }
324
+
325
+ public File getDistributionDir () {
326
+ return distDir ;
327
+ }
328
+
329
+ public File getZipFile () {
330
+ return distZip ;
122
331
}
123
- }
332
+ }
333
+ }
0 commit comments