4

I'm looking to create jars for AWS Lambda to run job tasks. Currently my build.sbt file looks something like this:

lazy val commonSettings = Seq(...)

lazy val core = project
    .settings(commonSettings: _*)

lazy val job = project
    .settings(commonSettings: _*)
    .dependsOn(core)

lazy val service = project
    .settings(commonSettings: _*)
    .settings(
        mainClass in assembly := Some("io.example.service.Lambda"),
        assemblyJarName in assembly := "lambda.jar"
    )
    .dependsOn(core)

Running sbt assembly assembles the service module into a jar for my API and that works fine. The module job however will have multiple Main classes (one pr. job) and when I run sbt assembly job the service module is also assembled (even through its not depended on).

How can I configure my setup to only assemble the job module when needed, and specify individual mainClasses as separately assembled jars?

Andreas Jarbol
  • 745
  • 2
  • 11
  • 27

2 Answers2

4

Set mainClass in assembly in job to define which main class to use, and run job/assembly to just assemble the job assembly jar.

Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55
  • Thank you for your quick response. That works for only assembling the job module how could I configure sbt to use multiple mains generating a jar each? – Andreas Jarbol Jan 16 '18 at 16:13
  • Easiest way would to create multiple projects that depend on `job`, and then either each containing their own main (no config necessary) or each configuring their main - which is defined the `job` project. – Dale Wijnand Jan 17 '18 at 09:57
  • @DaleWijnand is there and example of this somewhere? – CpILL Feb 13 '19 at 07:18
  • @CpILL here's one I found on GitHub: https://github.com/non/zillionaire/blob/95327d8b5f8adfd55d17d10d5cfa0a446b2b8159/build.sbt#L7 – Dale Wijnand Feb 13 '19 at 11:11
0

You will need to override the default main class at build time by setting the property explicitly.

sbt "; set mainClass in assembly := Some("another/class"); job/assembly"

Not sure it's good practice but alternatively you can define a sub-project for each job with the correct main class set.

lazy val job1 = project
    .settings(commonSettings: _*)
    .settings(
        mainClass in assembly := Some("io.example.service.Lambda"),
        assemblyJarName in assembly := "lambda.jar"
    )
    .dependsOn(core)

lazy val job2 = project
    .settings(commonSettings: _*)
    .settings(
        mainClass in assembly := Some("io.example.service.Lambda2"),
        assemblyJarName in assembly := "lambda2.jar"
    )
    .dependsOn(core)
Shane Perry
  • 980
  • 7
  • 12