如何sbt发布多模块项目

How to sbt publish multi module project

我是 sbt/scala 的初学者。我有一个用 sbt assembly 构建的多模块 sbt 项目,它创建我的模块的 jar 文件。当我尝试 sbt module2/publish 时,我注意到与 sbt-assembly 创建的 jar 文件相比,我目录中的输出 jar 文件非常小,当我尝试 运行:

时它会抛出错误
Error: Unable to initialize main class 
com.example.module2.service.Server
Caused by: java.lang.NoClassDefFoundError: scala/Function0

我需要在 build.sbt 文件中包含什么才能发布 sbt?

my-proj
├── Build.scala
├── common
│   ├── build.sbt
│   └── src
├── module1
│   ├── build.sbt
│   └── src
├── module2
│   ├── build.sbt
│   └── src
└── project
    ├── build.properties
    └── plugins.sbt

Build.scala

lazy val my_proj = (project in file("."))
  .aggregate(common, module1, module2)

lazy val common = project

lazy val common = (project in file("commons"))

lazy val module1 = (project in file("module1"))
  .dependsOn(common, module2)

lazy val module1 = (project in file("module1"))
  .dependsOn(common)

module1/build.sbt

name := "module1"
version := "0.1"
organization := "com.example"
scalaVersion := "2.11.12"
val akkaVersion = "2.5.16"

scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8")
fork := true

/** Dependencies */
resolvers ++= Seq("Akka Repository" at "http://repo.akka.io/releases/",
  "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
)

publishTo := Some(Resolver.file("file", new File("/tmp/my/artifactory")))

libraryDependencies ++= Seq(
  "org.scala-lang" % "scala-library"    % scalaVersion
  , "org.scala-lang" % "scala-compiler" % scalaVersion
  , "org.scala-lang" % "scala-reflect"  % scalaVersion
  , "com.typesafe.akka" %% "akka-actor" % akkaVersion
  
)
assemblyMergeStrategy in assembly := {
  case "META-INF\io.netty.versions.properties"             => MergeStrategy.first
  case m if m.toLowerCase.endsWith("manifest.mf")           => MergeStrategy.discard
  case m if m.toLowerCase.matches("meta-inf.*\.sf$")       => MergeStrategy.discard
  case m if m.toLowerCase.startsWith("meta-inf/services/")  => MergeStrategy.filterDistinctLines
  case "reference.conf"                                     => MergeStrategy.concat
  case _                                                    => MergeStrategy.first
}

要将“胖”jar 发布到存储库,您必须将以下内容添加到要发布的每个 sub-project 的 build.sbt

Compile / assembly / artifact ~= { art =>
  art.withClassifier(Some("fat"))
}

addArtifact(Compile / assembly / artifact, assembly).settings