How to setup sbt-native-packager in a single modul

2019-06-26 06:59发布

问题:

I have a single module client-server project with a main for each.

I'm trying to use sbt-native-packager to generate start-script for both.

project/P.scala

object Tactic extends Build {

  lazy val root =
    (project in file(".")).
    configs(Client, Server)
    .settings( inConfig(Client)(Defaults.configTasks) : _*)
    .settings( inConfig(Server)(Defaults.configTasks) : _*)

  lazy val Client = config("client") extend Compile
  lazy val Server = config("server") extend Compile

}

build.sbt

mainClass in Client := Some("myProject.Client")

mainClass in Server := Some("myProject.Server")

enablePlugins(JavaAppPackaging)

When I run client:stage the directory target/universal/stage/lib is created with all the necessary jars but the bin directory is missing. What am I doing wrong?

Subsidiary question: what is the key to set the starting script name?

回答1:

I would recommend setting up your project as a multi-module build, instead of creating and using new configurations. I tried your multiple configuration route and it gets hairy very quickly.

For example (I created a shared project for anything shared between client & server):

def commonSettings(module: String) = Seq[Setting[_]](
  organization := "org.tactic",
          name := s"tactic-$module",
       version := "1.0-SNAPSHOT",
  scalaVersion := "2.11.6"
)

lazy val root = (project in file(".")
  settings(commonSettings("root"))
  dependsOn (shared, client, server)
  aggregate (shared, client, server)
)

val shared = (project
  settings(commonSettings("shared"))
)

val client = (project
  settings(commonSettings("client"))
  enablePlugins JavaAppPackaging
  dependsOn shared
)

val server = (project
  settings(commonSettings("server"))
  enablePlugins JavaAppPackaging
  dependsOn shared
)

Note I'm enabling sbt-native-packager's JavaAppPackaging in the client and server.

Then run stage.


Also, the key for the starting script name is executableScriptName.