Play Framework多项目结构:routes文件编译报错求助
多项目Play框架Routes编译错误解决方法
问题场景
基于Play框架搭建多项目结构后,执行sbt clean compile时出现Routes解析错误,所有控制器均位于controllers包下,但编译报错提示找不到controllers.app包下的控制器。单项目结构下运行正常。
目录结构
conf - application.config - routes ..................... -> / app.Routes modules - app - conf - application.conf ..... play.http.router = app.Routes - app.routes - controllers ............. 基础控制器(属于controllers包) project - build.properties ........... sbt.version=1.7.1 - plugins.sbt ................ addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.16") build.sbt ..................... 配置见下文
build.sbt配置
name := """scala-play-angular-seed""" version := "1.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayScala).dependsOn(app).aggregate(app) lazy val app = (project in file("modules/app")).enablePlugins(PlayScala).settings( watchSources ++= (baseDirectory.value / "ui/src" ** "*").get, name := "app", scalaVersion := "2.13.8", libraryDependencies += guice, libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.1.0" % Test, libraryDependencies += "com.h2database" % "h2" % "2.1.214" ) resolvers += Resolver.sonatypeRepo("snapshots") scalaVersion := "2.13.8"
app.routes内容
# Routes # This file defines all application routes (Higher priority routes first) # ~~~~ # Serve index page from public directory GET / controllers.FrontendController.index() # An example route (Prefix all API routes with apiPrefix defined in application.conf) GET /api/summary controllers.HomeController.appSummary # An example route (Prefix all API routes with apiPrefix defined in application.conf) GET /api/segments controllers.HomeController.getSegments # Serve static assets under public directory GET /*file controllers.app.FrontendController.assetOrDefault(file) # Test post request POST /api/postTest controllers.HomeController.postTest()
编译错误信息
[error] /modules/app/conf/app.routes:6:1: type FrontendController is not a member of package controllers.app [error] GET / controllers.app.FrontendController.index() [error] /modules/app/conf/app.routes:9:1: type HomeController is not a member of package controllers.app [error] GET /api/summary controllers.app.HomeController.appSummary [error] /modules/app/conf/app.routes:6:1: type FrontendController is not a member of package controllers.app [error] GET / controllers.app.FrontendController.index() [error] /modules/app/conf/app.routes:9:1: type HomeController is not a member of package controllers.app [error] GET /api/summary controllers.app.HomeController.appSummary [error] four errors found [error] (app / Compile / compileIncremental) Compilation failed [error] Total time: 6 s, completed 15 Sep 2022, 11:42:52
解决方法
1. 修正Routes中的控制器路径错误
查看app.routes中的静态资源路由行,其中多了不必要的.app前缀,控制器实际位于controllers包下,需修改为:
GET /*file controllers.FrontendController.assetOrDefault(file)
2. 禁用Play多项目模块的自动包前缀
Play在多项目模块中,默认会将模块名(此处为app)作为前缀添加到控制器引用上,导致解析成controllers.app.XXX。可通过以下两种方式解决:
- 在模块的
modules/app/conf/application.conf中添加配置:play.routesGenerator = InjectedRoutesGenerator play.routesImport += "controllers._" - 或者直接在
app.routes文件顶部添加导入语句:import controllers._
3. 验证项目依赖与路由引用
确认根项目的conf/routes中引用模块路由的路径正确(当前配置-> / app.Routes无误),同时确保模块的application.conf中play.http.router = app.Routes配置与routes文件名一致。
内容的提问来源于stack exchange,提问作者David Tomecek
相关产品推荐
相关产品推荐

