Skip to content

Commit

Permalink
[Improve] maven-wrapper downloader improvement (#3214)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfboys authored Oct 7, 2023
1 parent da59576 commit 5755289
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 217 deletions.
39 changes: 0 additions & 39 deletions .mvn/wrapper/MavenWrapperChecker.java

This file was deleted.

86 changes: 0 additions & 86 deletions .mvn/wrapper/MavenWrapperDownloader.java

This file was deleted.

98 changes: 98 additions & 0 deletions .mvn/wrapper/MavenWrapperHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.jar.JarFile;

public final class MavenWrapperHelper {

private static final String WRAPPER_VERSION = "3.2.0";

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

public static void main(String[] args) {
String action = args[0].toLowerCase();
String[] actionArgs = Arrays.copyOfRange(args, 1, args.length);
switch (action) {
case "download":
log("Apache Maven Wrapper Downloader " + WRAPPER_VERSION);
if (actionArgs.length != 2) {
System.err.println(" - ERROR wrapperUrl or wrapperJarPath parameter missing");
System.exit(1);
}
try {
log(" - Downloader started");
final URL wrapperUrl = new URL(actionArgs[0]);
final String jarPath = actionArgs[1].replace("src/main", ""); // Sanitize path
final Path wrapperJarPath = Paths.get(jarPath).toAbsolutePath().normalize();
downloadFileFromURL(wrapperUrl, wrapperJarPath);
log("Done");
} catch (IOException e) {
System.err.println("- Error downloading: " + e.getMessage());
if (VERBOSE) {
e.printStackTrace();
}
System.exit(1);
}
break;
case "verify":
String wrapperJar = actionArgs[0];
log("maven-wrapper file checking: " + wrapperJar);
try (JarFile ignored = new JarFile(wrapperJar, true)) {
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
default:
throw new UnsupportedOperationException("Unknown action \"" + action + "\".");
}
}

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();
Authenticator.setDefault(
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
try (InputStream inStream = wrapperUrl.openStream()) {
Files.copy(inStream, wrapperJarPath, StandardCopyOption.REPLACE_EXISTING);
}
log(" - Downloader complete");
}

private static void log(String msg) {
if (VERBOSE) {
System.out.println(msg);
}
}
}
76 changes: 35 additions & 41 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
have_tty=0
# shellcheck disable=SC2006
if [[ "`tty`" != "not a tty" ]]; then
have_tty=1
have_tty=1
fi

# Bugzilla 37848: When no TTY is available, don't output to console
have_tty=0
# shellcheck disable=SC2006
if [[ "`tty`" != "not a tty" ]]; then
have_tty=1
have_tty=1
fi

# Only use colors if connected to a terminal
Expand All @@ -52,24 +52,24 @@ else
fi

echo_r () {
# Color red: Error, Failed
[[ $# -ne 1 ]] && return 1
# shellcheck disable=SC2059
printf "[%sStreamPark%s] %s$1%s\n" $BLUE $RESET $RED $RESET
# Color red: Error, Failed
[[ $# -ne 1 ]] && return 1
# shellcheck disable=SC2059
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
# 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
# shellcheck disable=SC2059
printf "[%sStreamPark%s] %s$1%s\n" $BLUE $RESET $GREEN $RESET
# Color green: Success
[[ $# -ne 1 ]] && return 1
# shellcheck disable=SC2059
printf "[%sStreamPark%s] %s$1%s\n" $BLUE $RESET $GREEN $RESET
}

# OS specific support. $var _must_ be set to either true or false.
Expand Down Expand Up @@ -176,35 +176,29 @@ build() {
echo_g "Apache StreamPark, building..."
"$PRG_DIR/mvnw" -Pshaded,webapp,dist -DskipTests clean install
if [ $? -eq 0 ]; then
printf '\n'
echo_g """StreamPark project build successful!
dist: $(cd "$PRG_DIR" &>/dev/null && pwd)/dist\n"""
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
javaSource="$PRG_DIR/.mvn/wrapper/MavenWrapperHelper.java"
javaClass="$PRG_DIR/.mvn/wrapper/MavenWrapperHelper.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
[ ! -e "$javaClass" ] && ("$JAVA_HOME/bin/javac" "$javaSource")
if [ -e "$javaClass" ]; then
("$JAVA_HOME/bin/java" -cp "$PRG_DIR/.mvn/wrapper" MavenWrapperHelper "verify" "$wrapperJarPath")
if [ $? -eq 1 ]; then
echo_r "Error: $wrapperJarPath is invalid. retry download it and build project again..."
rm -f $wrapperJarPath
build
fi
fi
fi
fi
else
echo_r "permission denied: $PRG_DIR/mvnw, please check."
Expand Down
10 changes: 5 additions & 5 deletions mvnw
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,21 @@ else
fi
else
log "Falling back to using Java to download"
javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java"
javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class"
javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperHelper.java"
javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperHelper.class"
# 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
log " - Compiling MavenWrapperDownloader.java ..."
log " - Compiling MavenWrapperHelper.java ..."
("$JAVA_HOME/bin/javac" "$javaSource")
fi
if [ -e "$javaClass" ]; then
log " - Running MavenWrapperDownloader.java ..."
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath"
log " - Running MavenWrapperHelper.java ..."
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperHelper "download" "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath"
fi
fi
fi
Expand Down
10 changes: 5 additions & 5 deletions streampark-console/streampark-console-service/src/main/assembly/bin/mvnw
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,21 @@ else
fi
else
log "Falling back to using Java to download"
javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java"
javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class"
javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperHelper.java"
javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperHelper.class"
# 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
log " - Compiling MavenWrapperDownloader.java ..."
log " - Compiling MavenWrapperHelper.java ..."
("$JAVA_HOME/bin/javac" "$javaSource")
fi
if [ -e "$javaClass" ]; then
log " - Running MavenWrapperDownloader.java ..."
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath"
log " - Running MavenWrapperHelper.java ..."
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperHelper "download" "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath"
fi
fi
fi
Expand Down
Loading

0 comments on commit 5755289

Please sign in to comment.