简体   繁体   中英

Play sub-projects: how to convert to build.sbt

I've a working multi-module Play 2.2 application which is organized like this...

myApp
  + app
  + conf
  + project
      + build.properties
      + Build.scala
      + plugin.sbt

... where Build.scala contains the following statements:

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

  val appName         = "myApp"
  val appVersion      = "1.0-SNAPSHOT"

  val authDependencies = Seq(
    "se.radley" %% "play-plugins-salat" % "1.3.0"
  )

  val mainDependencies = Seq(
    "se.radley" %% "play-plugins-salat" % "1.3.0"
  )

  lazy val auth = play.Project(
    appName + "-auth",
    appVersion,
    authDependencies,
    path = file("modules/auth")).settings(
      lessEntryPoints <<= baseDirectory(customLessEntryPoints),
      routesImport += "se.radley.plugin.salat.Binders._",
      templatesImport += "org.bson.types.ObjectId",
      testOptions in Test := Nil,
      resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
    )

  lazy val main = play.Project(
    appName,
    appVersion,
    mainDependencies).settings(
      scalacOptions += "-language:reflectiveCalls",
      routesImport += "se.radley.plugin.salat.Binders._",
      templatesImport += "org.bson.types.ObjectId",
      testOptions in Test := Nil,
      lessEntryPoints <<= baseDirectory(customLessEntryPoints),
      resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
    ).dependsOn(auth).aggregate(auth)

  def customLessEntryPoints(base: File): PathFinder = {
    (base / "app" / "assets" / "stylesheets" / "bootstrap" * "bootstrap.less") +++
    (base / "app" / "assets" / "stylesheets" * "*.less")
  }
}

object Resolvers {

  val scalaSbt = Resolver.url("Scala Sbt", url("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots"))(Resolver.ivyStylePatterns)
  val sonatype = Resolver.sonatypeRepo("snapshots")
}

Now reading the Play 2.2 documentation it looks like I should convert my project to build.sbt:

The following example uses a build.scala file to declare a play.Project. This approach was the way Play applications were defined prior to version 2.2. The approach is retained in order to support backward compatibility. We recommend that you convert to the build.sbt based approach or, if using a build.scala, you use sbt's Project type and project macro.

Is there any working example that describes how to replace project/build.scala with build.sbt ? I read some short articles here and there... but I was unable to get a working Play project.

There is no urgent need to convert your build to build.sbt . build.sbt is simpler, but basically just gets compiled into Build.scala .

The other answer to this question will work, but is perhaps a little verbose. Start with the SBT documentation:

http://www.scala-sbt.org/0.13.0/docs/Getting-Started/Multi-Project.html

Now, create specify your main project and sub projects, and put your main project settings into your main build.sbt file:

lazy val auth = project.in(file("modules/auth"))

lazy val main = project.in(file(".")).dependsOn(auth).aggregate(auth)

playScalaSettings

name := "myApp"

version := "1.0-SNAPSHOT"

libraryDependencies += "se.radley" %% "play-plugins-salat" % "1.3.0"

scalacOptions += "-language:reflectiveCalls"

routesImport += "se.radley.plugin.salat.Binders._"

templatesImport += "org.bson.types.ObjectId"

testOptions in Test := Nil

lessEntryPoints <<= baseDirectory(customLessEntryPoints)

resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)

object Resolvers {
  val scalaSbt = Resolver.url("Scala Sbt", url("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots"))(Resolver.ivyStylePatterns)
  val sonatype = Resolver.sonatypeRepo("snapshots")
}

And now, in modules/auth/build.sbt , put your settings for the auth module:

name := "myApp-auth"

lessEntryPoints <<= baseDirectory(customLessEntryPoints)

routesImport += "se.radley.plugin.salat.Binders._"

templatesImport += "org.bson.types.ObjectId"

testOptions in Test := Nil

resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)

Anyway, it might need a bit of tweaking, but hopefully you get the point.

if using a build.scala, you use sbt's Project type and project macro

Replace play.Project with Project and fix the arguments according to the ScalaDoc , it should be something like

lazy val auth = Project(
    appName + "-auth",
    file("modules/auth")).settings(
      version := appVersion,
      libraryDependencies ++= authDependencies,
      lessEntryPoints <<= baseDirectory(customLessEntryPoints),
      routesImport += "se.radley.plugin.salat.Binders._",
      templatesImport += "org.bson.types.ObjectId",
      testOptions in Test := Nil,
      resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
    )

lazy val main = Project(
    appName,
    file("app")).settings(
      version := appVersion,
      libraryDependencies ++= mainDependencies,
      scalacOptions += "-language:reflectiveCalls",
      routesImport += "se.radley.plugin.salat.Binders._",
      templatesImport += "org.bson.types.ObjectId",
      testOptions in Test := Nil,
      lessEntryPoints <<= baseDirectory(customLessEntryPoints),
      resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
    ).dependsOn(auth).aggregate(auth)

Same definitions can be used in build.sbt instead. I would also extract common settings:

val commonSettings = Seq(
  version := appVersion,
  routesImport += "se.radley.plugin.salat.Binders._",
  templatesImport += "org.bson.types.ObjectId",
  testOptions in Test := Nil,
  lessEntryPoints <<= baseDirectory(customLessEntryPoints),
  resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
)

lazy val auth = Project(
    appName + "-auth",
    file("modules/auth")).settings(commonSettings: _*).settings(
      libraryDependencies ++= authDependencies
    )

lazy val main = Project(
    appName,
    file("app")).settings(commonSettings: _*).settings(
      libraryDependencies ++= mainDependencies,
      scalacOptions += "-language:reflectiveCalls"
    ).dependsOn(auth).aggregate(auth)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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