Skip to content

Commit

Permalink
Move fallback logic into main entry script
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gzm55 committed Oct 20, 2022
1 parent a8a1107 commit 7c2349a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 73 deletions.
4 changes: 2 additions & 2 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<configuration>
<skip>false</skip>
<mainClass>org.mvndaemon.mvnd.client.DefaultClient</mainClass>
<imageName>mvnd</imageName>
<imageName>mvnd-native-${os.detected.name}-${os.detected.arch}</imageName>
<buildArgs>
--no-server
--no-fallback
Expand All @@ -170,4 +170,4 @@
</profile>
</profiles>

</project>
</project>
35 changes: 35 additions & 0 deletions dist/src/main/distro/bin/mvnd.sh → dist/src/main/distro/bin/mvnd
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 Down Expand Up @@ -89,6 +92,38 @@ if $mingw ; then
# TODO classpath?
fi

# force execute native image
if [ "${MVND_ENTRY_FALLBACK:-true}" = false ]; then
MVND_CMD=$(echo "$MVND_HOME"/bin/mvnd-native-*)
[ ! -x "$MVND_CMD" ] || exec "$MVND_CMD" "$@"
echo "Cannot run native mvnd '${MVND_CMD-}'!" >&2
exit 1
fi

case "$(uname -a)" in
(CYGWIN*|MINGW*) os=windows arch=$(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-}" != arm64 ] || arch=aarch64

# try native image
MVND_CMD="$MVND_HOME/bin/mvnd-native-$os-$arch"
if [ -x "$MVND_CMD" ]; then
is_native=true
if [ "$os" = linux ]; then
case "$(ldd "$MVND_CMD")" 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
67 changes: 0 additions & 67 deletions dist/src/main/distro/bin/mvnd-auto

This file was deleted.

16 changes: 16 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.
@REM -----------------------------------------------------------------------------

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

set ERROR_CODE=0

@REM try execute native image
set "MVND_CMD=%~dp0\mvnd-native-windows-%PROCESSOR_ARCHITECTURE:x64=amd64%.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
4 changes: 2 additions & 2 deletions dist/src/main/provisio/maven-distro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
</fileSet>
<fileSet to="/bin">
<directory path="${basedir}/../client/target">
<include>mvnd</include>
<include>mvnd.exe</include>
<include>mvnd-native-${os.detected.name}-${os.detected.arch}</include>
<include>mvnd-native-${os.detected.name}-${os.detected.arch}.exe</include>
</directory>
</fileSet>

Expand Down
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ limitations under the License.</inlineHeader>
<groovy>SLASHSTAR_STYLE</groovy>
<java>SLASHSTAR_STYLE</java>
<mvn>SCRIPT_STYLE</mvn>
<mvnd.sh>SCRIPT_STYLE</mvnd.sh>
<mvnd-auto>SCRIPT_STYLE</mvnd-auto>
<mvnd>SCRIPT_STYLE</mvnd>
<vbs>APOSTROPHE_STYLE</vbs>
</mapping>
<skipExistingHeaders>true</skipExistingHeaders><!-- we want to keep third party license headers -->
Expand Down

0 comments on commit 7c2349a

Please sign in to comment.