End*_*Neu 50 scala sbt scalac playframework
我希望能够使用-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)
Run Code Online (Sandbox Code Playgroud)
其他路线也一样.
有可能告诉scalac忽略文件吗?
Scala版本是2.11.8.
我已经为 Play 2.8.11 提出了 Scala 2.13.7 的工作解决方案(不需要任何插件)。查看这些示例并根据您的需求进行调整:
scalacOptions ++= Seq(
"-Wconf:cat=unused-imports&site=.*views.html.*:s", // Silence import warnings in Play html files
"-Wconf:cat=unused-imports&site=<empty>:s", // Silence import warnings on Play `routes` files
"-Wconf:cat=unused-imports&site=router:s", // Silence import warnings on Play `routes` files
"-Wconf:cat=unused-imports&site=v1:s", // Silence import warnings on Play `v1.routes` files
"-Wconf:cat=unused-imports&site=v2:s", // Silence import warnings on Play `v2.routes` files
"-Wconf:cat=unused-imports&site=views.v1:s", // Silence import warnings on Play `views.v1.routes` files
"-Wconf:cat=deprecation&site=controllers\\.v1.*&origin=scala.util.Either.right:s", // Silence deprecations in generated Controller classes
"-Wconf:cat=deprecation&site=.*v1.Routes.*&origin=scala.util.Either.right:s"
) // Silence deprecations in generated Controller classes
Run Code Online (Sandbox Code Playgroud)
如果您想了解更多信息,请查看此文档并在编译器消息输出中添加详细信息
scalacOptions += "-Wconf:any:wv",
Run Code Online (Sandbox Code Playgroud)
专业提示:仅在 CI 内未使用的编译失败
scalacOptions ++= {
// Mark unused errors as info for local development (due to -Werror usage)
if (insideCI.value) Seq.empty else Seq("-Wconf:cat=unused:i")
},
Run Code Online (Sandbox Code Playgroud)
我只是在Scala 2.12和Play 2.6(您现在可能正在使用)中遇到了相同的问题。
一个名为Silencer的Scala编译器插件对其进行了分类:https : //github.com/ghik/silencer
将以下依赖项添加到build.sbt中:
val silencerVersion = "1.2.1"
libraryDependencies ++= Seq(
compilerPlugin("com.github.ghik" %% "silencer-plugin" % silencerVersion),
"com.github.ghik" %% "silencer-lib" % silencerVersion % Provided
)
Run Code Online (Sandbox Code Playgroud)
然后添加(同样在build.sbt中):
scalacOptions += "-P:silencer:globalFilters=Unused import"
Run Code Online (Sandbox Code Playgroud)
后面的文本globalFilters=是正则表达式匹配项的列表,用于编译器警告静默时可以用逗号分隔。您可能需要根据自己的情况调整正则表达式,但是我发现上面的示例很好用。
它的确意味着它使所有“未使用的导入”警告都ctrl+alt+L消失了,但是,如果您习惯于自动格式化代码(在Intellij中),包括整理未使用的导入,那么它就不会造成问题。
一个可怕的“解决方案”可能是在生成路由之后但在编译任务运行之前删除那些未使用的导入。这是一个草图:
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())
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要理清任务依赖性:
// 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
Run Code Online (Sandbox Code Playgroud)
在启用之前,您可能还需要设置TwirlKeys.templateImports为保守列表-Ywarn-unused-import。像这样,取决于您的视图中使用的类型:
TwirlKeys.templateImports := Seq("play.api.mvc._", "play.api.i18n.Messages", "controllers.routes")
Run Code Online (Sandbox Code Playgroud)
我还必须TemplateMagic从 Twirl 模板中删除未使用的导入(YMMV):
removeUnusedImports(
_ / "twirl" ** "*.template.scala",
Set("import play.twirl.api.TemplateMagic._"),
Map())
Run Code Online (Sandbox Code Playgroud)
如果您这样做,请确保正确设置任务依赖性。
这对我有用。Scala 2.11.8、Play 2.5.10,两者均-Xfatal-warnings已-Ywarn-unused-import启用。这很可怕,但很有效。
| 归档时间: |
|
| 查看次数: |
1662 次 |
| 最近记录: |