【问题标题】:Custom Template in PlayFramework multiprojectPlay Framework 多项目中的自定义模板
【发布时间】:2017-12-30 08:58:03
【问题描述】:

我有一个带有播放模块和其他 scala 模块的多模块播放应用程序,并且一切正常。我想添加一个自定义 Twirl 模板,这就是问题出现的时候。这是Multiproject structure

build.sbt:

name := """scalaplay"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala).dependsOn(restfulapi,util).aggregate(restfulapi,util)
scalaVersion := "2.11.7"

/**
  * .dependsOn(util). will let us use element from dbmodule into apirestmodule. Specifically some element and structure
  * of the data model.
  *
  */

lazy val restfulapi = (project in file("modules/apirest")).enablePlugins(PlayScala).dependsOn(util).settings(scalaVersion:="2.11.7",
  libraryDependencies ++= Seq(
    cache,
    "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
  )
)

lazy val util = (project in file("modules/dbmodule")).settings(scalaVersion:="2.11.7")

TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")

apirest.routes 的一部分:

    #processing premierLeague
    POST     /premier/match             controllers.PremierleagueController.insertMatch

    GET      /premier/matchs            controllers.PremierleagueController.getMatchGame

    GET     /assets/*file            controllers.Assets.versioned(path="/public", file: Asset)

    GET     /records                    controllers.HomeController.records

使用模板的动作在 HomeController.scala 中:

......

 def records = Action {
    Ok(views.csv.records(Record.sampleRecords))
  }

.....

这是我显示来源时的结果:

[scalaplay] $ show twirlCompileTemplates::sourceDirectories
[info] restfulapi/compile:twirlCompileTemplates::sourceDirectories
[info]  List(/Users/ldipotet/scala/scalaplay/modules/apirest/app)
[info] root/compile:twirlCompileTemplates::sourceDirectories
[info]  List(/Users/ldipotet/scala/scalaplay/app)

这是我尝试编译项目时的编译错误:

[info] Compiling 22 Scala sources and 1 Java source to /Users/ldipotet/scala/scalaplay/modules/apirest/target/scala-2.11/classes...
[error] /Users/ldipotet/scala/scalaplay/modules/apirest/app/controllers/HomeController.scala:72: object csv is not a member of package views
[error]     Ok(views.csv.records(Record.sampleRecords))
[error]              ^
[error] one error found
[error] (restfulapi/compile:compileIncremental) Compilation failed
[error] Total time: 6 s, completed 24-jul-2017 17:18:11

有关更多信息,相同的自定义模板在单个 playframework 项目中编译和工作

【问题讨论】:

    标签: scala templates playframework multi-module


    【解决方案1】:

    问题是对自定义格式文件的引用:

    TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")

    它位于多项目根目录下的唯一built.sbt 文件中。

    -rw-r--r--   1 ldipotet  staff     695 27 jul 01:23 build.sbt
    drwxr-xr-x   5 ldipotet  staff     170 27 jul 01:16 conf
    -rw-r--r--   1 ldipotet  staff  895312 18 jul 10:44 football.txt
    drwxr-xr-x   3 ldipotet  staff     102 27 jul 01:43 logs
    drwxr-xr-x   5 ldipotet  staff     170 12 jul 12:28 modules
    -rw-r--r--   1 ldipotet  staff     191 20 jul 13:36 package.txt
    drwxr-xr-x   6 ldipotet  staff     204 27 jul 01:16 project
    drwxr-xr-x   5 ldipotet  staff     170  1 jun 12:24 public
    -rw-r--r--   1 ldipotet  staff  175256 12 jul 16:54 regex.png
    drwxr-xr-x  11 ldipotet  staff     374 27 jul 01:43 target
    drwxr-xr-x   4 ldipotet  staff     136  5 jul 09:22 test
    

    这是错误的,因为编译器试图在模板所在的项目中查找格式。我的意思是在项目本身(basedir/modules/api rest)中,模板在哪里,那里没有任何参考。

    解决方法:在子项目basedir/modules/apirest中创建一个build.sbt ,创建自定义模板的位置:

    basedir/modules/apirest/built.sbt

    name := """apirest"""
    
    TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
    

    当然还有从根项目的built.sbt 中删除 TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")

    无论是单项目还是多项目,引用都必须放在自定义模板所在的项目中。

    【讨论】:

      【解决方案2】:

      @ldipotet 给出的答案实际上解决了这个问题,但也有其他方法。

      1只需将TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat") 移动到主build.sbt 中的restfulapi 定义中,如下所示:

      ...
      lazy val restfulapi = (project in file("modules/apirest")).enablePlugins(PlayScala).dependsOn(util).settings(scalaVersion:="2.11.7",
        libraryDependencies ++= Seq(
          cache,
          "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
        ),
        TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
      )
      ...
      

      2 如果您将在其他子项目中需要自定义格式,请将常用设置移动到 basedir/project 目录中的新 Common.scala 文件(任意命名)中的对象:

      import sbt._
      
      object Common {
        val settings: Seq[Setting[_]] = Seq(
          TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
        )
      }
      

      然后在子项目build.sbt中使用Common对象:

      name := """apirest"""
      
      Common.settings
      

      有关详细信息,请参阅有关子项目的 PlaySBT 文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 1970-01-01
        • 2013-06-19
        相关资源
        最近更新 更多