Skip to content

Commit

Permalink
maven-wrapper improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfboys committed Oct 6, 2023
1 parent 0f7954a commit e3844e3
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 6 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);
}
}

}
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.jar.JarFile;

@Slf4j
@Data
Expand Down Expand Up @@ -193,6 +194,15 @@ public String getMavenArgs() {
CommandUtils.execute("mvn --version");
}
} catch (Exception e) {
File wrapperJar = new File(WebUtils.getAppHome().concat("/.mvn/wrapper/maven-wrapper.jar"));
if (wrapperJar.exists()) {
try {
JarFile jarFile = new JarFile(wrapperJar, true);
jarFile.close();
} catch (Exception ignored) {
FileUtils.deleteQuietly(wrapperJar);
}
}
if (windows) {
mvn = WebUtils.getAppHome().concat("/bin/mvnw.cmd");
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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);
}
}
}

0 comments on commit e3844e3

Please sign in to comment.