无法访问源生成器文件中的项目范围的依赖项

时间:2019-05-12 15:59:25

标签: scala import sbt

我有一个Play框架应用程序,其目录结构如下:

server
  - app
    - services
      - MyService.scala
  - project
    - Dependencies.scala
    - src/main/scala
      - MyMetaService.scala
  - build.sbt

/project目录中包含一个我希望能够使用Circe的源代码生成器任务(sbt Task)。我的build.sbt文件确实包含对circe程序包的引用,如果我发出:

import io.circe._

MyService.scala中,包可以很好地解析。但是,如果我在MyMetaService.scala中执行 same ,则程序包无法解析。我知道这是因为以某种方式我没有指定依赖项应该应用于/project目录,但是我不知道该怎么做。这是我的build.sbt

import src.main.scala.generate.ModelGenerator

name := "server"

version := "1.0"

lazy val `server` =
  (project in file("."))
    .settings(libraryDependencies ++= Dependencies.dependencies)
    .enablePlugins(PlayScala)

resolvers ++=
  Seq(
    "Akka Snapshot Repository" at "http://repo.akka.io/snapshots/",
    "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases",
    "releases" at "http://oss.sonatype.org/content/repositories/releases",
    "snapshots" at "http://oss.sonatype.org/content/repositories/snapshots"
  )

scalaVersion := "2.12.2"

sourceGenerators in Compile ++= Seq(
  ModelGenerator.generatorTask.taskValue
)

还有我的Dependencies.scala

import play.sbt.PlayImport._
import sbt._

object Dependencies {

  val dependencies: Seq[ModuleID] =
    Seq(
      jdbc,
      ehcache,
      ws,
      specs2 % Test,
      guice,
      "io.circe" %% "circe-core" % "0.11.1",
      "io.circe" %% "circe-generic" % "0.11.1",
      "io.circe" %% "circe-parser" % "0.11.1"
    )

}

1 个答案:

答案 0 :(得分:2)

build.sbt中为元构建项目创建单独的project/build.sbt,并以与正确构建相同的方式导入依赖项。例如

// This is project/build.sbt

val circeVersion = "0.10.0"

libraryDependencies ++= Seq(
  "io.circe" %% "circe-core",
  "io.circe" %% "circe-generic",
  "io.circe" %% "circe-parser"
).map(_ % circeVersion)

现在应该使project/.../MyMetaService.scala的马戏团可以使用