如何在Scala中引用本地项目

时间:2018-05-06 22:00:58

标签: scala sbt

首先,如果这个问题已经得到解答,或者如果有关于它的完整文档/教程,我很抱歉,我是Scala新手,我肯定错过了一些东西。

我有一个C#背景,我正在努力扩展我的学习Scala的知识。我想在微服务架构中包含一个Scala微服务,它实际上是使用.NET项目构建的。为了在.NET项目中实现共享架构代码,我开发了一系列基础项目,由所有微服务项目引用,并且使用VS进行此操作非常简单,但我没有掌握如何在Scala上实现相同的功能

特别是,我想了解是否有一种方法可以引用位于项目B中的本地文件夹中的项目A,其方式类似于从Maven引用项目,例如:

libraryDependencies ++= Seq(
    "com.typesafe.akka" %% "akka-http-spray-json"   % "10.1.1",
    "com.typesafe.akka" %% "akka-http"   % "10.1.1",
    "com.typesafe.akka" %% "akka-stream" % "2.5.11"
)

最好的方法是什么(例如,参考罐子)。

1 个答案:

答案 0 :(得分:1)

是的,您可以使用simple build tool引用scala中的另一个scala项目。

假设你的架构有

1) ChatMicroserviceApi.jar (depends on two local jars 2 and 3)
2) ChatMicroserviceSchema.jar
3) ChatMicroserviceExternalDeps.jar

然后你的sbt脚本如下所示。我使用sbt-assembly来创建您必须在addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")

中添加project/plugins.sbt的胖罐子
//build.sbt

name := "ChatMicroserviceParent"
organization in ThisBuild := "com.microservice.chat"
scalaVersion in ThisBuild := "2.12.5"

version := "1.0-SNAPSHOT"

//define what parent consists of in aggregate section
lazy val ChatMicroserviceParent = project
  .in(file("."))
  .settings(settings)
  .aggregate(ChatMicroserviceSchema,
             ChatMicroserviceExternalDeps,
             ChatMicroserviceApi)

//creates schema.jar
lazy val ChatMicroserviceSchema =
  project.settings(name := "ChatMicroserviceSchema",
                   publishMavenStyle := true,
                   settings,
                   apiSchemaAssemblySettings)

//creates external-deps.jar
lazy val ChatMicroserviceExternalDeps =
  project.settings(
    name := "ChatMicroserviceExternalDeps",
    publishMavenStyle := true,
    settings,
    apiSchemaAssemblySettings,
    libraryDependencies ++= Seq(
      "com.softwaremill.sttp" %% "core" % "1.1.12",
      "com.softwaremill.sttp" %% "async-http-client-backend-future" % "1.1.12"
        exclude ("org.asynchttpclient", "async-http-client"),
      "org.asynchttpclient" % "async-http-client" % "2.4.4"
        excludeAll( ExclusionRule(organization = "io.netty") ),
      "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.5",
      "com.fasterxml.jackson.module" % "jackson-modules-java8" % "2.9.5",
      "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.9.5",
      "com.fasterxml.jackson.datatype" % "jackson-datatype-jdk8" % "2.9.5"
    )
  )

val NettyVersion = "4.1.16.Final"

//creates api.jar which depends on schema.jar and external-deps.jar
lazy val ChatMicroserverApi = project
  .settings(
    name := "ChatMicroserverApi",
    settings,
    apiAssemblySettings,
    libraryDependencies ++= Seq(
      "com.github.finagle" %% "finch-core" % "0.18.0"
        excludeAll( ExclusionRule(organization = "io.netty") )
        exclude ("com.fasterxml.jackson.core", "jackson-databind"),
      "com.github.finagle" %% "finch-circe" % "0.18.0",
      "io.netty" % "netty-codec" % NettyVersion,
      "io.netty" % "netty-codec-http" % NettyVersion,
      "io.netty" % "netty-codec-http2" % NettyVersion,
      "io.netty" % "netty-transport" % NettyVersion,
      "io.netty" % "netty-buffer" % NettyVersion,
      "io.netty" % "netty-common" % NettyVersion,
      "io.netty" % "netty-resolver" % NettyVersion,
      "io.netty" % "netty-handler" % NettyVersion,
      "io.netty" % "netty-handler-proxy" % NettyVersion,
      "io.netty" % "netty-transport-native-unix-common" % NettyVersion,
      "io.netty" % "netty-transport-native-epoll" % NettyVersion,
      "io.netty" % "netty-tcnative-boringssl-static" % "2.0.6.Final",
      "io.netty" % "netty-codec-socks" % NettyVersion,
      "io.circe" %% "circe-generic" % "0.9.3",
      "com.typesafe" % "config" % "1.3.3",
      "ch.qos.logback" % "logback-classic" % "1.2.3",
      "com.typesafe.akka" %% "akka-actor" % "2.5.12",
      "com.typesafe.akka" %% "akka-stream" % "2.5.12",
      "com.typesafe.akka" %% "akka-http" % "10.1.1",
      "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.5",
      "com.fasterxml.jackson.module" % "jackson-modules-java8" % "2.9.5",
      "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.9.5",
      "com.fasterxml.jackson.datatype" % "jackson-datatype-jdk8" % "2.9.5",
      "io.circe" %% "circe-java8" % "0.9.3",
      "com.softwaremill.sttp" %% "core" % "1.1.12",
      "org.asynchttpclient" % "async-http-client" % "2.4.4"
        excludeAll( ExclusionRule(organization = "io.netty") ),
      "com.softwaremill.sttp" %% "async-http-client-backend-future" % "1.1.12"
        exclude ("org.asynchttpclient", "async-http-client"),
      "joda-time" % "joda-time" % "2.9.9"
    )
  )
  .dependsOn(ChatMicroserviceExternalDeps, ChatMicroserviceSchema)

lazy val commonDependencies = Seq()

lazy val settings =
  commonSettings

lazy val compilerOptions = Seq(
  "-unchecked",
  "-feature",
  "-language:existentials",
  "-language:higherKinds",
  "-language:implicitConversions",
  "-language:postfixOps",
  "-deprecation",
  "-encoding",
  "utf8"
)

lazy val commonSettings = Seq(
  scalacOptions ++= compilerOptions,
  resolvers ++= Seq(
    Resolver.sonatypeRepo("releases"),
    Resolver.sonatypeRepo("snapshots")
  )
)


lazy val apiAssemblySettings = Seq(
  assemblyJarName in assembly := name.value + "-" + version.value + ".jar",
  mainClass in assembly := Some(
    "com.microservice.MainServerClass"),
  assemblyMergeStrategy in assembly := {
    case PathList("reference.conf")    => MergeStrategy.concat
    case PathList("META-INF", xs @ _*) => MergeStrategy.discard
    case _                             => MergeStrategy.first
  }
)

lazy val apiSchemaAssemblySettings = Seq(
  assemblyJarName in assembly := name.value + "-" + version.value + ".jar")
assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case _                             => MergeStrategy.first
}

lazy val apiExternalDepsAssemblySettings = Seq(
  assemblyJarName in assembly := name.value + "-" + version.value + ".jar")
assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case _                             => MergeStrategy.first
}

以下是可能有用的工作示例 - https://github.com/duwamish-os/chat-server