joesan
joesan

Reputation: 15435

How to make properties file available to FileInputStream in Scala script?

I have a Scala script that I want to call from sbt. This Scala script refers to some dependencies. One of those dependencies uses a properties file. This properties file is provided by the run time as this dependency is run as a separate application.

Just to have the possibility to run that property-using dependency as a standalone, I wrote this Scala script that I want to call from sbt.

val fis = new FileInputStream("my.properties") // Fails here
val props = new Properties()

When I run the above code, it fails with an exception in my dependency where the properties file is loaded.

How to make this properties file available to the script under sbt?

Upvotes: 1

Views: 220

Answers (1)

Jacek Laskowski
Jacek Laskowski

Reputation: 74739

Place the file my.properties in src/main/resources and use Source.fromURL(getClass.getResource("/my.properties")) instead (as it gives you more flexibility in where you can place the file on file systems as long as it's on CLASSPATH).

As a helper, use the following code to learn about the place where the file is expected when a "bare" File* types are in use:

println(new java.io.File("my.properties").getAbsolutePath)

Since the current working directory is the top-level directory of a project, the file is searched in $PROJECT_ROOT_DIR/my.properties.

Upvotes: 1

Related Questions