From 898425b79e2287834c221f194431c0688a1db415 Mon Sep 17 00:00:00 2001 From: benjobs Date: Fri, 6 Oct 2023 19:56:09 +0800 Subject: [PATCH 1/3] [Improve] maven-wrapper downloader improvement --- .mvn/wrapper/MavenWrapperChecker.java | 39 -------- .mvn/wrapper/MavenWrapperDownloader.java | 86 ---------------- .mvn/wrapper/MavenWrapperHelper.java | 98 +++++++++++++++++++ build.sh | 21 ++-- mvnw | 10 +- .../console/MavenWrapperChecker.java | 41 -------- 6 files changed, 112 insertions(+), 183 deletions(-) delete mode 100644 .mvn/wrapper/MavenWrapperChecker.java delete mode 100644 .mvn/wrapper/MavenWrapperDownloader.java create mode 100644 .mvn/wrapper/MavenWrapperHelper.java delete mode 100644 streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/MavenWrapperChecker.java diff --git a/.mvn/wrapper/MavenWrapperChecker.java b/.mvn/wrapper/MavenWrapperChecker.java deleted file mode 100644 index 70c98524e0..0000000000 --- a/.mvn/wrapper/MavenWrapperChecker.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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); - } - } - -} diff --git a/.mvn/wrapper/MavenWrapperDownloader.java b/.mvn/wrapper/MavenWrapperDownloader.java deleted file mode 100644 index 3d765bee0b..0000000000 --- a/.mvn/wrapper/MavenWrapperDownloader.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.LinkOption; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.nio.file.StandardOpenOption; -import java.util.Properties; - -public final class MavenWrapperDownloader { - - 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) { - log("Apache Maven Wrapper Downloader " + WRAPPER_VERSION); - - if (args.length != 2) { - System.err.println(" - ERROR wrapperUrl or wrapperJarPath parameter missing"); - System.exit(1); - } - - try { - 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"); - } catch (IOException e) { - System.err.println("- Error downloading: " + e.getMessage()); - if (VERBOSE) { - e.printStackTrace(); - } - System.exit(1); - } - } - - 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); - } - } - -} diff --git a/.mvn/wrapper/MavenWrapperHelper.java b/.mvn/wrapper/MavenWrapperHelper.java new file mode 100644 index 0000000000..da55d396e2 --- /dev/null +++ b/.mvn/wrapper/MavenWrapperHelper.java @@ -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); + } + } +} diff --git a/build.sh b/build.sh index ba6859443e..baf384ceed 100755 --- a/build.sh +++ b/build.sh @@ -180,9 +180,8 @@ build() { 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" + 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 @@ -190,17 +189,15 @@ build() { 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 + [ ! -e "$javaClass" ] && ("$JAVA_HOME/bin/javac" "$javaSource") 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..." + echo_g "Check maven-wrapper starting..." + ("$JAVA_HOME/bin/java" -cp "$PRG_DIR/.mvn/wrapper" MavenWrapperHelper "verify" "$wrapperJarPath") + if [ $? -eq 0 ]; then + echo_r "" + else + echo_y "Error: $wrapperJarPath is invalid. retry download it and build project again..." rm -f $wrapperJarPath - echo_y " build project again..." build fi fi diff --git a/mvnw b/mvnw index 66df285428..916dee8a34 100755 --- a/mvnw +++ b/mvnw @@ -228,8 +228,8 @@ 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") @@ -237,12 +237,12 @@ else 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 diff --git a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/MavenWrapperChecker.java b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/MavenWrapperChecker.java deleted file mode 100644 index 5496e77994..0000000000 --- a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/MavenWrapperChecker.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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. - */ - -package org.apache.streampark.console; - -import java.util.jar.JarFile; - -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); - } - } -} From 6920452019d8a3715012295f48a4ed44bda6dc8e Mon Sep 17 00:00:00 2001 From: benjobs Date: Fri, 6 Oct 2023 19:59:39 +0800 Subject: [PATCH 2/3] mvnw minor improvement --- .../src/main/assembly/bin/mvnw | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) mode change 100644 => 100755 streampark-console/streampark-console-service/src/main/assembly/bin/mvnw diff --git a/streampark-console/streampark-console-service/src/main/assembly/bin/mvnw b/streampark-console/streampark-console-service/src/main/assembly/bin/mvnw old mode 100644 new mode 100755 index 66df285428..916dee8a34 --- a/streampark-console/streampark-console-service/src/main/assembly/bin/mvnw +++ b/streampark-console/streampark-console-service/src/main/assembly/bin/mvnw @@ -228,8 +228,8 @@ 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") @@ -237,12 +237,12 @@ else 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 From 04f5a6a5c1c75076c493fba00910a2b20ea98e2f Mon Sep 17 00:00:00 2001 From: benjobs Date: Fri, 6 Oct 2023 21:22:34 +0800 Subject: [PATCH 3/3] codestyle improvement --- build.sh | 73 +++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/build.sh b/build.sh index baf384ceed..cad35d8eea 100755 --- a/build.sh +++ b/build.sh @@ -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 @@ -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. @@ -176,32 +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 - 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 - echo_g "Check maven-wrapper starting..." - ("$JAVA_HOME/bin/java" -cp "$PRG_DIR/.mvn/wrapper" MavenWrapperHelper "verify" "$wrapperJarPath") - if [ $? -eq 0 ]; then - echo_r "" - else - echo_y "Error: $wrapperJarPath is invalid. retry download it and build project again..." - rm -f $wrapperJarPath - 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."