- Made the script use curl or wget if available and fallback to java in case none of them are.

- Moved the Java source code to the filesystem.
This commit is contained in:
Christofer Dutz 2017-07-14 13:46:56 +02:00 committed by rfscholte
parent 5a6e53da1c
commit 69369d2ead
2 changed files with 69 additions and 57 deletions

View File

@ -0,0 +1,38 @@
import java.net.*;
import java.io.*;
import java.nio.channels.*;
public class MavenWrapperDownloader {
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(!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
public static void downloadFileFromURL(String urlString, File destination) throws Exception {
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}

88
maven-wrapper/mvnw vendored
View File

@ -200,70 +200,44 @@ if [ -z "$BASE_DIR" ]; then
exit 1; exit 1;
fi 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 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 which wget >/dev/null; then
javaClass="/tmp/MavenWrapperDownloader.java" echo "Found wget ... using wget"
if [ ! -e "$javaClass" ]; then wget output-document=.mvn/wrapper/maven-wrapper.jar "$JAR_URL"
echo " - Generating MavenWrapperDownloader.java ..." elif which curl >/dev/null; then
# Generating downloader application java class ... echo "Found curl ... using curl"
cat <<EOF >"$javaClass" curl -o .mvn/wrapper/maven-wrapper.jar "$JAR_URL"
import java.net.*; else
import java.io.*; echo "Falling back to using Java to download"
import java.nio.channels.*; javaClass=".mvn/wrapper/MavenWrapperDownloader.java"
if [ -e "$javaClass" ]; then
public class MavenWrapperDownloader { if [ ! -e ".mvn/wrapper/MavenWrapperDownloader.class" ]; then
echo " - Compiling MavenWrapperDownloader.java ..."
public static void main(String args[]) { # Compiling the Java class
System.out.println("- Downloader started"); ("$JAVA_HOME/bin/javac" "$javaClass")
String url="http://central.maven.org/maven2/io/takari/maven-wrapper/0.2.1/maven-wrapper-0.2.1.jar"; fi
File baseDirectory = new File(args[0]); if [ -e ".mvn/wrapper/MavenWrapperDownloader.class" ]; then
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); # Running the downloader
File outputFile = new File(baseDirectory.getAbsolutePath(), ".mvn/wrapper/maven-wrapper.jar"); echo " - Running MavenWrapperDownloader.java ..."
if(!outputFile.getParentFile().exists()) { ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
outputFile.getParentFile().mkdirs(); fi
} fi
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
public static void downloadFileFromURL(String urlString, File destination) throws Exception {
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
EOF
fi fi
classFile=$(dirname "$javaClass")/MavenWrapperDownloader.class
if [ ! -e "$classFile" ]; then
echo " - Compiling MavenWrapperDownloader.java ..."
# Compiling the Java class
("$JAVA_HOME/bin/javac" "$javaClass")
fi
# Running the downloader
echo " - Running MavenWrapperDownloader.java ..."
echo "$MAVEN_PROJECTBASEDIR"
("$JAVA_HOME/bin/java" -cp /tmp MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
fi fi
##########################################################################################
# End of extension
##########################################################################################
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
if [ "$MVNW_VERBOSE" = true ]; then if [ "$MVNW_VERBOSE" = true ]; then
echo $MAVEN_PROJECTBASEDIR echo $MAVEN_PROJECTBASEDIR