4

I have my directory structure set up as such.

src/main/scala/main/Main.scala
src/main/scala/scripts/MainScript.scala

The script is a background job that will be running.

I've used sbt-assembly before to package up the main file into a jar to be deployed but I'm not sure how to create the two separate jars with sbt-assembly or sbt-native-packager. How would I go about doing that and what would be the best approach for this problem?

I would be looking to do something similar to this.

java -jar main.jar $PORT
java -jar scriptMain.jar
Petesta
  • 1,623
  • 3
  • 19
  • 29
  • Sbt native packager creates deployable artifacts for you. You can use Sbt assembly and Sbt native packager in tandem. We create rpm packages for spark where the fat jar created by Sbt assembly gets used. – Manas Apr 02 '16 at 00:14
  • @Manas Is it possible to code up a solution with `sbt-assembly` inside of `build.sbt` to create two separate JARs at the same time? I've used it before to create a single JAR. Because once I have those, I can create another file to do what's happening in the above code block. – Petesta Apr 02 '16 at 00:27
  • @Manas Should I be creating a multi-project build? So I would have the common code in `common`, MainScript in `script`, and Main in `main`. And build the JARs that way? – Petesta Apr 02 '16 at 01:08
  • 1
    Not sure how multi-project build will help you. I would still go with a `sbt-assembly` and use two scripts. One to call `main` and another to call `script`. Basically you will end up having same jar copied to two packages. – Manas Apr 02 '16 at 01:12

1 Answers1

1

One way to solve this only with native-packager would be the following.

  • Put all your main classes in src/main/scala
  • Define a mainClass in Compile := Some("foo.bar.Main") that should run as default
  • add additional scripts in src/universal/bin that you would like to provide. These scripts can call the main script generated by native-packager and set the -main parameter to the class you want to call.

Now you have an output package (e.g. a zip, rpm, deb) that has the following structure. Assuming your app is called myApp and you provided to other bin scripts called otherApp1 / otherApp2

lib/ (jars live here)
conf/ (configuration files here, if any)
bin/
  myApp
  otherApp1
  otherApp2

Unfortunately I have no example for the script (my bash-foo isn't good enough for instant magic on SO). In the end the scripts (otherApp1,otherApp2) should just pass the parameters they receive to the native-packager script (myApp).

There is an issue #633 that provides an automated way to generate scripts like this.

hope that helps, Muki

Muki
  • 3,513
  • 3
  • 27
  • 50