简体   繁体   English

困惑如何建立一个多项目的SBT项目

[英]Confused how to set up a multi-project sbt project

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.html it says I can do: http://jsuereth.com/scala/2013/06/11/effective-sbt.html上查看此幻灯片#10,它说我可以做:

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? 有人可以帮我这个忙吗?

There are a few issues I found in your build definition, but since you bought up Josh's Effective sbt talk, 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 项目/播放

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 build.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 models and services , you're passing in settings sequence into Project(...) constructor, so the default settings are likely not loaded. 对于modelsservices ,您要将设置序列传递到Project(...)构造函数中,因此可能不会加载默认设置。 You can pass in the default settings manually, or use settings(...) method on Project , which I would recommend. 您可以手动传入默认设置,也可以在Project上使用settings(...)方法,这是我推荐的方法。

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 ++= libraryDependencies ++ =

As the above example, you have to pass modelDependencies to libraryDependencies . 如上例所示,您必须将modelDependencies传递给libraryDependencies You had it calling directly into settings . 您可以直接调用settings

resolvers 解析器

The resolvers setting is not passed into anything, which likely is not correct. resolvers设置未传递到任何内容中,这可能是不正确的。

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

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