sbt中的工作目录 [英] working directory in sbt

查看:102
本文介绍了sbt中的工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在特定目录中运行Java程序。我认为,对工作目录进行参数设置非常方便,因为它可以轻松管理配置。

I would like to be able to run the java program in a specific directory. I think, that it is quite convenient to parametrize working directory, because it allows to easily manage configurations.

例如,在一个文件夹中,您可以进行测试配置,在另一个文件夹中,您可以拥有生产所需的资源。您可能会认为,可以选择操作类路径以包括/排除资源,但是仅当您对存储在类路径中的资源感兴趣并使用 Classloader.getResource(r)。但是,如果您具有一些外部配置,并且想要使用简单的说明(如 File file = new File( app.properties); ; )访问它,怎么办?

For example in one folder you could have configuration for test, in other you could have resources needed for production. You probably think, that there is option to manipulate classpath for including/exluding resources but such solution works only if you are interested in resources stored in classpath and referencing them using Classloader.getResource(r). But what if you have some external configuration and you want to access it using simple instructions like File file = new File("app.properties");?

让我们看一下普通示例。

Let's see ordinary example.

您的应用程序使用 app.properties 文件,在其中存储以下内容的凭据外部服务。应用程序在工作目录中查找此文件,因为您使用提到的 File file = new File( app.properties); 指令来访问它。在测试中,您想使用特定于测试的 app.properties 。在集成测试中,您要使用特定于其他环境的 app.properties 。最后,在构建和发布应用程序时,您想要提供其他 app.properties 文件。只需输入 File file = new File( app.properties); 而不是(伪代码),您总是希望以相同的方式访问所有这些资源:

Your application uses app.properties file, where you store credentials for external service. Application looks for this file in working directory, because you uses mentioned File file = new File("app.properties"); instruction to access it. In your tests you want to use app.properties specific to your tests. In you integration tests you want to use app.properties specific to another environment. And finally when you build and release application you want to provide other app.properties file. All these resources you want to access always in the same way just by typing File file = new File("app.properties"); instead of (pseudo code):

if(configTest) 
    file = File("testWorkDir/app.properties");
else if(config2) 
    file = File("config2WorkDir/app.properties");
else 
    file = File("app.properties");

或不使用类路径中的资源

or instead of using resources in classpath

this.getClass.getClassLoader.getResource("app.properties");

当然,您是聪明的程序员,并且使用了诸如maven,gradle或sbt这样的构建工具:)

Of course you are clever programmer and you use build tool such maven, gradle or sbt :)

一开始就应该尝试。至少有一个问题:

Enought at the outset. At least The Question:

是否可以在Java中设置工作目录,如果可以的话,如何在构建工具中配置它(尤其是在sbt中)? / strong>

Is there a way to set working directory in java and if yes how to configure it in build tools (especially in sbt)?

其他信息:


  • 更改 user.dir系统属性无法正常运行(我已尝试通过编程方式对其进行更改)。

  • 在sbt中通过baseDirectory设置更改工作目录 进行更改baseDirectory,它不是我的understangind中的基本目录,并且不等于 new java.io.File(。)。getAbsolutePath

  • 提供环境变量(例如YOUR_APP_HOME)并从该路径引用资源是可行的,但需要在代码中记住这一点。

  • changing 'user.dir' system property is not working (I've tried to change it programaticly).
  • In sbt changing 'working directory' via baseDirectory setting for test changes baseDirectory which is not base dir in my understangind and it is not equal new java.io.File(".").getAbsolutePath.
  • Providing environment variable like YOUR_APP_HOME and referencing resources from this path is feasible but require to remember about this in your code.

推荐答案

如果您进行分叉,则可以控制一切,包括工作目录。

If you fork, you can control everything, including the working directory.

http://www.scala-sbt.org/0.13.5/docs/Detailed-Topics/Forking.html

示例代码:

fork in run := true
baseDirectory in run := file("/path/to/working/directory/")

这篇关于sbt中的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆