|
21 | 21 | import java.io.InputStream;
|
22 | 22 | import java.net.Authenticator;
|
23 | 23 | import java.net.PasswordAuthentication;
|
| 24 | +import java.net.URI; |
24 | 25 | import java.net.URL;
|
25 | 26 | import java.nio.file.Files;
|
26 | 27 | import java.nio.file.Path;
|
27 | 28 | import java.nio.file.Paths;
|
28 | 29 | import java.nio.file.StandardCopyOption;
|
| 30 | +import java.util.concurrent.ThreadLocalRandom; |
29 | 31 |
|
30 |
| -public final class MavenWrapperDownloader |
31 |
| -{ |
32 |
| - private static final String WRAPPER_VERSION = "3.2.0"; |
| 32 | +public final class MavenWrapperDownloader { |
| 33 | + private static final String WRAPPER_VERSION = "3.3.0"; |
33 | 34 |
|
34 |
| - private static final boolean VERBOSE = Boolean.parseBoolean( System.getenv( "MVNW_VERBOSE" ) ); |
| 35 | + private static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("MVNW_VERBOSE")); |
35 | 36 |
|
36 |
| - public static void main( String[] args ) |
37 |
| - { |
38 |
| - log( "Apache Maven Wrapper Downloader " + WRAPPER_VERSION ); |
| 37 | + public static void main(String[] args) { |
| 38 | + log("Apache Maven Wrapper Downloader " + WRAPPER_VERSION); |
39 | 39 |
|
40 |
| - if ( args.length != 2 ) |
41 |
| - { |
42 |
| - System.err.println( " - ERROR wrapperUrl or wrapperJarPath parameter missing" ); |
43 |
| - System.exit( 1 ); |
| 40 | + if (args.length != 2) { |
| 41 | + System.err.println(" - ERROR wrapperUrl or wrapperJarPath parameter missing"); |
| 42 | + System.exit(1); |
44 | 43 | }
|
45 | 44 |
|
46 |
| - try |
47 |
| - { |
48 |
| - log( " - Downloader started" ); |
49 |
| - final URL wrapperUrl = new URL( args[0] ); |
50 |
| - final String jarPath = args[1].replace( "..", "" ); // Sanitize path |
51 |
| - final Path wrapperJarPath = Paths.get( jarPath ).toAbsolutePath().normalize(); |
52 |
| - downloadFileFromURL( wrapperUrl, wrapperJarPath ); |
53 |
| - log( "Done" ); |
54 |
| - } |
55 |
| - catch ( IOException e ) |
56 |
| - { |
57 |
| - System.err.println( "- Error downloading: " + e.getMessage() ); |
58 |
| - if ( VERBOSE ) |
59 |
| - { |
| 45 | + try { |
| 46 | + log(" - Downloader started"); |
| 47 | + final URL wrapperUrl = URI.create(args[0]).toURL(); |
| 48 | + final String jarPath = args[1].replace("..", ""); // Sanitize path |
| 49 | + final Path wrapperJarPath = Paths.get(jarPath).toAbsolutePath().normalize(); |
| 50 | + downloadFileFromURL(wrapperUrl, wrapperJarPath); |
| 51 | + log("Done"); |
| 52 | + } catch (IOException e) { |
| 53 | + System.err.println("- Error downloading: " + e.getMessage()); |
| 54 | + if (VERBOSE) { |
60 | 55 | e.printStackTrace();
|
61 | 56 | }
|
62 |
| - System.exit( 1 ); |
| 57 | + System.exit(1); |
63 | 58 | }
|
64 | 59 | }
|
65 | 60 |
|
66 |
| - private static void downloadFileFromURL( URL wrapperUrl, Path wrapperJarPath ) |
67 |
| - throws IOException |
68 |
| - { |
69 |
| - log( " - Downloading to: " + wrapperJarPath ); |
70 |
| - if ( System.getenv( "MVNW_USERNAME" ) != null && System.getenv( "MVNW_PASSWORD" ) != null ) |
71 |
| - { |
72 |
| - final String username = System.getenv( "MVNW_USERNAME" ); |
73 |
| - final char[] password = System.getenv( "MVNW_PASSWORD" ).toCharArray(); |
74 |
| - Authenticator.setDefault( new Authenticator() |
75 |
| - { |
| 61 | + private static void downloadFileFromURL(URL wrapperUrl, Path wrapperJarPath) |
| 62 | + throws IOException { |
| 63 | + log(" - Downloading to: " + wrapperJarPath); |
| 64 | + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { |
| 65 | + final String username = System.getenv("MVNW_USERNAME"); |
| 66 | + final char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); |
| 67 | + Authenticator.setDefault(new Authenticator() { |
76 | 68 | @Override
|
77 |
| - protected PasswordAuthentication getPasswordAuthentication() |
78 |
| - { |
79 |
| - return new PasswordAuthentication( username, password ); |
| 69 | + protected PasswordAuthentication getPasswordAuthentication() { |
| 70 | + return new PasswordAuthentication(username, password); |
80 | 71 | }
|
81 |
| - } ); |
| 72 | + }); |
82 | 73 | }
|
83 |
| - try ( InputStream inStream = wrapperUrl.openStream() ) |
84 |
| - { |
85 |
| - Files.copy( inStream, wrapperJarPath, StandardCopyOption.REPLACE_EXISTING ); |
| 74 | + Path temp = wrapperJarPath |
| 75 | + .getParent() |
| 76 | + .resolve(wrapperJarPath.getFileName() + "." |
| 77 | + + Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp"); |
| 78 | + try (InputStream inStream = wrapperUrl.openStream()) { |
| 79 | + Files.copy(inStream, temp, StandardCopyOption.REPLACE_EXISTING); |
| 80 | + Files.move(temp, wrapperJarPath, StandardCopyOption.REPLACE_EXISTING); |
| 81 | + } finally { |
| 82 | + Files.deleteIfExists(temp); |
86 | 83 | }
|
87 |
| - log( " - Downloader complete" ); |
| 84 | + log(" - Downloader complete"); |
88 | 85 | }
|
89 | 86 |
|
90 |
| - private static void log( String msg ) |
91 |
| - { |
92 |
| - if ( VERBOSE ) |
93 |
| - { |
94 |
| - System.out.println( msg ); |
| 87 | + private static void log(String msg) { |
| 88 | + if (VERBOSE) { |
| 89 | + System.out.println(msg); |
95 | 90 | }
|
96 | 91 | }
|
97 | 92 |
|
|
0 commit comments