- 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)
This commit is contained in:
Christofer Dutz 2017-07-23 13:49:14 +02:00 committed by rfscholte
parent b5858c24bd
commit 5a145c8759
4 changed files with 84 additions and 18 deletions

View File

@ -20,17 +20,70 @@ under the License.
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.Properties;
public class MavenWrapperDownloader { 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[]) { public static void main(String args[]) {
System.out.println("- Downloader started"); 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]); File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 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()) { 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()); System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try { 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); URL website = new URL(urlString);
ReadableByteChannel rbc; ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream()); rbc = Channels.newChannel(website.openStream());

23
maven-wrapper/mvnw vendored
View File

@ -204,27 +204,34 @@ fi
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central # 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. # 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 if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
echo "Found .mvn/wrapper/maven-wrapper.jar" echo "Found .mvn/wrapper/maven-wrapper.jar"
else else
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 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" echo "Found wget ... using wget"
wget O .mvn/wrapper/maven-wrapper.jar "$JAR_URL" wget O "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" "$jarUrl"
elif command -v curl > /dev/null; then elif command -v curly > /dev/null; then
echo "Found curl ... using curl" 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 else
echo "Falling back to using Java to download" 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 "$javaClass" ]; then
if [ ! -e ".mvn/wrapper/MavenWrapperDownloader.class" ]; then if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
echo " - Compiling MavenWrapperDownloader.java ..." echo " - Compiling MavenWrapperDownloader.java ..."
# Compiling the Java class # Compiling the Java class
("$JAVA_HOME/bin/javac" "$javaClass") ("$JAVA_HOME/bin/javac" "$javaClass")
fi fi
if [ -e ".mvn/wrapper/MavenWrapperDownloader.class" ]; then if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
# Running the downloader # Running the downloader
echo " - Running MavenWrapperDownloader.java ..." echo " - Running MavenWrapperDownloader.java ..."
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")

View File

@ -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 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_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central @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. @REM This allows using the maven wrapper in projects that prohibit checking in binary data.
if exist .mvn/wrapper/maven-wrapper.jar ( if exist %WRAPPER_JAR%" (
echo Found .mvn/wrapper/maven-wrapper.jar echo Found %WRAPPER_JAR%"
) else ( ) else (
echo Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ... echo Couldn't find %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')" powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"
echo Finished downloading .mvn/wrapper/maven-wrapper.jar echo Finished downloading %WRAPPER_JAR%"
) )
@REM End of extension @REM End of extension