【问题标题】:Run SBT 1.2.8 project with Java -D options on Windows在 Windows 上使用 Java -D 选项运行 SBT 1.2.8 项目
【发布时间】:2020-03-27 10:04:24
【问题描述】:

我一直在使用已弃用的 SBT Activator 包装器运行我的 Play 项目,它允许我为它启动的 JVM 指定 -D 选项,如下所示:

> activator -Dhttp.port=9000 -Dplay.server.websocket.frame.maxLength=10485760 "run 9000"

这非常有用,因为它允许我创建单独的 .bat 文件以在不同端口上运行给定项目,这非常好,因为我正在并行处理多个不同的网站。

但是,我无法将此命令行转换为直接使用 SBT:

> sbt -Dhttp.port=9000 -Dplay.server.websocket.frame.maxLength=10485760 "run 9000"
...
[error] Expected letter
[error] Expected symbol
[error] Expected '+'
[error] Expected '++'
[error] Expected 'java++'
[error] Expected 'java+'
[error] Expected '^'
[error] Expected '^^'
[error] Expected '+-'
[error] Expected 'debug'
[error] Expected 'info'
[error] Expected 'warn'
[error] Expected 'error'
[error] Expected 'addPluginSbtFile'
[error] Expected 'show'
[error] Expected 'all'
[error] Expected 'Global'
[error] Expected '*'
[error] Expected 'Zero'
[error] Expected 'ThisBuild'
[error] Expected 'ProjectRef('
[error] Expected '{'
[error] Expected project ID
[error] Expected configuration
[error] Expected configuration ident
[error] Expected key
[error] Expected end of input.
[error] Expected ';'
[error] Expected 'early('
[error] Expected '-'
[error] Expected '--'
[error] Expected '!'
[error] .port
[error] ^

按照https://stackoverflow.com/a/47914062/708381的建议添加-J

> sbt -J-Dhttp.port=9000 -J-Dplay.server.websocket.frame.maxLength=10485760 "run 9000"
...
[error] Expected symbol
[error] Not a valid command: -
[error] Expected end of input.
[error] Expected '--'
[error] Expected 'debug'
[error] Expected 'info'
[error] Expected 'warn'
[error] Expected 'error'
[error] Expected 'addPluginSbtFile'
[error] -J-Dhttp
[error]  ^

SBT 文档列出了许多属性(所有这些属性都包含点),但未能提供任何完整的命令行示例来说明如何实际指定它们。似乎您应该能够像我的第一个示例一样“只”执行 -Dprop=value :https://www.scala-sbt.org/1.x/docs/Command-Line-Reference.html

(是的,有更多最新版本的 SBT 可用,但我被这个错误阻止了:https://github.com/sbt/sbt/issues/5046 - 理想情况下,任何解决方案都适用于任何最新版本的 SBT)

【问题讨论】:

  • 我也试过sbt -D"http.port=9000" -D"play.server.websocket.frame.maxLength=10485760" "run 9000"sbt "-Dhttp.port=9000" "-Dplay.server.websocket.frame.maxLength=10485760" "run 9000" 无济于事。

标签: java scala sbt playback typesafe-activator


【解决方案1】:

首先,一些背景...

有多种安装 SBT 的方法。通常它带有一个包装器外壳脚本,它可以方便地运行,因此您不必指定 sbt jar 文件位置。但是,根据您的安装方法,您可能有一个非常简单或更高级的 sbt 包装脚本。

我建议实际检查一次你的 sbt 运行脚本,看看它做了什么。一些基本脚本或手动创建的脚本根本不会将 cmd args 传递给 JVM!

这是您可以使用的一种流行的 sbt 运行器脚本:https://github.com/paulp/sbt-extras

要获得它,只需编写一个像这样的脚本get_sbt.sh:

#!/bin/bash
curl -s https://raw.githubusercontent.com/paulp/sbt-extras/master/sbt > sbt \
  && chmod 0755 sbt

它会为你下载。

在 Fedora Linux(和其他非 Windows 操作系统)上,您可以简单地使用包管理器安装 sbt:

dnf install sbt

这是我的 fedora sbt 软件包的帮助页面:

$ sbt --help

...
  # jvm options and output control
  JAVA_OPTS           environment variable, if unset uses "-Dfile.encoding=UTF-8"
  .jvmopts            if this file exists in the current directory, its contents
                      are appended to JAVA_OPTS
  SBT_OPTS            environment variable, if unset uses ""
  .sbtopts            if this file exists in the current directory, its contents
                      are prepended to the runner args
  /etc/sbt/sbtopts    if this file exists, it is prepended to the runner args
  -Dkey=val           pass -Dkey=val directly to the java runtime
  -J-X                pass option -X directly to the java runtime
                      (-J is stripped)
  -S-X                add -X to sbt's scalacOptions (-S is stripped)

还有来自上述 github 脚本的类似帮助:

$ ./sbt -h

...

  # passing options to the jvm - note it does NOT use JAVA_OPTS due to pollution
  # The default set is used if JVM_OPTS is unset and no -jvm-opts file is found
  <default>         -Xms512m -Xss2m -XX:MaxInlineLevel=18
  JVM_OPTS          environment variable holding either the jvm args directly, or
                    the reference to a file containing jvm args if given path is prepended by '@' (e.g. '@/etc/jvmopts')
                    Note: "@"-file is overridden by local '.jvmopts' or '-jvm-opts' argument.
  -jvm-opts <path>  file containing jvm args (if not given, .jvmopts in project root is used if present)
  -Dkey=val         pass -Dkey=val directly to the jvm
  -J-X              pass option -X directly to the jvm (-J is stripped)

  # passing options to sbt, OR to this runner
  SBT_OPTS          environment variable holding either the sbt args directly, or
                    the reference to a file containing sbt args if given path is prepended by '@' (e.g. '@/etc/sbtopts')
                    Note: "@"-file is overridden by local '.sbtopts' or '-sbt-opts' argument.
  -sbt-opts <path>  file containing sbt args (if not given, .sbtopts in project root is used if present)
  -S-X              add -X to sbt's scalacOptions (-S is stripped)

...现在答案:

您可以像这样测试属性是否正确传递给 sbt:

$ echo 'sys.props.get("test")' | sbt -Dtest=works console
...
scala> sys.props.get("test")
res0: Option[String] = Some(works)

如果您看到None,则说明您的运行器脚本无法正常工作,请重新安装 SBT 或替换脚本。

如果这可行但您的端口没有改变,那么您的配置中的路径可能不同。类型安全配置中的每个点代表一个层次结构(在 json 中)。您可以在开始时打印完整配置以查看其结构。

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2018-02-09
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    相关资源
    最近更新 更多