From 905d425da9f5b388e3af003f325c1bdcfacb28de Mon Sep 17 00:00:00 2001 From: Aivaras Voveris Date: Thu, 29 Aug 2019 08:46:17 +0300 Subject: [PATCH] Added command line JAVA_OPTS support for AshScriptPlugin (#1255) --- build.sbt | 2 +- .../packager/archetypes/scripts/ash-template | 34 +++++++++++++++++++ .../ash/command-line-settings/build.sbt | 14 ++++++++ .../command-line-settings/project/plugins.sbt | 1 + .../src/main/scala/MainApp.scala | 3 ++ src/sbt-test/ash/command-line-settings/test | 4 +++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/sbt-test/ash/command-line-settings/build.sbt create mode 100644 src/sbt-test/ash/command-line-settings/project/plugins.sbt create mode 100644 src/sbt-test/ash/command-line-settings/src/main/scala/MainApp.scala create mode 100644 src/sbt-test/ash/command-line-settings/test diff --git a/build.sbt b/build.sbt index 81ec5588e..16be7f13d 100644 --- a/build.sbt +++ b/build.sbt @@ -115,7 +115,7 @@ addCommandAlias("validate", "; clean ; update ; validateFormatting ; test ; mima // List all scripted test separately to schedule them in different travis-ci jobs. // Travis-CI has hard timeouts for jobs, so we run them in smaller jobs as the scripted // tests take quite some time to run. -// Ultimatley we should run only those tests that are necessary for a change +// Ultimately we should run only those tests that are necessary for a change addCommandAlias("validateUniversal", "scripted universal/*") addCommandAlias("validateJar", "scripted jar/*") addCommandAlias("validateBash", "scripted bash/*") diff --git a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template index 4fcd23519..450443870 100644 --- a/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template +++ b/src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template @@ -55,6 +55,38 @@ get_java_cmd() { fi } +# Processes incoming arguments and places them in appropriate global variables. called by the run method. +process_args () { + local no_more_snp_opts=0 + while [[ $# -gt 0 ]]; do + case "$1" in + --) shift && no_more_snp_opts=1 && break ;; + -h|-help) usage; exit 1 ;; + -v|-verbose) verbose=1 && shift ;; + -d|-debug) debug=1 && shift ;; + + -no-version-check) no_version_check=1 && shift ;; + + -mem) echo "!! WARNING !! -mem option is ignored. Please use -J-Xmx and -J-Xms" && shift 2 ;; + -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; + + -main) custom_mainclass="$2" && shift 2 ;; + + -java-home) require_arg path "$1" "$2" && jre=`eval echo $2` && java_cmd="$jre/bin/java" && shift 2 ;; + + -D*|-agentlib*|-XX*) addJava "$1" && shift ;; + -J*) addJava "${1:2}" && shift ;; + *) addResidual "$1" && shift ;; + esac + done + + if [[ no_more_snp_opts ]]; then + while [[ $# -gt 0 ]]; do + addResidual "$1" && shift + done + fi +} + real_script_path="$(realpath "$0")" app_home="$(realpath "$(dirname "$real_script_path")")" lib_dir="$(realpath "${app_home}/../lib")" @@ -63,6 +95,8 @@ app_mainclass=${{app_mainclass}} ${{template_declares}} +process_args "$@" + java_cmd="$(get_java_cmd)" # If a configuration file exist, read the contents to $opts diff --git a/src/sbt-test/ash/command-line-settings/build.sbt b/src/sbt-test/ash/command-line-settings/build.sbt new file mode 100644 index 000000000..22248ee23 --- /dev/null +++ b/src/sbt-test/ash/command-line-settings/build.sbt @@ -0,0 +1,14 @@ +enablePlugins(JavaAppPackaging, AshScriptPlugin) + +name := "command-line-app" + +version := "0.1.0-SNAPSHOT" + +TaskKey[Unit]("runCheck") := { + val configArg = "-Dconfig.resource=/config.conf" + val cwd = (stagingDirectory in Universal).value + val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, configArg) + + val output = (sys.process.Process(cmd, cwd).!!).replaceAll("\n", "") + assert(output.contains(configArg), s"Application did not receive command line configuration resource $configArg") +} diff --git a/src/sbt-test/ash/command-line-settings/project/plugins.sbt b/src/sbt-test/ash/command-line-settings/project/plugins.sbt new file mode 100644 index 000000000..b53de154c --- /dev/null +++ b/src/sbt-test/ash/command-line-settings/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version")) diff --git a/src/sbt-test/ash/command-line-settings/src/main/scala/MainApp.scala b/src/sbt-test/ash/command-line-settings/src/main/scala/MainApp.scala new file mode 100644 index 000000000..52e6c7d8a --- /dev/null +++ b/src/sbt-test/ash/command-line-settings/src/main/scala/MainApp.scala @@ -0,0 +1,3 @@ +object MainApp extends App { + println(args.mkString("|")) +} diff --git a/src/sbt-test/ash/command-line-settings/test b/src/sbt-test/ash/command-line-settings/test new file mode 100644 index 000000000..c35f92d69 --- /dev/null +++ b/src/sbt-test/ash/command-line-settings/test @@ -0,0 +1,4 @@ +# Run the staging and check the script. +> stage +$ exists target/universal/stage/bin/command-line-app +> runCheck \ No newline at end of file