working directory in sbt

2020-02-12 03:35发布

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.

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.

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");

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

Enought at the outset. At least The Question:

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

Additional info:

  • 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.

2条回答
▲ chillily
2楼-- · 2020-02-12 04:07

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

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

Example code:

fork in run := true
baseDirectory in run := file("/path/to/working/directory/")
查看更多
做个烂人
3楼-- · 2020-02-12 04:17

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.

I'm not sure what the above statement means, but with sbt you need to fork to change your working directory during the run or test. This is documented in Enable forking and Change working directory.

查看更多
登录 后发表回答