Best practice for Props file + SBT

804 views
Skip to first unread message

parsa

unread,
Jun 11, 2011, 10:50:28 AM6/11/11
to simple-build-tool
Hi folks,

I need simple key-value "properties" files in most of my projects,
where I as well use SBT. I was wondering:

1. If SBT has some utility for making this simpler
2. if there's an SBT plugin for this

Parsa

Doug Tangren

unread,
Jun 11, 2011, 11:04:30 AM6/11/11
to simple-b...@googlegroups.com
The layout of sbt projects follows the same pattern of similar maven style project

It's a common practice to put configuration and properties files under /src/{cp}/resources/ (where cp is either main or test) which the copy-resources will copy them to the target directory when building the project. In your source you can reference them as you would any other in a similar mvn project

src
    /main
           /resources
                          /config.properties
          /scala
                  /Foo.scala

From Foo.scala, you can say getClass().getResourceAsStream("/config.properties") which can be loaded in to a java Properties object or similar config object

There are a few other scala config libraries out there like configgy [1] and fig [2] both of which are probably overkill if you only need key-value configuration

As far as sbt is concerned there is a property interface [1] for typed env properties


-Doug Tangren
http://lessis.me


Byron Weber Becker

unread,
Jun 11, 2011, 2:30:05 PM6/11/11
to simple-b...@googlegroups.com

On 2011-06-11, at 11:04 AM, Doug Tangren wrote:

> The layout of sbt projects follows the same pattern of similar maven style project
>
> It's a common practice to put configuration and properties files under /src/{cp}/resources/ (where cp is either main or test) which the copy-resources will copy them to the target directory when building the project. In your source you can reference them as you would any other in a similar mvn project
>
> src
> /main
> /resources
> /config.properties
> /scala
> /Foo.scala
>
> From Foo.scala, you can say getClass().getResourceAsStream("/config.properties") which can be loaded in to a java Properties object or similar config object

This is something I was looking at just today (in the source for net.liftweb.util.Props)
and still don't understand. In the target directory, the structure in the source directory
tree is maintained:

/target/scala_2.8.1
/resources
properties file
/classes
Foo.class

What logic does getResourceAsStream use to navigate "sideways" in the
directory hierarchy to get from /classes to /resources? It seems like the
argument to getResourceAsStream should be "../resources/config.properties"
-- but that's not how it seems to work.

Explanations welcome!

Byron

---------------------------------------------------------
Byron Weber Becker Voice: 519-888-4567 x34661
School of Computer Science Fax: 519-885-1208
University of Waterloo Office: DC3105
Waterloo, ON N2L 3G1

Advising FAQ: http://www.cs.uwaterloo.ca/current/faq/index.shtml

Reply all
Reply to author
Forward
0 new messages