22

What is the best way to set the target package for an SBT project to be an executable jar file?

It would need to bundle scala-library.jar and set the manifest for main-method.

Synesso
  • 37,610
  • 35
  • 136
  • 207
  • 1
    Have a look at https://github.com/retronym/sbt-onejar. I haven't tried myself but I'm interested to know if that works. Also if that does not work, retronym also points to https://github.com/nuttycom/sbt-proguard-plugin. – huynhjl May 03 '11 at 01:11
  • Thanks huynhjl. It worked fine, but I decided against it as one-jar itself seems full of classloading jiggery-pokery. I prefer the mega-jar approach of assembly-sbt. – Synesso May 03 '11 at 02:34

1 Answers1

35

Use the assembly-sbt plugin (originally created by Coda Hale, now maintained by Eugene Yokota): https://github.com/sbt/sbt-assembly

Once you add this to your project it will build a so-called "fatjar" which is executable via java -jar projectName-assembly.jar. It will autodetect your main method -- if there is more than one in your source, you can explicitly set which one to use by setting mainclass, e.g.:

mainClass in assembly := Some("com.package.ClassNameWithMain")

Note: I edited this answer to be up-to-date with the current release of SBT (0.11+).

Community
  • 1
  • 1
Thomas Lockney
  • 2,567
  • 20
  • 13
  • Thanks Thomas. It's a good one. Simple to use. Would benefit from having proguard trim out the fat. – Synesso May 03 '11 at 02:33
  • You can use the Proguard plugin to do that. Make sure you specify that you want to keep the main class. – Philippe Jan 05 '12 at 16:14
  • 1
    Yes, Proguard can do this, but it's a pain to configure correctly. The beauty of the sbt-assembly is how incredibly simple it is. – Thomas Lockney Jan 06 '12 at 04:42
  • @folone I tried this using assembly and generated jar but still it won't execute using double click. How do I create executable jar which will run when you click on it? – Nishan Dec 01 '16 at 16:45