Skip to content
Simeon H.K. Fitch edited this page Jul 31, 2015 · 5 revisions

This collection contains small snippets picked up in the gitter chat / stackoverflow and twitter.

Compile settings for packageBin

Question

Is it possible to use a custom compiler setting when packaging with universal:packageBin? When I run universal:packageBin I would like to first compile with -Xdisable-assertions before packaging so that I can use assertions more liberally during production and testing. Is this possible?

Solution

commands += Command("betterPackage") { state: State => 
   "clean" :: "set scalacOption in Compile += \"-Xdisable-assertions\"" :: 
   "universal:packageBin" :: "session clear" :: state 
}

Deploy multiple launchers for one project

I have a single project with multiple main classes. Is it possible to create separate start scripts or packages for one project?

As of version 1.0.3 this isn't precisely possible with a single project module, but with multiple sub-modules you can generate separate (independent) packages fairly easily.

Assuming your base project is, say, cat-finder, and you want to create two packages, one for cat-finder-cli and cat-finder-gui, you can use something like the following in (in a packaging subdirectory), under which you'd have the build definitions for the individual packages (e.g. cat-finder-gui and cat-finder-cli).

addCommandAlias("package", "collectPackages")

enablePlugins(JavaAppPackaging)

// TODO: Need to figure out how to look this up from the outer project
version in ThisBuild := "0.0.1-SNAPSHOT"

val baseSettings = Seq(
  publishArtifact in (Compile, packageDoc) := false,
  publishArtifact in packageDoc := false,
  sources in (Compile,doc) := Seq.empty
  )

// Due to the way that SBT works, subprojects have to be defined manually as below.
// IOW, don't try to refactor :-)
lazy val packaging = project
  .in(file("."))
  .aggregate(a, b)

lazy val catFinder = ProjectRef(file(".."), "cat-finder")

lazy val catFinderCli = Project(id = "cat-finder-cli", base = file("cat-finder-cli"))
  .dependsOn(catFinder)
  .settings(baseSettings: _*)

lazy val catFinderGui = Project(id = "cat-finder-gui", base = file("cat-finder-gui"))
  .dependsOn(catFinder)
  .settings(baseSettings: _*)

val packageSubs = taskKey[Seq[File]]("Build packages in subrojects")
(packageSubs in packaging) := Seq(
  (packageBin.in(catFinderCli, Universal)).value,
  (packageBin.in(catFinderGui, JDKPackager)).value
)

// Collect all the generated packages in one place.
val collectPackages = taskKey[File]("Move built packages")
collectPackages := {
  val dest = target.value / "packages"
  dest.mkdirs()
  val files = packageSubs.value
  val dests = files.map { f 
    val d = dest / f.getName
    IO.copyFile(f, d)
    d
  }
  streams.value.log.info(s"Collected packages in $dest:\n${dests.map(_.getName).mkString("  ","\n  ", "")}")
  dest
}
Clone this wiki locally