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
21 changes: 21 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,9 @@
@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_ENTRY_FALLBACK (Optional) Flag to disable fallback to pure java mvnd,
@REM default 'true' enable the fallback,
@REM set to 'false' to force execute the native mvnd.
gzm55 marked this conversation as resolved.
Show resolved Hide resolved
@REM -----------------------------------------------------------------------------

@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
Expand All @@ -43,6 +46,24 @@ if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %*

set ERROR_CODE=0

@REM try execute native image
if "%PROCESSOR_ARCHITEW6432%"=="" (
if not exist "%~dp0\platform-windows-%PROCESSOR_ARCHITECTURE%" goto noNativeImage
) else (
if not exist "%~dp0\platform-windows-%PROCESSOR_ARCHITEW6432%" goto noNativeImage
)
set "MVND_CMD=%~dp0\mvnd.exe"
if not exist "%MVND_CMD%" goto noNativeImage
"%MVND_CMD%" %*
if ERRORLEVEL 1 goto error
goto end
:noNativeImage
if not "%MVND_ENTRY_FALLBACK%"=="false" goto fallback
echo Cannot run native mvnd %MVND_CMD%! >&2
goto error
:fallback
@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
37 changes: 35 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,9 @@
# 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_ENTRY_FALLBACK (Optional) Flag to disable fallback to pure java mvnd,
# default 'true' enable the fallback,
# set to 'false' to force execute the native mvnd.
# -----------------------------------------------------------------------------

if [ -z "$MAVEN_SKIP_RC" ] ; then
Expand All @@ -43,8 +46,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 +92,36 @@ if $mingw ; then
# TODO classpath?
fi

# force execute native image
if [ "${MVND_ENTRY_FALLBACK:-true}" = false ]; then
exec "$MVND_HOME/bin/mvnd${native_ext:-}" "$@"
fi

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

# try native image
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

# 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