From 5a145c87599ad36a891429d441d692ca1efe9faf Mon Sep 17 00:00:00 2001 From: Christofer Dutz Date: Sun, 23 Jul 2017 13:49:14 +0200 Subject: [PATCH] - Made the wrapperUrl configurable from the maven-wrapper.properties file - Updated and tested the linux/unix/mac version of the script - Updated and tested the Java downloader - Updated the windows cmd (but not tested yet) (Need to test the changes to the windows command on a windows machine) --- .../.mvn/wrapper/MavenWrapperDownloader.java | 61 +++++++++++++++++-- .../.mvn/wrapper/maven-wrapper.properties | 2 +- maven-wrapper/mvnw | 23 ++++--- maven-wrapper/mvnw.cmd | 16 +++-- 4 files changed, 84 insertions(+), 18 deletions(-) diff --git a/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java b/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java index 61f85acea5..fdbdd022ca 100644 --- a/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java +++ b/maven-wrapper/.mvn/wrapper/MavenWrapperDownloader.java @@ -20,17 +20,70 @@ under the License. import java.net.*; import java.io.*; import java.nio.channels.*; +import java.util.Properties; public class MavenWrapperDownloader { + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = + "http://central.maven.org/maven2/io/takari/maven-wrapper/0.2.1/maven-wrapper-0.2.1.jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + public static void main(String args[]) { System.out.println("- Downloader started"); - String url="http://central.maven.org/maven2/io/takari/maven-wrapper/0.2.1/maven-wrapper-0.2.1.jar"; File baseDirectory = new File(args[0]); System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); - File outputFile = new File(baseDirectory.getAbsolutePath(), ".mvn/wrapper/maven-wrapper.jar"); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if(mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if(mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: : " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); if(!outputFile.getParentFile().exists()) { - outputFile.getParentFile().mkdirs(); + if(!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } } System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); try { @@ -44,7 +97,7 @@ public class MavenWrapperDownloader { } } - public static void downloadFileFromURL(String urlString, File destination) throws Exception { + private static void downloadFileFromURL(String urlString, File destination) throws Exception { URL website = new URL(urlString); ReadableByteChannel rbc; rbc = Channels.newChannel(website.openStream()); diff --git a/maven-wrapper/.mvn/wrapper/maven-wrapper.properties b/maven-wrapper/.mvn/wrapper/maven-wrapper.properties index a447c9fa81..9dda3b659b 100644 --- a/maven-wrapper/.mvn/wrapper/maven-wrapper.properties +++ b/maven-wrapper/.mvn/wrapper/maven-wrapper.properties @@ -1 +1 @@ -distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip \ No newline at end of file +distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip diff --git a/maven-wrapper/mvnw b/maven-wrapper/mvnw index b16e3813a2..5c8094eb74 100755 --- a/maven-wrapper/mvnw +++ b/maven-wrapper/mvnw @@ -204,27 +204,34 @@ fi # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central # This allows using the maven wrapper in projects that prohibit checking in binary data. ########################################################################################## -JAR_URL="http://central.maven.org/maven2/io/takari/maven-wrapper/0.2.1/maven-wrapper-0.2.1.jar" if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then echo "Found .mvn/wrapper/maven-wrapper.jar" else echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." - if command -v wget > /dev/null; then + + jarUrl="http://central.maven.org/maven2/io/takari/maven-wrapper/0.2.1/maven-wrapper-0.2.1.jar" + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + echo "Downloading from: $jarUrl" + + if command -v wgety > /dev/null; then echo "Found wget ... using wget" - wget ‐O .mvn/wrapper/maven-wrapper.jar "$JAR_URL" - elif command -v curl > /dev/null; then + wget ‐O "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" "$jarUrl" + elif command -v curly > /dev/null; then echo "Found curl ... using curl" - curl -o .mvn/wrapper/maven-wrapper.jar "$JAR_URL" + curl -o "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" "$jarUrl" else echo "Falling back to using Java to download" - javaClass=".mvn/wrapper/MavenWrapperDownloader.java" + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" if [ -e "$javaClass" ]; then - if [ ! -e ".mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then echo " - Compiling MavenWrapperDownloader.java ..." # Compiling the Java class ("$JAVA_HOME/bin/javac" "$javaClass") fi - if [ -e ".mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then # Running the downloader echo " - Running MavenWrapperDownloader.java ..." ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") diff --git a/maven-wrapper/mvnw.cmd b/maven-wrapper/mvnw.cmd index b3bd9faa41..9aa55a5e1c 100644 --- a/maven-wrapper/mvnw.cmd +++ b/maven-wrapper/mvnw.cmd @@ -118,17 +118,23 @@ for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do s SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set DOWNLOAD_URL="http://central.maven.org/maven2/io/takari/maven-wrapper/0.2.1/maven-wrapper-0.2.1.jar" +FOR /F "tokens=3,* delims=.=" %%G IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( set %%G=%%H ) + if "%%G"=="wrapperUrl" + set DOWNLOAD_URL=%%H +echo Downloading from: "%DOWNLOAD_URL%" + set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central @REM This allows using the maven wrapper in projects that prohibit checking in binary data. -if exist .mvn/wrapper/maven-wrapper.jar ( - echo Found .mvn/wrapper/maven-wrapper.jar +if exist %WRAPPER_JAR%" ( + echo Found %WRAPPER_JAR%" ) else ( - echo Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ... - powershell -Command "(New-Object Net.WebClient).DownloadFile('http://central.maven.org/maven2/io/takari/maven-wrapper/0.2.1/maven-wrapper-0.2.1.jar', '.mvn/wrapper/maven-wrapper.jar')" - echo Finished downloading .mvn/wrapper/maven-wrapper.jar + echo Couldn't find %WRAPPER_JAR%", downloading it ... + powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" + echo Finished downloading %WRAPPER_JAR%" ) @REM End of extension