简体   繁体   English

如何通过命令行将scalacOptions(以下Xelide)传递给sbt

[英]How to pass scalacOptions (Xelide-below) to sbt via command line

I am trying to call sbt assembly from the command line passing it a scalac compiler flag to elides (elide-below 1). 我正在尝试从命令行调用sbt 程序集 ,将它的scalac编译器标志传递给elides(低于1的elide)。

I have managed to get the flag working in the build.sbt by adding this line to the build.sbt 通过将这一行添加到build.sbt中,我设法使该标志在build.sbt中工作

scalacOptions ++= Seq("-Xelide-below", "1")

And also it's working fine when I start sbt and run the following: 当我启动sbt并运行以下命令时,它也可以正常工作:

$> sbt                                                                                                                        
$> set scalacOptions in ThisBuild ++=Seq("-Xelide-below", "0")

But I would like to know how to pass this in when starting sbt, so that my CI jobs can use it while doing different assembly targets (ie. dev/test/prod). 但是我想知道在启动sbt时如何传递它,以便我的CI作业可以在执行不同的组装目标(例如dev / test / prod)时使用它。

One way to pass the elide level as a command line option is to use system properties 通过级别作为命令行选项的一种方法是使用系统属性

scalacOptions ++= Seq("-Xelide-below", sys.props.getOrElse("elide.below", "0"))

and run sbt -Delide.below=20 assembly . 并运行sbt -Delide.below=20 assembly Quick, dirty and easy. 快速,肮脏和容易。

Another more verbose way to accomplish the same thing is to define different commands for producing test/prod artifacts. 完成同一件事的另一种更冗长的方法是定义用于生成测试/生产工件的不同命令。

lazy val elideLevel = settingKey[Int]("elide code below this level.")
elideLevel in Global := 0
scalacOptions ++= Seq("-Xelide-below", elideLevel.value.toString)
def assemblyCommand(name: String, level: Int) =
  Command.command(s"${name}Assembly") { s =>
    s"set elideLevel in Global := $level" ::
      "assembly" ::
      s"set elideLevel in Global := 0" ::
      s
  }
commands += assemblyCommand("test", 10)
commands += assemblyCommand("prod", 1000)

and you can run sbt testAssembly prodAssembly . 您可以运行sbt testAssembly prodAssembly This buys you a cleaner command name in combination with the fact that you don't have to exit an active sbt-shell session to call for example testAssembly . 这为您提供了更简洁的命令名称,同时您无需退出活动的sbt-shell会话即可调用例如testAssembly My sbt-shell sessions tend to live for a long time so I personally prefer the second option. 我的sbt-shell会话通常存在很长时间,因此我个人更喜欢第二种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM