scala 如何找到 Play Framework 应用程序的绝对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4064563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:32:13  来源:igfitidea点击:

How do I find the absolute path to a Play Framework app?

javascalaplayframework

提问by Tobias Schmid

At the moment I'm working with a team on a Play! Framework app. With the next user story, I have to implement some different file modifications, such as moving a file to a defined directory.

目前我正在与一个团队合作玩游戏!框架应用程序。对于下一个用户故事,我必须实现一些不同的文件修改,例如将文件移动到定义的目录。

Because we are working on different platforms, I'm not always really sure if the app has the right path. So I want to work with a absolute path of the app directory.

因为我们在不同的平台上工作,所以我并不总是确定应用程序是否有正确的路径。所以我想使用应用程序目录的绝对路径。

How do I get the absolute path of a Play! app? Is there a method for this?

我如何获得 Play 的绝对路径!应用程序?有没有办法做到这一点?

采纳答案by Damo

This answer applies only to older versions of the Play Framework, before v2.

此答案仅适用于 v2 之前的旧版 Play 框架。

Play has an application path property:

Play 有一个应用程序路径属性:

String projectRoot = Play.applicationPath;

This will give you the directory that Play is running from.

这将为您提供运行 Play 的目录。

I think a better practice is moving the directory outside of your project install directory and placing the path to it in your application.confas a property. You then retrieve it when needed. For example:

我认为更好的做法是将目录移到项目安装目录之外,并将其路径application.conf作为属性放置在您的项目中。然后在需要时检索它。例如:

Application.conf:

应用程序.conf:

my.file.path=/tmp/whatever

Code:

代码:

String projectRoot = Play.configuration.getProperty("my.file.path");

回答by Tom

Since version 2.5.0, play.Playclass is deprecated. It is recommended to inject play.Environmentand use the method as follows:

2.5.0版开始,play.Play不推荐使用 class。建议注入play.Environment使用方法如下:

public File rootPath();

The play.Environmentsingleton also contains some very handy method, which provide files by relative path e.g.

play.Environment单也包含了一些非常方便的方法,这种方法相对路径如提供的文件

public File getFile(String relativePath);
public URL resource(String relativePath);
public InputStream resourceAsStream(String relativePath);

回答by user363349

As of play 2.0: play.Play.application().path().getAbsolutePath()

从 play 2.0 开始: play.Play.application().path().getAbsolutePath()

回答by MrCeeJ

Neither of those worked on my version of play, so I used this instead: Play.current().path().getAbsolutePath()

这些都不适用于我的游戏版本,所以我改用了这个:Play.current().path().getAbsolutePath()