Skip to content

Commit

Permalink
Try native image then fallback to pure java version (#717)
Browse files Browse the repository at this point in the history
* 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-<os>-<arch>'
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
  • Loading branch information
gzm55 committed Jan 24, 2023
1 parent 4f90d55 commit 6f28a18
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
Expand Down
28 changes: 28 additions & 0 deletions dist/src/main/distro/bin/mvnd.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
Expand Down
39 changes: 37 additions & 2 deletions dist/src/main/distro/bin/mvnd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions dist/src/main/provisio/maven-distro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<include>mvnd</include>
<include>mvnd.exe</include>
</directory>
<file touch="platform-${os.detected.name}-${os.detected.arch}"/>
</fileSet>

<archive name="maven-mvnd-${project.version}-${os.detected.name}-${os.detected.arch}.zip"
Expand Down

0 comments on commit 6f28a18

Please sign in to comment.