在生产期间获取 Play Framework 中的根文件夹?

Get root folder in the Play Framework during production?

我正在做一个项目,我必须在文件系统上保存一些用户生成的临时文件。在我的开发环境中,我在我的根文件夹和用户相对路径中创建了一个名为 usr 的文件夹以从该文件夹读取和写入。

然而,当我 运行 生产服务器使用 /.activator start 时。框架尝试读取 root/target/universal/stage/usr.

我什至尝试使用 Play.application.path.getPath,但我总是得到相同的路径。

有没有办法在生产中获取根文件夹?

谢谢

您正在获取正确的根文件夹。 start 命令的意义是验证您的应用程序将如何 运行 在生产中。因此它构建应用程序,就像您将其推送到产品时所做的那样。然后 "deploy it to prod" - 将其解压缩到 target/universal/stage 文件夹和该文件夹中的 运行 应用程序。所以 target/universal/stage 是您正确的根文件夹。

注意 - 启动命令已弃用:

[warn] The start command is deprecated, and will be removed in a future version of Play.

[warn] To run Play in production mode, run 'stage' instead, and then execute the generated start script in target/universal/stage/bin.

[warn] To test your application using production mode, run 'testProd' instead.

更新

您需要添加到build.sbt下一个代码

mappings in Universal ++=
  (baseDirectory.value / "usr" * "*" get) map
    (x => x -> ("usr/" + x.getName))

这会告诉 sbt 将您的 "usr" 文件夹添加到 "production package"(阶段)。请注意 - 它不会 "link",它会将 usr 文件夹硬复制到 target/universal/stage,因此您将拥有 target/universal/stage/usr,即 usr 的副本在你 运行 命令 start

之前