如何找到 Play Framework 应用程序的绝对路径?
目前我正在与一个团队合作开发 Play!框架应用程序。在下一个用户故事中,我必须实现一些不同的文件修改,例如将文件移动到定义的目录。
因为我们在不同的平台上工作,所以我并不总是确定应用程序是否有正确的路径。所以我想使用应用程序目录的绝对路径。
如何获取 Play! 的绝对路径应用程序?有没有办法呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此答案仅适用于 v2 之前的旧版本 Play 框架。
Play 有一个应用程序路径属性:
这将为您提供运行 Play 的目录。
我认为更好的做法是将目录移到项目安装目录之外,并将其路径作为属性放在
application.conf
中。然后您可以在需要时检索它。例如:Application.conf:
代码:
This answer applies only to older versions of the Play Framework, before v2.
Play has an application path property:
This will give you the directory that Play is running from.
I think a better practice is moving the directory outside of your project install directory and placing the path to it in your
application.conf
as a property. You then retrieve it when needed. For example:Application.conf:
Code:
自版本 2.5.0 起,
play.Play
类已弃用。建议注入play.Environment
并使用如下方法:play.Environment
单例还包含一些非常方便的方法,它们通过相对路径提供文件,例如Since version 2.5.0,
play.Play
class is deprecated. It is recommended to injectplay.Environment
and use the method as follows:The
play.Environment
singleton also contains some very handy method, which provide files by relative path e.g.从 play 2.0 开始: play.Play.application().path().getAbsolutePath()
As of play 2.0: play.Play.application().path().getAbsolutePath()
这些都不适用于我的游戏版本,所以我使用了这个:
Play.current().path().getAbsolutePath()
Neither of those worked on my version of play, so I used this instead:
Play.current().path().getAbsolutePath()