2014-11-14 53 views
20

我喜歡在頂層定義scalacOptions像這樣(作爲一個例子,忽略項目軸現在):在sbt中,如何在所有配置中覆蓋控制檯的scalacOptions?

scalacOptions += "-Ywarn-unused-import" 

但後來我意識到這是過於嚴格的console。所以我嘗試設置:

scalacOptions in console ~= (_ filterNot (_ == "-Ywarn-unused-import")) 

但是,這沒有奏效(在REPL中仍然有(致命的)警告)。

我用inspect,試圖理解爲什麼:

> inspect console 
[info] Task: Unit 
[info] Description: 
[info] Starts the Scala interpreter with the project classes on the classpath. 
[info] Provided by: 
[info] {file:/a/}b/compile:console 
[info] Defined at: 
[info] (sbt.Defaults) Defaults.scala:261 
[info] Dependencies: 
[info] compile:console::compilers 
[info] compile:console::initialCommands 
[info] compile:console::fullClasspath 
[info] compile:console::taskTemporaryDirectory 
[info] compile:console::scalaInstance 
[info] compile:console::streams 
[info] compile:console::cleanupCommands 
[info] compile:console::scalacOptions 
[info] Delegates: 
[info] compile:console 
[info] *:console 
[info] {.}/compile:console 
[info] {.}/*:console 
[info] */compile:console 
[info] */*:console 
[info] Related: 
[info] test:console 

注:consolecompile:console

  • 提供

    • 取決於compile:console::scalacOptions

    則:

    > inspect compile:console::scalacOptions 
    [info] Task: scala.collection.Seq[java.lang.String] 
    [info] Description: 
    [info] Options for the Scala compiler. 
    [info] Provided by: 
    [info] {file:/a/}b/compile:scalacOptions 
    [info] Defined at: 
    [info] (sbt.Classpaths) Defaults.scala:1593 
    [info] Reverse dependencies: 
    [info] compile:console 
    [info] Delegates: 
    [info] compile:console::scalacOptions 
    [info] compile:scalacOptions 
    [info] *:console::scalacOptions 
    [info] *:scalacOptions 
    [info] {.}/compile:console::scalacOptions 
    [info] {.}/compile:scalacOptions 
    [info] {.}/*:console::scalacOptions 
    [info] {.}/*:scalacOptions 
    [info] */compile:console::scalacOptions 
    [info] */compile:scalacOptions 
    [info] */*:console::scalacOptions 
    [info] */*:scalacOptions 
    [info] Related: 
    [info] *:console::scalacOptions 
    [info] compile:scalacOptions 
    [info] *:scalacOptions 
    [info] */*:scalacOptions 
    [info] test:scalacOptions 
    

    注:compile:console::scalacOptionscompile:scalacOptions

  • 提供了委託鏈沒有達到*:console::scalacOptions(這是我所定義的)

    我的問題是怎麼做的我爲控制檯的所有配置覆蓋scalacOptions?是否可以更改委派鏈?

    我想避免必須設置scalacOptions in (Compile, console)(因爲它將複製(Test, console))或定義一個scalac選項的值。

  • +0

    另請參閱https:// github。com/typelevel/wartremover/issues/131 – 2014-11-27 20:23:21

    回答

    12

    我的問題是如何覆蓋控制檯所有配置的scalacOptions

    我不認爲我們可以給出由sbt的Defaults提供的compile:scalacOptions的存在。唯一具有較高優先級的範圍是compile:console::scalacOptions。 在大多數情況下,人們不希望CompileTest設置跨接線,因此配置範圍更高的優先級我不認爲是一個錯誤的默認值。

    lazy val commonSettings = Seq(
        scalaVersion := "2.11.4", 
        scalacOptions += "-Ywarn-unused-import", 
        scalacOptions in (Compile, console) ~= (_ filterNot (_ == "-Ywarn-unused-import")), 
        scalacOptions in (Test, console) := (scalacOptions in (Compile, console)).value 
    ) 
    

    是否有可能改變委託鏈?

    不,這是不可能的。 BuildStructure中有一個delegates函數的實例,它在加載時初始化並用於所有任務。 訂購在Scope.delegates完成。

    +1

    感謝您的迴應尤金,特別是對內臟的細節/鏈接對我來說非常有價值。我想有一個設計決定在任務之前在配置中進行了默認搜索(在'*:console :: scalacOptions'之前的'compile:scalacOptions')。 – 2015-01-09 11:36:14

    +0

    有沒有辦法讓這些線路上的東西起作用? ivyConfigurations.value.map(c => scalacOptions in(c,console):= consoleCompilerOptions)'(它沒有) – aij 2016-08-18 15:46:36

    相關問題