From 6f28a189b6448d64693098fa0eb20e5ff2e66940 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Tue, 24 Jan 2023 17:51:56 +0800 Subject: [PATCH] Try native image then fallback to pure java version (#717) * Add script mvnd-auto to auto select native or pure java mvnd * Move fallback logic into main entry script 1. rename native binary to 'mvnd-native--' 2. add environment switch MVND_ENTRY_FALLBACK, default 'true' enables the fallback logic, set to 'false' to force execute the native mvnd. 3. rename mvnd.sh to mvnd * change entry name on windows * Add script mvnd-persist-native for moving the native image to the default entry path * improve platform detect * fix error on dash * rollback default entry to the native image * use MVND_CLIENT switch to control the selection of mvnd client * improve comment docs as suggestion --- client/pom.xml | 2 +- dist/src/main/distro/bin/mvnd.cmd | 28 ++++++++++++++++++ dist/src/main/distro/bin/mvnd.sh | 39 +++++++++++++++++++++++-- dist/src/main/provisio/maven-distro.xml | 1 + 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/client/pom.xml b/client/pom.xml index f9808d067..8fb167f92 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -225,7 +225,7 @@ - build + compile-no-fork package diff --git a/dist/src/main/distro/bin/mvnd.cmd b/dist/src/main/distro/bin/mvnd.cmd index a14caedf5..50fbeea16 100644 --- a/dist/src/main/distro/bin/mvnd.cmd +++ b/dist/src/main/distro/bin/mvnd.cmd @@ -25,6 +25,12 @@ @REM MAVEN_BATCH_PAUSE (Optional) set to 'on' to wait for a key stroke before ending. @REM MAVEN_OPTS (Optional) Java runtime options used when Maven is executed. @REM MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files. +@REM MVND_CLIENT (Optional) Control how to select mvnd client to communicate with the daemon: +@REM 'auto' (default) - prefer the native client mvnd.exe if it suits the current +@REM OS and processor architecture; otherwise use the pure +@REM Java client. +@REM 'native' - use the native client mvnd.exe +@REM 'jvm' - use the pure Java client @REM ----------------------------------------------------------------------------- @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' @@ -43,6 +49,28 @@ if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* set ERROR_CODE=0 +set "MVND_CMD=%~dp0\mvnd.exe" +if "%MVND_CLIENT%"=="native" goto runNative +if "%MVND_CLIENT%"=="" goto checkNative +if not "%MVND_CLIENT%"=="auto" goto runJvm + +:checkNative +@REM try execute native image +if "%PROCESSOR_ARCHITEW6432%"=="" ( + if not exist "%~dp0\platform-windows-%PROCESSOR_ARCHITECTURE%" goto runJvm +) else ( + if not exist "%~dp0\platform-windows-%PROCESSOR_ARCHITEW6432%" goto runJvm +) +if not exist "%MVND_CMD%" goto runJvm + +:runNative +"%MVND_CMD%" %* +if ERRORLEVEL 1 goto error +goto end + +:runJvm +@REM fallback to pure java version + @REM ==== START VALIDATION ==== if not "%JAVA_HOME%"=="" goto OkJHome for %%i in (java.exe) do set "JAVACMD=%%~$PATH:i" diff --git a/dist/src/main/distro/bin/mvnd.sh b/dist/src/main/distro/bin/mvnd.sh index 029ac1a59..72eafe544 100755 --- a/dist/src/main/distro/bin/mvnd.sh +++ b/dist/src/main/distro/bin/mvnd.sh @@ -25,6 +25,11 @@ # JAVA_HOME Must point at your Java Development Kit installation. # MAVEN_OPTS (Optional) Java runtime options used when Maven is executed. # MAVEN_SKIP_RC (Optional) Flag to disable loading of mavenrc files. +# MVND_CLIENT (Optional) Control how to select mvnd client to communicate with the daemon: +# 'auto' (default) - prefer the native client mvnd if it suits the current OS and +# processor architecture; otherwise use the pure Java client. +# 'native' - use the native client mvnd +# 'jvm' - use the pure Java client # ----------------------------------------------------------------------------- if [ -z "$MAVEN_SKIP_RC" ] ; then @@ -43,8 +48,8 @@ fi cygwin=false; mingw=false; case "`uname`" in - CYGWIN*) cygwin=true;; - MINGW*) mingw=true;; + CYGWIN*) cygwin=true native_ext=.exe ;; + MINGW*) mingw=true native_ext=.exe ;; esac ## resolve links - $0 may be a link to Maven's home @@ -89,6 +94,36 @@ if $mingw ; then # TODO classpath? fi +if [ "${MVND_CLIENT:=auto}" = native ]; then + # force execute native image + exec "$MVND_HOME/bin/mvnd${native_ext:-}" "$@" +elif [ "$MVND_CLIENT" = auto ]; then + # try native image + case "$(uname -a)" in + (CYGWIN*|MINGW*) os=windows arch="${PROCESSOR_ARCHITEW6432:-"${PROCESSOR_ARCHITECTURE:-"$(uname -m)"}"}" ;; + (Darwin*x86_64) os=darwin arch=amd64 ;; + (Darwin*arm64) os=darwin arch=aarch64 ;; + (Linux*) os=linux arch=$(uname -m) ;; + (*) os=$(uname) arch=$(uname -m) ;; + esac + [ "${arch-}" != x86_64 ] || arch=amd64 + [ "${arch-}" != AMD64 ] || arch=amd64 + [ "${arch-}" != arm64 ] || arch=aarch64 + + MVND_CMD="$MVND_HOME/bin/mvnd${native_ext:-}" + if [ -e "$MVND_HOME/bin/platform-$os-$arch" ] && [ -x "$MVND_CMD" ]; then + is_native=true + if [ "$os" = linux ]; then + case "$(ldd "$MVND_CMD" 2>&1)" in + (''|*"not found"*) is_native=false ;; + esac + fi + ! $is_native || exec "$MVND_CMD" "$@" + fi +fi + +# fallback to pure java version + if [ -z "$JAVA_HOME" ] ; then JAVACMD="`\\unset -f command; \\command -v java`" else diff --git a/dist/src/main/provisio/maven-distro.xml b/dist/src/main/provisio/maven-distro.xml index 8f0605d5c..0675148ab 100644 --- a/dist/src/main/provisio/maven-distro.xml +++ b/dist/src/main/provisio/maven-distro.xml @@ -79,6 +79,7 @@ mvnd mvnd.exe +