From b9e6795b6ce7f15e26bd436543886d97dbda732f Mon Sep 17 00:00:00 2001 From: "Vincent Jansen (v.jansen@blueriq.com)" Date: Fri, 17 Aug 2018 16:24:48 +0200 Subject: [PATCH] Download maven wrapper with authentication if availble --- .../.mvn/wrapper/MavenWrapperDownloader.java | 10 ++++++++++ maven-wrapper/mvnw | 13 +++++++++++-- maven-wrapper/mvnw.cmd | 9 ++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java b/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java index a52b0a12db..eabf010ffd 100644 --- a/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java +++ b/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java @@ -98,6 +98,16 @@ public class MavenWrapperDownloader { } private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_WRAPPER_AUTH_USERNAME") != null && System.getenv("MVNW_WRAPPER_AUTH_PASSWORD") != null) { + String username = System.getenv("MVNW_WRAPPER_AUTH_USERNAME"); + char[] password = System.getenv("MVNW_WRAPPER_AUTH_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); diff --git a/maven-wrapper/mvnw b/maven-wrapper/mvnw index 8dfad2cffb..e13f6777fc 100755 --- a/maven-wrapper/mvnw +++ b/maven-wrapper/mvnw @@ -225,12 +225,21 @@ else if [ "$MVNW_VERBOSE" = true ]; then echo "Found wget ... using wget" fi - wget "$jarUrl" -O "$wrapperJarPath" + if [ -z "$MVNW_WRAPPER_AUTH_USERNAME" ] || [ -z "$MVNW_WRAPPER_AUTH_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_WRAPPER_AUTH_USERNAME --http-password=$MVNW_WRAPPER_AUTH_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi elif command -v curl > /dev/null; then if [ "$MVNW_VERBOSE" = true ]; then echo "Found curl ... using curl" fi - curl -o "$wrapperJarPath" "$jarUrl" + if [ -z "$MVNW_WRAPPER_AUTH_USERNAME" ] || [ -z "$MVNW_WRAPPER_AUTH_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_WRAPPER_AUTH_USERNAME:$MVNW_WRAPPER_AUTH_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + else if [ "$MVNW_VERBOSE" = true ]; then echo "Falling back to using Java to download" diff --git a/maven-wrapper/mvnw.cmd b/maven-wrapper/mvnw.cmd index 48363fa60b..fbf543c9c4 100644 --- a/maven-wrapper/mvnw.cmd +++ b/maven-wrapper/mvnw.cmd @@ -132,7 +132,14 @@ if exist %WRAPPER_JAR% ( ) else ( echo Couldn't find %WRAPPER_JAR%, downloading it ... echo Downloading from: %DOWNLOAD_URL% - powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_WRAPPER_AUTH_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_WRAPPER_AUTH_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_WRAPPER_AUTH_USERNAME%', '%MVNW_WRAPPER_AUTH_PASSWORD%');"^ + "}"^ + "$webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" echo Finished downloading %WRAPPER_JAR% ) @REM End of extension