scala 对如何设置多项目 sbt 项目感到困惑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23188771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:12:49  来源:igfitidea点击:

Confused how to set up a multi-project sbt project

scalaplayframeworksbt

提问by Blankman

I'm using sbt .13 here.

我在这里使用 sbt .13。

I have this so far:

到目前为止我有这个:

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

  val appVersion = "1.0"

  resolvers += "local maven" at "/Users/blankman/.m2/repository/"

  val commonDependencies = Seq()
  val modelDependencies = Seq(
    "com.typesafe.slick" %% "slick" % "2.0.1",
    "org.slf4j" % "slf4j-nop" % "1.6.4"
  )

  val serviceDependencies = Seq(
    "com.typesafe.slick" %% "slick" % "2.0.1",
    "org.slf4j" % "slf4j-nop" % "1.6.4"
  )

  val webDependencies = Seq(
    //"org.apache.tomcat" % "tomcat-jdbc" % "8.0.3",
    "mysql" % "mysql-connector-java" % "5.1.30",
    "com.typesafe.slick" %% "slick" % "2.0.1"
  )


  lazy val common = Project(
    id = "app-common",
    base = file("app-common"),
    dependencies = commonDependencies
  )

  lazy val models = Project(
    id = "app-models",
    base = file("app-models"),
    settings(modelDependencies: _*)
    )
  ).dependsOn(common)

  lazy val services = Project(
    id = "app-services",
    base = file("app-services"),
    settings = Seq(
      libraryDependencies ++= serviceDependencies
    )
  ).dependsOn(models, common)


  lazy val web = play.Project("app-web", appVersion, webDependencies,
                        path = file("app-web"))
                      .settings(playScalaSettings: _*)
                      .dependsOn(services)

}

This doesn't work. For example, if I go into:

这不起作用。例如,如果我进入:

project app-models

项目应用模型

and try and compile, it says compile isn't valid or something.

并尝试编译,它说编译无效或什么的。

I'm really confused how to set up a project. What is the correct way?

我真的很困惑如何建立一个项目。正确的方法是什么?

Looking at this slide #10 here http://jsuereth.com/scala/2013/06/11/effective-sbt.htmlit says I can do:

在这里查看这张幻灯片 #10 http://jsuereth.com/scala/2013/06/11/effective-sbt.html它说我可以做到:

lazy val web = (
  Project("app-models", file("app-models"))
  settings(
     libraryDependencies += modelDependencies
  )
)

But when I do this I get an error also.

但是当我这样做时,我也会出错。

I basically have a few projects inside of sbt:

我基本上在 sbt 里面有几个项目:

common
models
services
web (which is play)
  • models depends on commons
  • services depends on commons + models
  • web depends on services
  • 模型取决于公地
  • 服务取决于公地+模型
  • 网络依赖于服务

Can someone help me get this to work?

有人可以帮我解决这个问题吗?

回答by Eugene Yokota

There are a few issues I found in your build definition, but since you bought up Josh's Effective sbttalk, I think we should go whole hog on the style.

我在您的构建定义中发现了一些问题,但是既然您购买了 Josh 的Effective sbt谈话,我认为我们应该全力以赴。

Effective sbt

有效的sbt

Here are the files.

这是文件。

project/build.properties

项目/build.properties

sbt.version=0.13.2

project/play.sbt

项目/play.sbt

val playVersion = "2.2.2"

resolvers += Resolver.typesafeRepo("releases")

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % playVersion) 

project/commons.scala

项目/commons.scala

import sbt._
import Keys._

object Commons {
  val appVersion = "1.0"

  val settings: Seq[Def.Setting[_]] = Seq(
    version := appVersion,
    resolvers += Opts.resolver.mavenLocalFile
  )
}

project/dependencies.scala

项目/dependencies.scala

import sbt._
import Keys._

object Dependencies {
  val slickVersion = "2.0.1"
  val slick = "com.typesafe.slick" %% "slick" % slickVersion
  val slf4jVersion = "1.6.4"
  val slf4jNop = "org.slf4j" % "slf4j-nop" % slf4jVersion
  val mysqlConnectorVersion = "5.1.30"
  val mysqlConnector = "mysql" % "mysql-connector-java" % mysqlConnectorVersion

  val commonDependencies: Seq[ModuleID] = Seq(
    slick,
    slf4jNop
  )
  val modelDependencies: Seq[ModuleID] = Seq()
  val serviceDependencies: Seq[ModuleID] = Seq()
  val webDependencies: Seq[ModuleID] = Seq(
    mysqlConnector
  )
}

build.sbt

生成.sbt

import play.Project._
import Dependencies._

lazy val appCommon = (project in file("app-common")).
  settings(Commons.settings: _*).
  settings(
    libraryDependencies ++= commonDependencies
  )

lazy val appModels = (project in file("app-models")).
  settings(Commons.settings: _*).
  settings(
    libraryDependencies ++= modelDependencies
  ).
  dependsOn(appCommon)

lazy val appServices = (project in file("app-services")).
  settings(Commons.settings: _*).
  settings(
    libraryDependencies ++= serviceDependencies
  ).
  dependsOn(appModels, appCommon)

lazy val appWeb = (project in file("app-web")).
  settings(Commons.settings: _*).
  settings(playScalaSettings: _*).
  dependsOn(appServices)

notes

笔记

settings parameter vs settings method

设置参数与设置方法

For modelsand services, you're passing in settings sequence into Project(...)constructor, so the default settings are likely not loaded. You can pass in the default settings manually, or use settings(...)method on Project, which I would recommend.

对于modelsand services,您将设置序列传递给Project(...)构造函数,因此可能不会加载默认设置。您可以手动传入默认设置,或者使用我推荐的settings(...)方法 on Project

lazy val appModels = (project in file("app-models")).
  settings(
    libraryDependencies ++= modelDependencies
  ).
  dependsOn(appCommon)

Josh uses postfix notation using parenthesis, but I prefer using dot notation for this, so that's a slight deviation from the talk.

Josh 使用带括号的后缀表示法,但我更喜欢为此使用点表示法,因此这与谈话略有不同。

libraryDependencies ++=

库依赖++=

As the above example, you have to pass modelDependenciesto libraryDependencies. You had it calling directly into settings.

如上面的例子,你必须传递modelDependencieslibraryDependencies. 你让它直接调用到settings.

resolvers

解析器

The resolverssetting is not passed into anything, which likely is not correct.

resolvers设置不会传递给任何东西,这可能是不正确的。