Skip to content

Commit 3776c5e

Browse files
committed
FIX #82 adding the ability to specify jvm options via sbt
1 parent aa85388 commit 3776c5e

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

src/main/scala/com/typesafe/sbt/packager/archetypes/JavaServerApplication.scala

+20-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package packager
33
package archetypes
44

55
import sbt._
6-
import sbt.Keys.{ target, mainClass, sourceDirectory, streams }
6+
import sbt.Keys.{ target, mainClass, sourceDirectory, streams, javaOptions, run }
77
import SbtNativePackager.{ Debian, Rpm, Universal }
88
import packager.Keys.{ packageName }
99
import linux.{ LinuxFileMetaData, LinuxPackageMapping, LinuxSymlink, LinuxPlugin }
@@ -46,6 +46,7 @@ object JavaServerAppPackaging extends AutoPlugin {
4646
* - config directory
4747
*/
4848
def linuxSettings: Seq[Setting[_]] = Seq(
49+
javaOptions in Linux := Nil,
4950
// === logging directory mapping ===
5051
linuxPackageMappings <+= (packageName in Linux, defaultLinuxLogsLocation, daemonUser in Linux, daemonGroup in Linux) map {
5152
(name, logsDir, user, group) => packageTemplateMapping(logsDir + "/" + name)() withUser user withGroup group withPerms "755"
@@ -60,7 +61,7 @@ object JavaServerAppPackaging extends AutoPlugin {
6061
if (overrideScript.exists) overrideScript.toURI.toURL
6162
else etcDefaultTemplateSource
6263
},
63-
makeEtcDefault <<= (packageName in Linux, target in Universal, linuxEtcDefaultTemplate, linuxScriptReplacements)
64+
makeEtcDefault <<= (packageName in Linux, target in Universal, linuxEtcDefaultTemplate, linuxScriptReplacements, javaOptions in Linux)
6465
map makeEtcDefaultScript,
6566
linuxPackageMappings <++= (makeEtcDefault, bashScriptConfigLocation) map { (conf, configLocation) =>
6667
configLocation.flatMap { path =>
@@ -259,10 +260,26 @@ object JavaServerAppPackaging extends AutoPlugin {
259260
Some(script)
260261
}
261262

262-
protected def makeEtcDefaultScript(name: String, tmpDir: File, source: java.net.URL, replacements: Seq[(String, String)]): Option[File] = {
263+
/**
264+
* Creates the etc-default file, which will contain the basic configuration
265+
* for an app.
266+
*
267+
* @param name of the etc-default config file
268+
* @param tmpDir to store the resulting file in (e.g. target in Universal)
269+
* @param source of etc-default script
270+
* @param replacements for placeholders in etc-default script
271+
* @param javaOptions that get appended to the etc-default script
272+
*
273+
* @return Some(file: File)
274+
*/
275+
protected def makeEtcDefaultScript(name: String, tmpDir: File, source: java.net.URL,
276+
replacements: Seq[(String, String)], javaOptions: Seq[String]): Option[File] = {
263277
val scriptBits = TemplateWriter.generateScript(source, replacements)
264278
val script = tmpDir / "tmp" / "etc" / "default" / name
265279
IO.write(script, scriptBits)
280+
if (javaOptions.nonEmpty) {
281+
IO.writeLines(script, "# java options from build" +: javaOptions, append = true)
282+
}
266283
Some(script)
267284
}
268285

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
enablePlugins(JavaServerAppPackaging)
2+
3+
name := "simple-test"
4+
5+
version := "0.1.0"
6+
7+
javaOptions in Linux ++= Seq(
8+
"-J-Xmx64m", "-J-Xms64m", "-Dproperty=true"
9+
)
10+
11+
TaskKey[Unit]("check") := {
12+
val etc = (target in Universal).value / "tmp" / "etc" / "default" / name.value
13+
val content = IO.read(etc)
14+
val options = (javaOptions in Linux).value
15+
options.foreach { opt =>
16+
assert(content.contains(opt), "Option [" + opt + "] is missing")
17+
}
18+
}
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,3 @@
1+
object Main extends App {
2+
println("Hello world")
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Run the zip packaging.
2+
> show makeEtcDefault
3+
$ exists target/universal/tmp/etc/default/simple-test
4+
> check

src/sphinx/archetypes/java_server/customize.rst

+14-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ As this file is OS dependend, each OS gets section.
1818
Linux Configuration
1919
-------------------
2020

21-
Create ``src/templates/etc-default`` with the following template
21+
You have two options. First, you can specify your options via the ``build.sbt``.
22+
23+
.. code-block :: scala
24+
25+
javaOptions in Linux ++= Seq(
26+
"-J-Xmx64m", "-J-Xms64m", "-Dproperty=true",
27+
)
28+
29+
For the ``-X`` settings you need to add a suffix ``-J`` so the start script will
30+
recognize these as vm config parameters.
31+
32+
The other option is to create ``src/templates/etc-default`` with the following template
2233

2334
.. code-block :: bash
2435
@@ -59,7 +70,8 @@ Create ``src/templates/etc-default`` with the following template
5970
# -d -- -d
6071
6172
The file will be installed to ``/etc/default/<normalizedName>`` and read from there
62-
by the startscript.
73+
by the startscript. You can use ``#`` for comments and new lines as you like. The
74+
available are listed in the template or you can display them via ``show linuxScriptReplacements``.
6375

6476
Environment variables
6577
~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)