[Play 2.6] Play subproject not able to import play-json library

sbt

(Raul Rodriguez) #1

Hi Guys,

So I’ve split my app into sub-projects for better organization, however. My core sub-module which is a play app can’t import play.api.libs.json.Json
Here is my build.sbt

project/Common.scala

import sbt._
import sbt.Keys._

object Common {

  val settings = Seq(
    organization := "com.company.app",
    scalaVersion := "2.12.5"
  )

  val dependencies = new {

    val macwireVersion = "2.3.0"
    val reactiveMongoVersion = "0.13.0-play26"
    val slickVersion = "3.0.3"
    val postgresVersion = "42.2.2"
    val scalatestPlayVersion = "3.1.2"

    val macwire = "com.softwaremill.macwire" %% "macros" % macwireVersion % "provided"
    val reactivemongo = "org.reactivemongo" %% "play2-reactivemongo" % reactiveMongoVersion
    val slick = "com.typesafe.play" %% "play-slick" % slickVersion
    val slickEvolutions = "com.typesafe.play" %% "play-slick-evolutions" % slickVersion
    val postgresql = "org.postgresql" % "postgresql" % postgresVersion
    val scalatestPlay = "org.scalatestplus.play" %% "scalatestplus-play" % scalatestPlayVersion % Test
  }

  lazy val commonDependencies = Seq(
    dependencies.macwire,
    dependencies.reactivemongo,
    dependencies.slick,
    dependencies.slickEvolutions,
    dependencies.postgresql,
    dependencies.scalatestPlay
  )
}

build.sbt

lazy val core = (project in file("modules/core"))
  .enablePlugins(PlayScala)
  .settings(
    name := "app-core",
    libraryDependencies ++= Common.commonDependencies ++ Seq(
    ),
    Common.settings
  )

lazy val cms = (project in file("modules/cms"))
  .enablePlugins(PlayScala)
  .settings(
    name := "app-cms",
    libraryDependencies ++= Common.commonDependencies,
    Common.settings
  )
  .dependsOn(core % "test->test;compile->compile")
  .aggregate(core)

lazy val api = (project in file("modules/api"))
  .enablePlugins(PlayScala)
  .settings(
    name := "app-api",
    libraryDependencies ++= Common.commonDependencies,
    Common.settings
  )
  .dependsOn(core % "test->test;compile->compile")
  .aggregate(core)

lazy val reporting = (project in file("modules/reporting"))
  .enablePlugins(PlayScala)
  .settings(
    name := "app-reporting",
    libraryDependencies ++= Common.commonDependencies,
    Common.settings
  )
  .dependsOn(core % "test->test;compile->compile")
  .aggregate(core)

lazy val backend = (project in file("."))
  .enablePlugins(PlayScala)
  .settings(
    name := "app-backend",
    libraryDependencies ++= Common.commonDependencies
  )
  .dependsOn(
    core % "test->test;compile->compile",
    cms,
    api,
    reporting
  )
  .aggregate(cms, api, reporting

For the backend app, I can import w/o issues, is there something I’m missing here in the submodule part?

Thanks,


(Marcos Pereira) #2

You need to add Play JSON dependency:

lazy val core = (project in file("modules/core"))
  .enablePlugins(PlayScala)
  .settings(
    name := "app-core",
    libraryDependencies ++= Common.commonDependencies ++ Seq(
        // See https://mvnrepository.com/artifact/com.typesafe.play/play-json
        // for the latest version.
        "com.typesafe.play" %% "play-json" % "2.6.9"
    ),
    Common.settings
  )

Best.


(Raul Rodriguez) #3

I thought the sbt-play-plugin and enablePlugins(PlayScala) took care of that.
Actually, I made it work in IntelliJ by manually configure the subprojects and re-ran the sbt import task again.

I’m confused this is not highlighted in the Play 2.6 documentation for sub projects.
So I guess my question is what libraries get appended when you enable the Play scala plugin in a subproject.

thanks


(Greg Methvin) #4

Pretty sure it was just an IntelliJ issue. You can always check this by seeing if sbt compile works.

If you enable PlayScala you should get play and play-server, from the Play plugin (https://github.com/playframework/playframework/blob/2.6.x/framework/src/sbt-plugin/src/main/scala/play/sbt/PlaySettings.scala#L64) as well as play-akka-http-server by default from the PlayAkkaHttpServer auto plugin (https://github.com/playframework/playframework/blob/2.6.x/framework/src/sbt-plugin/src/main/scala/play/sbt/Play.scala#L122). play-json gets pulled in as a dependency of the core play library: https://mvnrepository.com/artifact/com.typesafe.play/play_2.12/2.6.13.

You can check the (direct) dependencies of the core module using core/libraryDependencies in the sbt shell. If you want more advanced tooling around sbt dependencies I’d suggest https://github.com/jrudolph/sbt-dependency-graph.