简体   繁体   中英

Sbt assembly - Multiple jars with different Main classes from single project

I have a project with this structure:

--resources
--src
     |
     |-- mypackage.main
                    |
                    |-- MainClassOne.scala
                    |-- MainClassTwo.scala
     |-- mypackage.utils
                    |
                    |-- SomeOne.scala
                    |-- SomeTwo.scala

I am trying to build two fat jars, with each one having its own Main class. Say jarOne.jar with MainClassOne and jarTwo.jar with MainClassTwo

Below are contents of my sbt file:

lazy val assemblySettings = Seq(
  assemblyMergeStrategy in assembly := {
    case "application.conf"            => MergeStrategy.concat
    case "defaults.conf"            => MergeStrategy.concat
    case PathList("UnusedStubClass.class") => MergeStrategy.discard
    case x =>  val oldStrategy = (assemblyMergeStrategy in assembly).value
      oldStrategy(x)

  }
)

lazy val BuildOne = (project in file("."))
  .settings(mainClass in Compile := Some("mypackage.main.MainClassOne"))
  .settings(mainClass in assembly := Some("mypackage.main.MainClassOne"))
  .settings(assemblyJarName in assembly := "jarOne.jar")
  .settings(myMetaSettings: _*)
  .settings(myScalaSettings: _*)
  .settings(resolvers ++= myRepositories)
  .settings(libraryDependencies ++= myDependencies)
  .enablePlugins(AssemblyPlugin)
  .settings(assemblySettings: _*)

lazy val BuildTwo = (project in file("."))
  .settings(mainClass in Compile := Some("mypackage.main.MainClassTwo"))
  .settings(mainClass in assembly := Some("mypackage.main.MainClassTwo"))
  .settings(assemblyJarName in assembly := "jarTwo.jar")
  .settings(myMetaSettings: _*)
  .settings(myScalaSettings: _*)
  .settings(resolvers ++= myRepositories)
  .settings(libraryDependencies ++= myDependencies)
  .enablePlugins(AssemblyPlugin)
  .settings(assemblySettings: _*)

However, I note that this results in only one jar - jarTwo.jar - and not the two like I'd originally hoped. The idea is to be able to build jars(with different names and main classes) from the same project. Appreciate inputs on how one could achieve this using sbt.

It's just a random idea, so I might be wrong, but you can try

val AlternativeAssembly: Configuration = config("alt") extend Assembly describedAs "Alternative assembly config"

...

project
  .settings(Assembly / mainClass := Some("package.Main1"))
  .settings(AlternativeAssembly / mainClass := Some("package.Main2"))

which you would run:

project/assembly
project/alt:assembly

Any update on this issue? I'am facing the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM