scala – SBT编译顺序不正确

TL / DR:Checkout最后两次提交并运行sbt clean compile:
https://github.com/mirelon/akka-in-action/tree/json/chapter2

有RestInterface.scala:

package com.goticks

import akka.actor._

import spray.routing._
import spray.http.StatusCodes
import spray.httpx.SprayJsonSupport._
import spray.routing.RequestContext
import akka.util.Timeout
import scala.concurrent.duration._
import scala.language.postfixOps

class RestInterface extends HttpServiceActor
                    with RestApi {
  def receive = runRoute(routes)
}

trait RestApi extends HttpService { actor: Actor =>
  import context.dispatcher
  import com.goticks.TicketProtocol._

  implicit val timeout = Timeout(10 seconds)
  import akka.pattern.ask

  def routes: Route =

    path("events") {
      get { requestContext =>
        context.actorOf(Props[Resu]).ask(GetEvents)
      }
    }

}

class Responder(requestContext:RequestContext) extends Actor {
  import TicketProtocol._

  def receive = {

    case Events(events) =>
      requestContext.complete(StatusCodes.OK, events)
      self ! PoisonPill

  }
}

和Resu.scala(请注意Resu在RestInterface之后按字母顺序排列)

package com.goticks

import akka.actor.Actor

class Resu extends Actor {
  import TicketProtocol._
  import spray.json._

  def receive = {

    case GetEvents => {
      println(Event(event = "E").toJson)
    }
  }
}

object TicketProtocol {
  import spray.json._

  case class Event(event:String)

  case object GetEvents

  case class Events(events:List[Event])

  object Event extends DefaultJsonProtocol {
    implicit val format = jsonFormat1(Event.apply)
  }

}

build.sbt:

name := "goticks"

version := "0.1-SNAPSHOT"

organization := "com.goticks"

scalaVersion := "2.11.1"

libraryDependencies ++= {
  val akkaVersion = "2.3.4"
  val sprayVersion = "1.3.1"
  Seq(
    "com.typesafe.akka" %% "akka-actor" % akkaVersion,
    "io.spray" %% "spray-can" % sprayVersion,
    "io.spray" %% "spray-routing" % sprayVersion,
    "io.spray" %% "spray-json" % "1.2.6"
  )
}

build.properties;

sbt.version=0.13.7

plugins.sbt:

resolvers += Classpaths.typesafeResolver

resolvers += "sbt-idea" at "http://mpeltonen.github.com/maven/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")

addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.10.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.8")

编译问题

当我运行sbt clean编译时,一切正常.但是当我将Resu类重构为Ress时(注意Ress在RestInterface之前按字母顺序排列)(也重命名它的文件,可以在这里查看diff:https://github.com/mirelon/akka-in-action/commit/583ca801fb7d1564024eee2f98d57f03ecacc6e5),然后出现编译错误:

[error] /home/miso/IdeaProjects/akka-in-action/chapter2/src/main/scala/com/goticks/Ress.scala:12: Cannot find JsonWriter or JsonFormat type class for com.goticks.TicketProtocol.Event
[error]       println(Event(event = "E").toJson)
[error]                                  ^
[error] one error found
[error] (compile:compile) Compilation failed

显然,缺少一个隐含的json编写器.可能是因为编译顺序不正确?可以按字母顺序编译类,忽略导入吗?

最佳答案 是的,sbt总是按字母顺序编译.问题是sbt在运行编译器之前不知道文件之间的依赖关系.

您所看到的是scala编译器本身依赖于源文件的排序. Sbt总是对源文件进行排序,因此您可以通过将代码放入有效的排序中来解决这些问题.

我不是百分之百确定你为什么要从宏观上的一些暗示中解决这个问题,但这可能是错误的.

点赞