Skip to content

Commit 5b696b9

Browse files
authored
Define addJava in ash-template (#944)
* Define addJava in ash-template Previously, there was no addJava function defined in the ash-template. This resulted in warnings when the ash script ran because template declares expects the function to be defined. Now, the addJava function is defined. It adds options to java_opts. As a convenience, if JAVA_OPTS is set then it is added to java_opts. This makes the ash script behave more like the bash script. The result is then passed to java at the end of the script. * Add tests for ashscript and use extraBashScriptDefines
1 parent f5e2eb7 commit 5b696b9

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

src/main/resources/com/typesafe/sbt/packager/archetypes/scripts/ash-template

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ realpath () {
2727
)
2828
}
2929

30+
# Allow user and template_declares (see below) to add java options.
31+
addJava () {
32+
java_opts="$java_opts $1"
33+
}
34+
35+
# Allow user to specify java options. These get listed first per bash-template.
36+
if [ -n "$JAVA_OPTS" ]
37+
then
38+
addJava "$JAVA_OPTS"
39+
fi
40+
3041
# Loads a configuration file full of default command line options for this script.
3142
loadConfigFile() {
3243
cat "$1" | sed '/^\#/d' | sed 's/^-J-X/-X/' | tr '\r\n' ' '
@@ -44,4 +55,4 @@ ${{template_declares}}
4455
# If a configuration file exist, read the contents to $opts
4556
[ -f "$script_conf_file" ] && opts=$(loadConfigFile "$script_conf_file")
4657

47-
exec java -classpath $app_classpath $opts $app_mainclass $@
58+
exec java $java_opts -classpath $app_classpath $opts $app_mainclass $@

src/main/scala/com/typesafe/sbt/packager/archetypes/scripts/AshScriptPlugin.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ object AshScriptPlugin extends AutoPlugin {
7777

7878
override def projectSettings = Seq(
7979
bashScriptTemplateLocation := (sourceDirectory.value / "templates" / ashTemplate),
80-
bashScriptDefines := Defines((scriptClasspath in bashScriptDefines).value, bashScriptConfigLocation.value)
80+
bashScriptDefines := Defines((scriptClasspath in bashScriptDefines).value, bashScriptConfigLocation.value),
81+
bashScriptDefines ++= bashScriptExtraDefines.value
8182
)
8283

8384
/**
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
enablePlugins(JavaAppPackaging, AshScriptPlugin)
2+
3+
name := "simple-app"
4+
5+
version := "0.1.0"
6+
7+
bashScriptExtraDefines ++= Seq(
8+
"""addJava "-Xms64m"""",
9+
"""addJava "-Xmx64m""""
10+
)
11+
12+
TaskKey[Unit]("script-check") := {
13+
val startScript = (stagingDirectory in Universal).value / "bin" / executableScriptName.value
14+
val options = IO.read(startScript)
15+
assert(options contains """addJava "-Xms64m"""", "Script doesn't contain xmx setting:\n" + options)
16+
assert(options contains """addJava "-Xmx64m"""", "Script doesn't contain xms setting:\n" + options)
17+
}
18+
19+
TaskKey[Unit]("run-check") := {
20+
val cwd = (stagingDirectory in Universal).value
21+
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath)
22+
val memory = (Process(cmd, cwd).!!).replaceAll("\n", "")
23+
assert(memory.toLong <= 64, "Runtime memory is bigger then 64m < " + memory + "m")
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object MainApp extends App {
2+
val memory = Runtime.getRuntime.maxMemory() / (1024L * 1024L)
3+
print(memory)
4+
}

src/sbt-test/ash/memory-settings/test

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Run the staging and check the script.
2+
> stage
3+
$ exists target/universal/stage/bin/simple-app
4+
> script-check
5+
> run-check

0 commit comments

Comments
 (0)