Bower integration in play

Play Logo

Bower integration in play

If you want to use a package.json in your play application add

  • the webjars dependency
  • a package.json in root
  • and the following in sbt , so you can access the javascript libraries.
val bowerPath = SettingKey[String]("bower-path", "where bower is installed")

lazy val bowerGenerateTaskImpl: Def.Initialize[Task[Seq[File]]] = (bowerPath,
                                                                   baseDirectory,
                                                                   streams) map {
  (bower, base, s) =>
    try {
      Process("npm" :: "install" :: Nil) ! s.log
      Process(bower :: "install" :: "--allow-root" :: Nil, base) ! s.log
      s.log.info("Bower install finished")
    } catch {
      case e: java.io.IOException =>
        s.log.error(
          "Bower couldn't be found. Please set the configuration key 'SimpleBowerKeys.bowerPath' properly. " + e.getMessage)
    }
    Seq()
}

bowerPath := sys.props.getOrElse("pathBower", default = "bower")
lazy val bowerGen = taskKey[Seq[File]]("Run bower")
bowerGen := bowerGenerateTaskImpl.value

lazy val copyBower = TaskKey[Seq[File]]("copyRes")
copyBower := {
  streams.value.log("Copy bower_components to public folder")
  val source = baseDirectory.value / "bower_components"
  val dest   = baseDirectory.value / "public" / "bower_components"
  IO.copyDirectory(source, dest)
  Seq()
}

lazy val bower = taskKey[Seq[File]]("Run bower")
bower := Def.sequential(bowerGen, copyBower).value
sourceGenerators in Compile += bower.taskValue