Custom configuration files - Play! Framework 2.0
Asked Answered
D

4

7

I have a question about loading properties from custom configuration files. I have tried two different approaches to loading my oauth.properties file but I can't get either to work so I'm hoping someone here can help me.

The first approach I tried was to add the file to the conf directory and reference it thusly:

String oauthPropertiesFile = ClassLoader.getSystemResource("oauth.properties").getFile();

But that just returned NULL.

The second approach I tries was to add:

@include.oauthProperties = oauth.properties

to the application.conf file and then reference it in my controller like:

String clientId = oauthProperties.clientId;

However this doesn't compile.

Can anyone shed some light on what I'm doing wrong here?

Despain answered 1/5, 2012 at 1:36 Comment(0)
M
12

I'm not sure if conf is part of the classpath. So I would try /conf/oauth.properties or put the file into the classpath. Furthermore you should use Play.application.classloader() instead of Classloader.

About the include: I still think you need to call Play.application().configuration().get("clientID");

To analyze the situation you can start the app with -Dconfig.trace=loads and analyse the configuration with Play.application().configuration().root().render().

Hope this give you enough hints so that you can solve your problem.

Megasporophyll answered 1/5, 2012 at 15:31 Comment(0)
S
16

What worked for me with a file in /conf:

import com.typesafe.config.ConfigFactory

val myConfig = ConfigFactory.load("myfile.properties").getConfig("my.config.prefix")
Spue answered 8/8, 2012 at 14:47 Comment(0)
M
12

I'm not sure if conf is part of the classpath. So I would try /conf/oauth.properties or put the file into the classpath. Furthermore you should use Play.application.classloader() instead of Classloader.

About the include: I still think you need to call Play.application().configuration().get("clientID");

To analyze the situation you can start the app with -Dconfig.trace=loads and analyse the configuration with Play.application().configuration().root().render().

Hope this give you enough hints so that you can solve your problem.

Megasporophyll answered 1/5, 2012 at 15:31 Comment(0)
S
6

In a generic case, to fetch any File from the "/conf" directory, using Play 2.2 it can be done as follows (note that "/conf" is on the classpath, so you should not include it).

Play.application().classloader().getResource("any_file");
Subconscious answered 18/1, 2014 at 15:4 Comment(0)
A
-2

If you want to use scala.io.Source you can also do something like

Source.fromFile(Play.getFile("path-in-your-play-project"))
Accentuation answered 27/11, 2015 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.