9

Today I integrated sbt-native-packager into my scala project, mostly to generate handy execution scripts and/or packages.

Now, I added to my build.sbt line:

packageArchetype.java_application

and to my plugins.sbt

resolvers += "sbt-plugins" at "http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases"

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.7.0-RC2")

when I invoke sbt stage I get the target/universal/stage directory, but there is only lib there, no bin with scripts (which according to http://www.scala-sbt.org/sbt-native-packager/GettingStartedApplications/MyFirstProject.html should be created).

Do I need to add something else to get bin directory with scripts?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Andna
  • 6,539
  • 13
  • 71
  • 120
  • I came up to this question while looking how to deploy a Lagom app. The answer for deploying a Lagom app is here: https://stackoverflow.com/questions/27858758/scala-sbt-assembly-no-main-manifest-attribute/45368662#45368662 – Christian Vielma Jul 28 '17 at 08:35

2 Answers2

18

The issue was that in my project I had multiple main clases. In build.sbt I had:

Keys.mainClass in (Compile, run) := Some("Rest")

which should be

Keys.mainClass in (Compile) := Some("Rest")

and now it works perfectly.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Andna
  • 6,539
  • 13
  • 71
  • 120
  • 3
    That's a good one, thanks. I added the [jdom library](http://jdom.org/), which has main classes, to my project and got this mysterious breakage. A warning from `sbt-native-packager` would be a nice touch. – Adam Mackler Jul 25 '14 at 02:03
  • 1
    I had to add `Compile` scope for `mainClass` key in order to generate bin script in `packageArchetype.java_server` project – Stanislav Savulchik Aug 11 '14 at 02:37
  • I had this problem too; defined `mainClass in (Compile, run) := Some("foo")` instead of `mainClass in (Compile) := Some("foo")`. Is this standard expected SBT behavior, or does this look like a bug in sbt-native-packager? – metasim Jan 30 '15 at 20:56
  • That's an interesting question tbh - I did not investigate this issue further after I resolved it - did you experience the same issue with the newest version of sbt-native-packager? – Andna Feb 01 '15 at 10:51
  • Works for docker packaging too – Felipe Jul 04 '16 at 18:07
1

On a side note, changing the configuration of the mainClass affects in-sbt running of the application. In order to configure your build to both run the application in sbt (e.g. during development) as well as create packages, you will need 2 mainClass definitions (in build.sbt):

mainClass in Compile := Some("MyBootKernel")

mainClass in (Compile, run) := Some("MyApp")

Where the 2 main classes are:

class MyBootKernel extends Bootable {
  def startup = { MyApp.main(Array()) }
  def shutdown = {}
}

and

object MyApp extends App {
    // initialize application.
}

The start script in the bin directory passes the app main class to akka microkernel, which must extend Bootable (which then initializes the app), while running from sbt directly doesn't need boot stuff (i.e. just the App directly).

Brett
  • 5,690
  • 6
  • 36
  • 63