简体   繁体   English

Sbt组装-单个项目中具有不同主类的多个jar

[英]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. 我正在尝试建造两个胖子罐,每个胖子罐都有自己的Main类。 Say jarOne.jar with MainClassOne and jarTwo.jar with MainClassTwo jarOne.jarMainClassOnejarTwo.jarMainClassTwo

Below are contents of my sbt file: 以下是我的sbt文件的内容:

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. 但是,我注意到这只会产生一个jar- jarTwo.jar而不是我最初希望的两个。 The idea is to be able to build jars(with different names and main classes) from the same project. 这个想法是要能够从同一项目构建jar(具有不同的名称和主类)。 Appreciate inputs on how one could achieve this using sbt. 赞赏有关如何使用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. 我正面临着同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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