无法从Scala中的CrossType.Pure sbt项目导入

时间:2016-08-08 19:09:37

标签: scala playframework playframework-2.0 sbt scala.js

我正在尝试在前端和一个共享项目上使用Scala.js制作Play框架项目。我的sbt配置是:

import sbt.Project.projectToRef

lazy val scalaV = "2.11.8"

lazy val shared = (crossProject.crossType(CrossType.Pure) in file("shared"))
  .settings(
    scalaVersion := scalaV,
    libraryDependencies ++= Seq(
      "com.mediamath" %%% "scala-json" % "1.0"
    ),
    resolvers += "mmreleases" at "https://artifactory.mediamath.com/artifactory/libs-release-global",
    addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
  )
  // set up settings specific to the JS project
  .jsConfigure(_ enablePlugins ScalaJSPlay)


lazy val sharedJVM = shared.jvm.settings(name := "sharedJVM")

lazy val sharedJS = shared.js.settings(name := "sharedJS")

lazy val root = (project in file(".")).settings(
  scalaVersion := scalaV,
  scalaJSProjects := jsProjects,
  pipelineStages := Seq(scalaJSProd, gzip),
  routesGenerator := InjectedRoutesGenerator,
  scalikejdbcSettings,
  libraryDependencies ++= Seq(
    jdbc,
    cache,
    ws,
    evolutions,
    "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test,
    "mysql" % "mysql-connector-java" % "5.1.39",
    "com.vmunier" % "play-scalajs-scripts_2.11" % "0.5.0"
  ),
  resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
).
  enablePlugins(PlayScala).
  aggregate(jsProjects.map(projectToRef): _*)

lazy val jsProjects = Seq(js)


lazy val js = (project in file("client")).settings(
  scalaVersion := scalaV,
  persistLauncher := true,
  persistLauncher in Test := false,
  autoCompilerPlugins := true,
  scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
  libraryDependencies ++= Seq(
    "org.scala-js" %%% "scalajs-dom" % "0.9.0",
    "com.mediamath" %%% "scala-json" % "1.0"
  ),
  resolvers += "mmreleases" at "https://artifactory.mediamath.com/artifactory/libs-release-global",
  resolvers += Resolver.sonatypeRepo("releases"),
  addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
).enablePlugins(ScalaJSPlugin, ScalaJSPlay)

一切正常,但问题是:我无法从Scala.js和Play Framework项目中的共享项目导入任何内容。以下是我的共享项目结构的外观: enter image description here

以下是我试图导入它的方法:

import services.Encryptor

在编译时我收到错误:

  

未找到:对象服务[error] import services.Encryptor

如何修复此问题?

1 个答案:

答案 0 :(得分:1)

首先,永远不会(!)这样做:

lazy val sharedJVM = shared.jvm.settings(name := "sharedJVM")
lazy val sharedJS = shared.js.settings(name := "sharedJS")

这会创建由sbt拾取的新项目,因此交叉项目不再保留正确的项目。有关详细信息,请参阅docs

相反,请使用jsSettingsjvmSettings

(crossProject.crossType(CrossType.Pure) in file("shared"))
  // snip
  .jsSettings(name := "sharedJS")
  .jvmSettings(name := "sharedJVM")

lazy val sharedJVM = shared.jvm
lazy val sharedJS = shared.js

在您的构建中,您的js项目似乎不依赖于共享项目。因此,如果当然共享项目的内容不可用。

你需要

lazy val js = (project in file("client"))
  // snip
  .dependsOn(shared.js)