sbt 中的工作目录

working directory in sbt(sbt 中的工作目录)

本文介绍了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 中)?

附加信息:

  • 更改user.dir"系统属性不起作用(我尝试以编程方式更改它).
  • 在 sbt 中通过用于测试的 baseDirectory 设置更改 '工作目录' 更改 baseDirectory 这不是我的基本目录理解和它不等于 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.

推荐答案

如果你fork,你可以控制一切,包括工作目录.

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 中的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:sbt 中的工作目录

基础教程推荐