【问题标题】:Why is a a deprecation warning causing a compile error in SBT为什么弃用警告会导致 SBT 中的编译错误
【发布时间】:2017-11-03 21:07:52
【问题描述】:

我正在进行的项目使用 Scala 和 SBT。我需要在我们的代码中引入一个不推荐使用的方法,而不是仅仅给我一个错误,当我尝试编译代码时,sbt 给我一个编译错误。

是否有某个标志或设置使我可以更改?

method getDB in class Mongo is deprecated: see corresponding Javadoc 
for more information.
[error]   lazy val db: com.mongodb.DB = mongoClient.getDB("foo")
[error]                                             ^
[error] one error found
[error] (web/compile:compileIncremental) Compilation failed
[error] Total time: 9 s, completed Jun 2, 2017 7:20:53 AM

谢谢。

【问题讨论】:

    标签: java scala sbt


    【解决方案1】:

    SBT 为您提供了许多将折旧警告设置为错误的选项。

    set scalacOptions in ThisBuild ++= Seq("-unchecked", "-deprecation", "-Xfatal-warnings")
    

    sbt compile -deprecation
    

    您可以在 build.sbt 文件中进行相同的配置。

    如果您正在使用,则必须删除上述任何选项。 (检查你的编译器设置)

    另外,请sbt clean reload compile。 这对我有用。

    【讨论】:

      【解决方案2】:

      Scalac 有一个标志Xfatal-warnings,它将警告变成错误。见Is there a way in sbt to convert compiler warnings to errors so the build fails?https://github.com/scala/bug/issues/8410

      一种解决方法是定义一个已弃用的特征来调用该方法并使其伴随对象实现该特征:

      scala> @deprecated("","") def foo = "I am deprecated"
      foo: String
      
      scala> @deprecated("","") trait Foo { def safeFoo = foo }; object Foo extends Foo
      defined trait Foo
      defined object Foo
      
      scala> foo
      <console>:13: warning: method foo is deprecated:
             foo
             ^
      res0: String = I am deprecated
      
      scala> Foo.safeFoo
      res1: String = I am deprecated
      

      【讨论】:

      • 该标志实际上是在我的 build.sbt 中设置的,但我已将其删除,但仍然出现编译错误。
      • 你能用你得到的错误编辑你的问题吗?
      • @Kohl 更改后是否重新加载了 sbt 配置(重新启动 sbt 或在 sbt 提示符下键入 reload)?
      • @jan0sch 领先我一秒。
      • @Kohl 可能你有一个多项目构建并且没有从有问题的子项目中删除-Xfatal-warnings,而是从另一个子项目中删除?
      【解决方案3】:

      在您的 build.sbt 中,您可以设置 scalaOptions 如下:

      lazy val exScalacOptions: Seq[String] = Seq(
        "-Xlint",
        "-feature",
        "-deprecation:false",
        "-unchecked"
      )
      

      设置上述值后,您可以在build.sbt 的项目模块设置中引用它,如下所示:

      scalacOptions := exScalacOptions
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-24
        • 2017-07-12
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        • 2011-06-09
        • 1970-01-01
        相关资源
        最近更新 更多