From 672ed329a5995e5dc6daddd0cdd7c78fc2e593a7 Mon Sep 17 00:00:00 2001 From: Derek Wickern Date: Fri, 29 Dec 2023 12:55:18 -0800 Subject: [PATCH 1/3] bat script: reduce nesting in process_args This will make it easier to support new arguments. It shouldn't change the functionality. --- .../packager/archetypes/scripts/bat-template | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template index 013c0e61e..e25165f24 100644 --- a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template +++ b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template @@ -135,23 +135,21 @@ exit /B 0 rem Processes incoming arguments and places them in appropriate global variables :process_args :param_loop - call set _PARAM1=%%1 - set "_TEST_PARAM=%~1" + shift + call set _PARAM1=%%0 + set "_TEST_PARAM=%~0" if ["!_PARAM1!"]==[""] goto param_afterloop + if "!_TEST_PARAM!"=="-main" ( + call set CUSTOM_MAIN_CLASS=%%1 + shift + goto param_loop + ) - rem ignore arguments that do not start with '-' - if "%_TEST_PARAM:~0,1%"=="-" goto param_java_check - set _APP_ARGS=!_APP_ARGS! !_PARAM1! - shift - goto param_loop - - :param_java_check if "!_TEST_PARAM:~0,2!"=="-J" ( rem strip -J prefix set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM:~2! - shift goto param_loop ) @@ -160,22 +158,18 @@ rem Processes incoming arguments and places them in appropriate global variables for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO ( if not ["%%H"] == [""] ( set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1! - ) else if [%2] neq [] ( + ) else if [%1] neq [] ( rem it was a normal property: -Dprop=42 or -Drop="42" - call set _PARAM1=%%1=%%2 + call set _PARAM1=%%0=%%1 set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1! shift ) ) - ) else ( - if "!_TEST_PARAM!"=="-main" ( - call set CUSTOM_MAIN_CLASS=%%2 - shift - ) else ( - set _APP_ARGS=!_APP_ARGS! !_PARAM1! - ) + goto param_loop ) - shift + + set _APP_ARGS=!_APP_ARGS! !_PARAM1! + goto param_loop :param_afterloop From ccced7a9123d842e4ca1ff540d6b524d08dc9710 Mon Sep 17 00:00:00 2001 From: Derek Wickern Date: Fri, 29 Dec 2023 15:12:34 -0800 Subject: [PATCH 2/3] bat script: fix parsing of -J-XX: jvm args --- .../sbt/packager/archetypes/scripts/bat-template | 10 +++++++++- src/sbt-test/windows/test-bat-template/build.sbt | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template index e25165f24..5ba105e9d 100644 --- a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template +++ b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template @@ -149,7 +149,15 @@ rem Processes incoming arguments and places them in appropriate global variables if "!_TEST_PARAM:~0,2!"=="-J" ( rem strip -J prefix - set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM:~2! + call set _TEST_PARAM=!_TEST_PARAM:~2! + if not "!_TEST_PARAM:~0,5!" == "-XX:+" if not "!_TEST_PARAM:~0,5!" == "-XX:-" if "!_TEST_PARAM:~0,3!" == "-XX" ( + rem special handling for -J-XX since '=' gets parsed away + for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO ( + call set _TEST_PARAM=!_TEST_PARAM!=%%1 + shift + ) + ) + set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM! goto param_loop ) diff --git a/src/sbt-test/windows/test-bat-template/build.sbt b/src/sbt-test/windows/test-bat-template/build.sbt index b2395a059..a0634e67f 100644 --- a/src/sbt-test/windows/test-bat-template/build.sbt +++ b/src/sbt-test/windows/test-bat-template/build.sbt @@ -100,6 +100,12 @@ TaskKey[Unit]("checkScript") := { "arg #0 is [first]\narg #1 is [-XX]\narg #2 is [last]\nproperty(test.hoge) is [huga]\nvmarg #0 is [-Dtest.hoge=huga]\nvmarg #1 is [-Xms6m]\nSUCCESS!", Map("show-vmargs" -> "true") ) + checkOutput( + "with -J-XX java-opts", + Seq("-J-XX:+UseG1GC", "-J-XX:-UnlockExperimentalVMOptions", "-J-XX:MaxGCPauseMillis=500"), + "vmarg #0 is [-XX:+UseG1GC]\nvmarg #1 is [-XX:-UnlockExperimentalVMOptions]\nvmarg #2 is [-XX:MaxGCPauseMillis=500]\nSUCCESS!", + Map("show-vmargs" -> "true") + ) checkOutput( "include space", Seq("""-Dtest.hoge=C:\Program Files\Java""", """"C:\Program Files\Java""""), From a95ddac4fb6b25f68a050419e720e426726dfc24 Mon Sep 17 00:00:00 2001 From: Derek Wickern Date: Fri, 29 Dec 2023 15:38:17 -0800 Subject: [PATCH 3/3] bat script: add -java-home option Parse arguments before verifying the Java installation --- .../packager/archetypes/scripts/bat-template | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template index 5ba105e9d..dbf58125b 100644 --- a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template +++ b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/bat-template @@ -57,6 +57,11 @@ if defined BUNDLED_JVM ( if "%_JAVACMD%"=="" set _JAVACMD=java +rem if configuration files exist, prepend their contents to the script arguments so it can be processed by this runner +call :parse_config "%SCRIPT_CONF_FILE%" SCRIPT_CONF_ARGS + +call :process_args %SCRIPT_CONF_ARGS% %%* + rem Detect if this java is ok to use. for /F %%j in ('"%_JAVACMD%" -version 2^>^&1') do ( if %%~j==java set JAVAINSTALLED=1 @@ -86,11 +91,6 @@ if "%JAVAOK%"=="false" ( exit /B 1 ) -rem if configuration files exist, prepend their contents to the script arguments so it can be processed by this runner -call :parse_config "%SCRIPT_CONF_FILE%" SCRIPT_CONF_ARGS - -call :process_args %SCRIPT_CONF_ARGS% %%* - set _JAVA_OPTS=!_JAVA_OPTS! !_JAVA_PARAMS! if defined CUSTOM_MAIN_CLASS ( @@ -147,6 +147,13 @@ rem Processes incoming arguments and places them in appropriate global variables goto param_loop ) + if "!_TEST_PARAM!"=="-java-home" ( + set "JAVA_HOME=%~1" + set "_JAVACMD=%~1\bin\java.exe" + shift + goto param_loop + ) + if "!_TEST_PARAM:~0,2!"=="-J" ( rem strip -J prefix call set _TEST_PARAM=!_TEST_PARAM:~2!