52

-Xfatal-warningsとを使用できるようにしたいのです-Ywarn-unused-importが、問題は、アプリケーションの再生ルートを含むファイルでコンパイラがエラーをトリガーしていることです。

[error] /path/to/app/conf/routes: Unused import
[error] /path/to/app/conf/routes: Unused import
[error] /path/to/app/conf/routes:1: Unused import
[error] GET        /document/:id        my.app.controllers.MyController.getById(id: Int)

他のルートも同様です。

scalac にファイルを無視するように指示することは可能でしょうか?

Scalaのバージョンは2.11.8.

4

5 に答える 5

4

恐ろしい「解決策」は、ルートが生成された後、コンパイルタスクが実行される前に、これらの未使用のインポートを削除することです。ここにスケッチがあります:

lazy val optimizeRoutesImports = taskKey[Unit]("Remove unused imports from generated routes sources.")

optimizeRoutesImports := {

  def removeUnusedImports(targetFiles: (File) => PathFinder, linesToRemove: Set[String], linesToReplace: Map[String, String]) = {
    val files = targetFiles(crossTarget.value).get
    files foreach { file =>
      val lines = sbt.IO.readLines(file)
      val updatedLines = lines map { line =>
        linesToReplace.getOrElse(line, line)
      } filterNot { line =>
        linesToRemove.contains(line.trim)
      }
      sbt.IO.writeLines(file, updatedLines, append = false)
    }
  }

  removeUnusedImports(
    _ / "routes" / "main" / "controllers" / "ReverseRoutes.scala",
    Set("import ReverseRouteContext.empty"),
    Map(
      "import play.api.mvc.{ QueryStringBindable, PathBindable, Call, JavascriptLiteral }" ->
        "import play.api.mvc.{ QueryStringBindable, PathBindable, Call }",
      "import play.core.routing.{ HandlerDef, ReverseRouteContext, queryString, dynamicString }" ->
        "import play.core.routing.{ ReverseRouteContext, queryString, dynamicString }"
    )
  )

  removeUnusedImports(
    _ / "routes" / "main" / "controllers" / "javascript" / "JavaScriptReverseRoutes.scala",
    Set(
      "import play.core.routing.{ HandlerDef, ReverseRouteContext, queryString, dynamicString }",
      "import ReverseRouteContext.empty"
    ),
    Map(
      "import play.api.mvc.{ QueryStringBindable, PathBindable, Call, JavascriptLiteral }" ->
        "import play.api.mvc.{ QueryStringBindable, PathBindable }"
    )
  )

  removeUnusedImports(
    _ / "routes" / "main" / "router" / "Routes.scala",
    Set("import play.core.j._"),
    Map())
}

次に、タスクの依存関係を整理します。

// Our optimize routes imports task depends on the routes task.
optimizeRoutesImports := (optimizeRoutesImports dependsOn (play.sbt.routes.RoutesKeys.routes in Compile)).value

// And compilation depends on the unused routes having been removed.
compile := ((compile in Compile) dependsOn optimizeRoutesImports).value

TwirlKeys.templateImportsを有効にする前に、保守的なリストに設定する必要がある場合もあります-Ywarn-unused-import。ビューで使用されているタイプに応じて、次のようになります。

TwirlKeys.templateImports := Seq("play.api.mvc._", "play.api.i18n.Messages", "controllers.routes")

TemplateMagicまた、Twirl テンプレート (YMMV) から未使用のインポートを削除する必要がありました。

  removeUnusedImports(
    _ / "twirl" ** "*.template.scala",
    Set("import play.twirl.api.TemplateMagic._"),
    Map())

その場合は、タスクの依存関係が適切に設定されていることを確認してください。

これは私にとってはうまくいきます。Scala 2.11.8、Play 2.5.10、両方とも-Xfatal-warnings有効-Ywarn-unused-import。恐ろしいですが、うまくいきます。

于 2017-01-17T00:12:11.117 に答える