From 790be24342237632552bba8e7a9dd290c560da7f Mon Sep 17 00:00:00 2001 From: Reinhold Degenfellner Date: Tue, 18 Dec 2018 20:58:17 +0100 Subject: [PATCH] use MVNW_USER and MVNW_PASSWORD instead of system properties fix distribution naming add readme entry --- maven-wrapper/README.md | 13 ++++++++++ .../maven/wrapper/DefaultDownloader.java | 24 ++++++++++++++----- .../maven/wrapper/MavenWrapperMain.java | 3 +++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/maven-wrapper/README.md b/maven-wrapper/README.md index 9ad0e76833..eed05350ae 100644 --- a/maven-wrapper/README.md +++ b/maven-wrapper/README.md @@ -102,6 +102,19 @@ which works for any version except snapshots. Once you have a wrapper you can ch distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip ``` +## Using Basic Authentication for Distribution download + +To download Maven from a location that requires Basic Authentication you have 2 options: + +1. Set the environment variables MVNW_USER and MVNW_PASSWORD + + or + +2. add user and password to the distributionUrl like that: +`distributionUrl=https://username:password@/maven2/org/apache/maven/apache-maven/3.2.1/apache-maven-3.2.1-bin.zip` + + + [1]: https://github.com/takari/takari-maven-plugin ## Specifying Maven Distribution Base Path diff --git a/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java b/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java index d6fe728dd3..79a2ae25e8 100644 --- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java +++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/DefaultDownloader.java @@ -16,9 +16,21 @@ package org.apache.maven.wrapper; -import java.io.*; +import static org.apache.maven.wrapper.MavenWrapperMain.MVNW_PASSWORD; +import static org.apache.maven.wrapper.MavenWrapperMain.MVNW_USER; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.lang.reflect.Method; -import java.net.*; +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; /** * @author Hans Dockter @@ -104,7 +116,7 @@ public class DefaultDownloader implements Downloader { return; } if (!"https".equals(address.getScheme())) { - Logger.info("WARNING Using HTTP Basic Authentication over an insecure connection to download the Gradle distribution. Please consider using HTTPS."); + Logger.info("WARNING Using HTTP Basic Authentication over an insecure connection to download the Maven distribution. Please consider using HTTPS."); } connection.setRequestProperty("Authorization", "Basic " + base64Encode(userInfo)); } @@ -132,14 +144,14 @@ public class DefaultDownloader implements Downloader { Method encodeMethod = loader.loadClass("javax.xml.bind.DatatypeConverter").getMethod("printBase64Binary", byte[].class); return (String) encodeMethod.invoke(null, new Object[]{userInfo.getBytes("UTF-8")}); } catch (Exception java5OrEarlier) { - throw new RuntimeException("Downloading Gradle distributions with HTTP Basic Authentication is not supported on your JVM.", java5OrEarlier); + throw new RuntimeException("Downloading Maven distributions with HTTP Basic Authentication is not supported on your JVM.", java5OrEarlier); } } } private String calculateUserInfo(URI uri) { - String username = System.getProperty("maven.wrapperUser"); - String password = System.getProperty("maven.wrapperPassword"); + String username = System.getenv(MVNW_USER); + String password = System.getenv(MVNW_PASSWORD); if (username != null && password != null) { return username + ':' + password; } diff --git a/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java b/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java index 040d24afef..d0888c620b 100644 --- a/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java +++ b/maven-wrapper/src/main/java/org/apache/maven/wrapper/MavenWrapperMain.java @@ -37,7 +37,10 @@ public class MavenWrapperMain { public static final String MAVEN_USER_HOME_ENV_KEY = "MAVEN_USER_HOME"; public static final String MVNW_VERBOSE = "MVNW_VERBOSE"; + public static final String MVNW_USER = "MVNW_USER"; + public static final String MVNW_PASSWORD = "MVNW_PASSWORD"; + public static void main(String[] args) throws Exception { File wrapperJar = wrapperJar(); File propertiesFile = wrapperProperties(wrapperJar);