SBT 中测试和发布的不同编译选项? [英] Different compile options for tests and release in SBT?
问题描述
我有一个项目,我需要在创建二进制文件时禁用断言.现在我可以这样做:
I have a project where I need to disable assertions when creating the binaries. Now I could just do:
scalacOptions += "-Xdisable-assertions"
但是单元测试也将在没有断言的情况下运行.有没有(希望)简单的方法来实现我的需要?
But then also the unit tests would be run without assertions. Is there a (hopefully) simple way to achieve what I need?
推荐答案
您如何创建二进制文件?您使用什么任务/命令?
How do you create the binaries? What task/command do you use?
使用任务作为 scalacOptions
的范围来为它们设置不同的值.请参阅 按配置轴确定范围:
Use the task as the scope for scalacOptions
to have different values for them. See Scoping by configuration axis:
默认情况下,所有与编译、打包和运行范围限定于配置,因此可能会起作用每种配置都不同.最明显的例子是任务键编译、打包和运行;但所有影响的键这些键(例如 sourceDirectories 或 scalacOptions 或fullClasspath) 也适用于配置.
By default, all the keys associated with compiling, packaging, and running are scoped to a configuration and therefore may work differently in each configuration. The most obvious examples are the task keys compile, package, and run; but all the keys which affect those keys (such as sourceDirectories or scalacOptions or fullClasspath) are also scoped to the configuration.
有疑问时使用inspect
.
> inspect scalacOptions
[info] Task: scala.collection.Seq[java.lang.String]
[info] Description:
[info] Options for the Scala compiler.
[info] Provided by:
[info] {file:/C:/dev/sandbox/task-dependsOn/}task-dependson/compile:scalacOptions
[info] Defined at:
[info] (sbt.Classpaths) Defaults.scala:1424
[info] Dependencies:
[info] task-dependson/compile:autoCompilerPlugins
[info] task-dependson/compile:settingsData
[info] task-dependson/compile:update
[info] task-dependson/compile:buildDependencies
[info] task-dependson/compile:thisProjectRef
[info] Delegates:
[info] task-dependson/compile:scalacOptions
[info] task-dependson/*:scalacOptions
[info] {.}/compile:scalacOptions
[info] {.}/*:scalacOptions
[info] */compile:scalacOptions
[info] */*:scalacOptions
[info] Related:
[info] b/compile:scalacOptions
[info] b/test:scalacOptions
[info] task-dependson/test:scalacOptions
[info] task-dependson/jacoco:scalacOptions
[info] a/jacoco:scalacOptions
[info] */*:scalacOptions
[info] a/test:scalacOptions
[info] a/compile:scalacOptions
[info] b/jacoco:scalacOptions
Compile
配置范围是默认范围(请参阅项目的 show defaultConfiguration
)所以 scalacOptions += "-Xdisable-assertions"
实际上是scalacOptions in Compile += "-Xdisable-assertions"
.使用不同的配置,比如 Test
,你会得到不同的结果.
The Compile
configuration scope is the default one (see show defaultConfiguration
for a project) so scalacOptions += "-Xdisable-assertions"
is in fact scalacOptions in Compile += "-Xdisable-assertions"
. Use different configuration, say Test
, and you'll get different results.
然而,SBT 有一个问题(我第一次回复时就错过了)——设置是链接的,当一个设置没有在范围中定义时,它会从更通用的范围中获取它的值.当我说 scalacOptions +=...
实际上是 scalacOptions in Compile
我错过了设置的重要功能 - scalacOptions
是全局的,而 scalacOptions in Compile
是 Compile
范围的.
There's however a hitch in SBT (I missed the very first time I responded) - settings are chained and when a setting is not defined in a scope, it gets its value from a more general scope. When I said, scalacOptions +=...
is in fact scalacOptions in Compile
I missed the important feature of settings - scalacOptions
is global while scalacOptions in Compile
is Compile
-scoped.
这篇关于SBT 中测试和发布的不同编译选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!