|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.net.Authenticator; |
| 23 | +import java.net.PasswordAuthentication; |
| 24 | +import java.net.URI; |
| 25 | +import java.net.URL; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.nio.file.Paths; |
| 29 | +import java.nio.file.StandardCopyOption; |
| 30 | +import java.util.concurrent.ThreadLocalRandom; |
| 31 | + |
| 32 | +public final class MavenWrapperDownloader { |
| 33 | + private static final String WRAPPER_VERSION = "3.3.2"; |
| 34 | + |
| 35 | + private static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("MVNW_VERBOSE")); |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + log("Apache Maven Wrapper Downloader " + WRAPPER_VERSION); |
| 39 | + |
| 40 | + if (args.length != 2) { |
| 41 | + System.err.println(" - ERROR wrapperUrl or wrapperJarPath parameter missing"); |
| 42 | + System.exit(1); |
| 43 | + } |
| 44 | + |
| 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) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + System.exit(1); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 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() { |
| 68 | + @Override |
| 69 | + protected PasswordAuthentication getPasswordAuthentication() { |
| 70 | + return new PasswordAuthentication(username, password); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 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); |
| 83 | + } |
| 84 | + log(" - Downloader complete"); |
| 85 | + } |
| 86 | + |
| 87 | + private static void log(String msg) { |
| 88 | + if (VERBOSE) { |
| 89 | + System.out.println(msg); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments