Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try native image then fallback to pure java version #717

Merged
merged 9 commits into from
Jan 24, 2023
Merged
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
27 changes: 27 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,11 @@
@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 if it works; otherwise use
@REM the pure Java client.
@REM 'native' - use the native client mvnd.exe
gzm55 marked this conversation as resolved.
Show resolved Hide resolved
@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 +48,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 works; otherwise use
# the pure Java client.
# 'native' - use the native client mvnd.exe
gzm55 marked this conversation as resolved.
Show resolved Hide resolved
# '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 @@ -97,6 +97,7 @@
<include>mvnd</include>
<include>mvnd.exe</include>
</directory>
<file touch="platform-${os.detected.name}-${os.detected.arch}"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this line ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the flag file is used to detect whether the bin/mvnd is built for the current os and arch.

</fileSet>

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