问题描述
我的项目有多个子项目,我使用 sbt-tpolecat
1 在这个项目中.我在我的代码中使用了 Java 框架.这个框架大量使用流畅的接口,所以我需要在我的代码中抑制许多丢弃的非单位值"警告.
My project has multiple subprojects, and I use sbt-tpolecat
1 in this project.I use a Java framework in my code. This framework use fluent interface heavily so I need to suppress many "discarded non-Unit value" warnings in my code.
这个 sbt-tpolecat
提供了很多开箱即用的有用 scalac 选项,我只想排除我的用例的 -Wvalue-discard
scalac 选项.
This sbt-tpolecat
provided a lot of useful scalac options out of the box, and I just want to exclude the -Wvalue-discard
scalac option for my use case.
问题是我有 4-5 个子项目2 在这个项目中.现在我需要将以下内容添加到每个子项目的 settings
:
The problem is I have 4-5 subprojects2 in this project. And now I need to add the below to every subproject's settings
:
sub_project_name.settings(
scalacOptions ~= (_.filterNot(Set("-Wvalue-discard")))
)
// or
sub_project_name.settings(valueDiscardSetting)
lazy val valueDiscardSetting =
Seq(scalacOptions ~= (_.filterNot(Set("-Wvalue-discard"))))
有没有办法以 DRY 方式在所有子项目中排除此选项?我当前的子项目层次结构类似于:
Is there a way to exclude this option in all subprojects in a DRY way?My current subprojects hierarchy is similar to this:
App -> Frontend -> Common
-> Backend -> Common
推荐答案
常用设置 val
在 val 中定义一系列常用设置并将它们添加到每个项目.以这种方式学习的概念更少.
例如
lazy val commonSettings = Seq(
scalacOptions ~= (_.filterNot(Set("-Wvalue-discard"))),
...
)
lazy val util = (project in file("util")).settings(commonSettings)
lazy val core = (project in file("core")).settings(commonSettings)
常用设置自动插件
自动插件可以为每个项目设置设置.在project/CommonSettingsPlugin.scala
Common settings auto plugin
Auto plugins can set settings for every project. Create the following small plugin under project/CommonSettingsPlugin.scala
object CommonSettingsPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements
override lazy val projectSettings = Seq(
scalacOptions ~= (_.filterNot(Set("-Wvalue-discard")))
)
}
覆盖
override def requires = plugins.JvmPlugin
应该有效地启用插件,而不必在build.sbt
中显式调用enablePlugin
.
should effectively enable the plugin without having to call explicitly enablePlugin
in build.sbt
.
onLoad
发生在所有项目构建和加载后的最后.
onLoad
happens at the end after all projects are built and loaded.
lazy val settingsAlreadyOverridden = SettingKey[Boolean]("settingsAlreadyOverridden","Has overrideSettings command already run?")
settingsAlreadyOverridden := false
commands += Command.command("removeScalacOptions") { state =>
if (settingsAlreadyOverridden.value) {
state
} else {
Project.extract(state).appendWithSession(
Seq(
settingsAlreadyOverridden := true,
scalacOptions ~= (_.filterNot(Set("-Wvalue-discard")))
),
state
)
}
}
onLoad in Global := (onLoad in Global).value andThen ("removeScalacOptions" :: _)
还要考虑他们如何通过解决社区构建中的问题removeScalacOptions
.
Also consider how they addressed the problem in community-build via removeScalacOptions
.
这篇关于如何以 DRY 方式过滤/禁用 SBT 中所有子项目的 scalac 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!