6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

play2.6でtemplateやらroutesでUnusedな警告が出るのを消す

Last updated at Posted at 2017-12-03

play2.6でtemplateやらroutesで覚えのないUnused警告を消したときのメモ

scalacオプションに以下を追加すると、

build.sbt
scalacOptions ++= Seq("-deprecation", "-feature", "-unchecked", "-Xlint")

こんな警告が出た。

sbt.compile
[warn] $HOME/workspace/my-item-search/conf/routes: Unused import
[warn] $HOME/workspace/my-item-search/conf/routes: Unused import
[warn] $HOME/workspace/my-item-search/conf/routes: Unused import
[warn] $HOME/workspace/my-item-search/conf/routes: Unused import
[warn] $HOME/workspace/my-item-search/conf/routes: Unused import
[warn] $HOME/workspace/my-item-search/app/views/footer.scala.html:1: Unused import
[warn] <div class="footer">
[warn] ^
[warn] $HOME/workspace/my-item-search/app/views/header.scala.html:1: Unused import
[warn] @(headerTitle: String)(userName: String)
[warn] ^
[warn] $HOME/workspace/my-item-search/app/views/index.scala.html:1: Unused import
[warn] @*
[warn] ^
[warn] $HOME/workspace/my-item-search/app/views/main.scala.html:1: Unused import
[warn] @import controllers.routes
[warn] ^
[warn] $HOME/workspace/my-item-search/app/views/menu.scala.html:1: Unused import
[warn] @(userName: String)
[warn] ^
[warn] $HOME/workspace/my-item-search/app/views/sidebar.scala.html:1: Unused import
[warn] <div class="side-bar">
[warn] ^
[warn] 11 warnings found

以下が参考になる。

要するに、viewに関してはデフォルトで↓みたいなのが勝手にimportされているがtemplate内で呼び出してないからUnused警告が出るらしい。

xxx.scala.html
import play.twirl.api._
import play.twirl.api.TemplateMagic._

解決策は、まずbuild.sbtに以下を追記する。

build.sbt
TwirlKeys.templateImports := Seq.empty

しかし、こうするとviewのroutesとかHtmlタイプとかがエラーになるので、それぞれ個別に
importする必要がある。

main.scala.html
@import controllers.routes
@import play.twirl.api.Html

routesの警告に関しては、AssetがUnusedらしい。
build.sbtとroutesのassetsを下記のようにする。

build.sbt
import play.sbt.routes.RoutesKeys
RoutesKeys.routesImport := Seq.empty
routes.
GET     /assets/*file              controllers.Assets.at(file)

で、scala.htmlはこんな感じにする。

main.scala.html
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">

元々は↓のようにしていました。

build.sbt
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
main.scala.html
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">

詳細は以下を参照。

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?