简体   繁体   English

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

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

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. 所以我想使用app目录的绝对路径。

How do I get the absolute path of a Play! 我如何获得Play的绝对路径! app? 应用程序吗? Is there a method for this? 有这个方法吗?

This answer applies only to older versions of the Play Framework, before v2. 在v2之前,此答案仅适用于旧版本的Play Framework。

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.conf as a property. 我认为更好的做法是将目录移到项目安装目录之外,并将其作为属性放在application.conf中。 You then retrieve it when needed. 然后在需要时检索它。 For example: 例如:

Application.conf: Application.conf:

my.file.path=/tmp/whatever

Code: 码:

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

Since version 2.5.0 , play.Play class is deprecated. 从版本2.5.0开始 ,不推荐使用play.Play类。 It is recommended to inject play.Environment and use the method as follows: 建议注入play.Environment并使用如下方法:

public File rootPath();

The play.Environment singleton also contains some very handy method, which provide files by relative path eg play.Environment单例还包含一些非常方便的方法,它通过相对路径提供文件,例如

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

截至播放2.0:play.Play.application()。path()。getAbsolutePath()

这些都不适用于我的播放版本,所以我使用了它:Play.current()。path()。getAbsolutePath()

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

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