Skip to content

Commit

Permalink
[Improve] Upgrade maven-wrapper to 3.2.0 (#3213)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfboys authored Oct 6, 2023
1 parent 809eda8 commit da59576
Show file tree
Hide file tree
Showing 10 changed files with 556 additions and 325 deletions.
39 changes: 39 additions & 0 deletions .mvn/wrapper/MavenWrapperChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.jar.JarFile;
import java.util.zip.ZipException;
public class MavenWrapperChecker {

private static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("MVNW_VERBOSE"));

public static void main(String[] args) {
String wrapperJar = args[0];
log("maven-wrapper file checking: " + wrapperJar);
try(JarFile ignored = new JarFile(wrapperJar, true)) {
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
}
private static void log(String msg) {
if (VERBOSE) {
System.out.println(msg);
}
}

}
93 changes: 19 additions & 74 deletions .mvn/wrapper/MavenWrapperDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,69 +29,38 @@
import java.util.Properties;

public final class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "3.1.1";

private static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("MVNW_VERBOSE"));

/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL =
"https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/" + WRAPPER_VERSION
+ "/maven-wrapper-" + WRAPPER_VERSION + ".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";
private static final String WRAPPER_VERSION = "3.2.0";

/**
* 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";
private static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("MVNW_VERBOSE"));

public static void main(String[] args) {
if (args.length == 0) {
System.err.println(" - ERROR projectBasedir parameter missing");
System.exit(1);
}
log("Apache Maven Wrapper Downloader " + WRAPPER_VERSION);

log(" - Downloader started");
final String dir = args[0].replace("..", ""); // Sanitize path
final Path projectBasedir = Paths.get(dir).toAbsolutePath().normalize();
if (!Files.isDirectory(projectBasedir, LinkOption.NOFOLLOW_LINKS)) {
System.err.println(" - ERROR projectBasedir not exists: " + projectBasedir);
if (args.length != 2) {
System.err.println(" - ERROR wrapperUrl or wrapperJarPath parameter missing");
System.exit(1);
}

log(" - Using base directory: " + projectBasedir);

// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
Path mavenWrapperPropertyFile = projectBasedir.resolve(MAVEN_WRAPPER_PROPERTIES_PATH);
String url = readWrapperUrl(mavenWrapperPropertyFile);

try {
Path outputFile = projectBasedir.resolve(MAVEN_WRAPPER_JAR_PATH);
createDirectories(outputFile.getParent());
downloadFileFromURL(url, outputFile);
log(" - Downloader started");
final URL wrapperUrl = new URL(args[0]);
final String jarPath = args[1].replace("..", ""); // Sanitize path
final Path wrapperJarPath = Paths.get(jarPath).toAbsolutePath().normalize();
downloadFileFromURL(wrapperUrl, wrapperJarPath);
log("Done");
System.exit(0);
} catch (IOException e) {
System.err.println("- Error downloading");
e.printStackTrace();
System.err.println("- Error downloading: " + e.getMessage());
if (VERBOSE) {
e.printStackTrace();
}
System.exit(1);
}
}

private static void downloadFileFromURL(String urlString, Path destination) throws IOException {
log(" - Downloading to: " + destination);
private static void downloadFileFromURL(URL wrapperUrl, Path wrapperJarPath)
throws IOException {
log(" - Downloading to: " + wrapperJarPath);
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
final String username = System.getenv("MVNW_USERNAME");
final char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Expand All @@ -102,36 +71,12 @@ protected PasswordAuthentication getPasswordAuthentication() {
}
});
}
URL website = new URL(urlString);
try (InputStream inStream = website.openStream()) {
Files.copy(inStream, destination, StandardCopyOption.REPLACE_EXISTING);
try (InputStream inStream = wrapperUrl.openStream()) {
Files.copy(inStream, wrapperJarPath, StandardCopyOption.REPLACE_EXISTING);
}
log(" - Downloader complete");
}

private static void createDirectories(Path outputPath) throws IOException {
if (!Files.isDirectory(outputPath, LinkOption.NOFOLLOW_LINKS)) {
Path createDirectories = Files.createDirectories(outputPath);
log(" - Directories created: " + createDirectories);
}
}

private static String readWrapperUrl(Path mavenWrapperPropertyFile) {
String url = DEFAULT_DOWNLOAD_URL;
if (Files.exists(mavenWrapperPropertyFile, LinkOption.NOFOLLOW_LINKS)) {
log(" - Reading property file: " + mavenWrapperPropertyFile);
try (InputStream in = Files.newInputStream(mavenWrapperPropertyFile, StandardOpenOption.READ)) {
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(in);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, DEFAULT_DOWNLOAD_URL);
} catch (IOException e) {
System.err.println(" - ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
}
}
log(" - Downloading from: " + url);
return url;
}

private static void log(String msg) {
if (VERBOSE) {
System.out.println(msg);
Expand Down
2 changes: 1 addition & 1 deletion .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
#

distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
101 changes: 95 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ echo_r () {
printf "[%sStreamPark%s] %s$1%s\n" $BLUE $RESET $RED $RESET
}

echo_y () {
# Color yellow: Warning
[[ $# -ne 1 ]] && return 1
# shellcheck disable=SC2059
printf "[%sStreamPark%s] %s$1%s\n" $BLUE $RESET $YELLOW $RESET
}

echo_g () {
# Color green: Success
[[ $# -ne 1 ]] && return 1
Expand All @@ -66,14 +73,69 @@ echo_g () {
}

# OS specific support. $var _must_ be set to either true or false.
cygwin=false
os400=false
# shellcheck disable=SC2006
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
cygwin=false;
darwin=false;
mingw=false
case "$(uname)" in
CYGWIN*) cygwin=true ;;
MINGW*) mingw=true;;
Darwin*) darwin=true
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
if [ -z "$JAVA_HOME" ]; then
if [ -x "/usr/libexec/java_home" ]; then
JAVA_HOME="$(/usr/libexec/java_home)"; export JAVA_HOME
else
JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
fi
fi
;;
esac

if [ -z "$JAVA_HOME" ] ; then
if [ -r /etc/gentoo-release ] ; then
JAVA_HOME=$(java-config --jre-home)
fi
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=$(cygpath --unix "$JAVA_HOME")
[ -n "$CLASSPATH" ] &&
CLASSPATH=$(cygpath --path --unix "$CLASSPATH")
fi

# For Mingw, ensure paths are in UNIX format before anything is touched
if $mingw ; then
[ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] &&
JAVA_HOME="$(cd "$JAVA_HOME" || (echo "cannot cd into $JAVA_HOME."; exit 1); pwd)"
fi

if [ -z "$JAVA_HOME" ]; then
javaExecutable="$(which javac)"
if [ -n "$javaExecutable" ] && ! [ "$(expr "\"$javaExecutable\"" : '\([^ ]*\)')" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=$(which readlink)
if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then
if $darwin ; then
javaHome="$(dirname "\"$javaExecutable\"")"
javaExecutable="$(cd "\"$javaHome\"" && pwd -P)/javac"
else
javaExecutable="$(readlink -f "\"$javaExecutable\"")"
fi
javaHome="$(dirname "\"$javaExecutable\"")"
javaHome=$(expr "$javaHome" : '\(.*\)/bin')
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
fi

if [ -z "$JAVA_HOME" ] ; then
echo "Warning: JAVA_HOME environment variable is not set."
fi

# resolve links - $0 may be a softlink
PRG="$0"

Expand All @@ -90,6 +152,7 @@ while [[ -h "$PRG" ]]; do
fi
done


# Get standard environment variables
# shellcheck disable=SC2006
PRG_DIR=`dirname "$PRG"`
Expand All @@ -116,6 +179,32 @@ build() {
printf '\n'
echo_g """StreamPark project build successful!
dist: $(cd "$PRG_DIR" &>/dev/null && pwd)/dist\n"""
else
echo_g "Check maven-wrapper starting..."
javaSource="$PRG_DIR/.mvn/wrapper/MavenWrapperChecker.java"
javaClass="$PRG_DIR/.mvn/wrapper/MavenWrapperChecker.class"
wrapperJarPath="$PRG_DIR/.mvn/wrapper/maven-wrapper.jar"
# For Cygwin, switch paths to Windows format before running javac
if $cygwin; then
javaSource=$(cygpath --path --windows "$javaSource")
javaClass=$(cygpath --path --windows "$javaClass")
fi
if [ -e "$javaSource" ]; then
if [ ! -e "$javaClass" ]; then
echo_g " - Compiling MavenWrapperChecker.java ..."
("$JAVA_HOME/bin/javac" "$javaSource")
fi
if [ -e "$javaClass" ]; then
echo_y " - Running MavenWrapperChecker.java ..."
"$JAVA_HOME/bin/java" -cp "$PRG_DIR/.mvn/wrapper" MavenWrapperChecker "$wrapperJarPath"
if [ $? -eq 1 ]; then
echo_y " $wrapperJarPath is invalid. now remove maven-wrapper..."
rm -f $wrapperJarPath
echo_y " build project again..."
build
fi
fi
fi
fi
else
echo_r "permission denied: $PRG_DIR/mvnw, please check."
Expand Down
Loading

0 comments on commit da59576

Please sign in to comment.