avaje / avaje-config

Application configuration / properties loading for JVM applications
https://avaje.io/config
Apache License 2.0
47 stars 7 forks source link

Load configuration from a specific file - is it possible? #129

Closed vnnv closed 3 months ago

vnnv commented 3 months ago

I didn't find a way to crate a Configuration using a specific file via constructor or a specific method. Is there a way to load an yaml file containing configuration that is stored somewhere on non default location? I saw that I can pass file name using pros.file system property or env variable, but I'd like something like Config.from(configFileName) ?

My use case is I have a single properties file that is application configuration but also ebean configuration. I'd like to use yaml and achieve the same.

SentryMan commented 3 months ago

set load.properties via a system property or set it in a property file in the default config location.

SentryMan commented 3 months ago

I'd like something like Config.from(configFileName)?

what benefit does this get you over the configuration-based approach?

vnnv commented 3 months ago

what benefit does this get you over the configuration-based approach?

Full control what is loaded for example. Btw, I tried with load.properties. Not sure why it is not working. I use such code:

        System.setProperty("load.properties", fileName);
        final Configuration configuration = Config.asConfiguration();
        final Properties properties = Config.asProperties();

Properties is almost empty - 2 keys only. Also add it via jvm arguments - -D style also did not worked.

SentryMan commented 3 months ago

Full control what is loaded for example.

you still have that control doing it via configuration.

it is not working.

is the property file on the classpath? if it's somewhere else on your system you'll need to use the file path instead of the name.

SentryMan commented 3 months ago

Ah I see, the issue is a bug on our side

vnnv commented 3 months ago

no the file is not in the classpath. :) ok :) so... I got a 🐛 :)

SentryMan commented 3 months ago

you can also try loading with the -P command line argument

rob-bygrave commented 3 months ago

Also note that the default loading (of application.properties|yaml) etc can be bypassed by using Configuration.builder() like:

https://github.com/avaje/avaje-config/blob/master/avaje-config/src/test/java/io/avaje/config/CoreConfigurationTest.java#L150-L154

BUT wait ...

My use case is I have a single properties file that is application configuration but also ebean configuration

So what is the problem exactly? Ebean uses avaje-config by default so it isn't clear to me what the problem is? You could have application and ebean configuration in the same place like application.yaml - it isn't clear to me what the issue is here?

vnnv commented 3 months ago

Yes, I'd like to avoid loading application.yaml file at all (and all other sources of configuration that are possible) and load a specific file that is somewhere on the filesystem. My applications receive an environment variable - for example $APP1_HOME and in that directory there are sub-directories and files. One of these files is the configuration. My current setup is a file that contains properties (ebean settings + my app settings) and because the file is of properties type it is very easy to load the file and pass it to ebean to create different databases (data sources). So I'd like to replace the properties file with yaml and use the same workflow. That is the reason I am looking for an easy solution to identify the config file runtime and pass it (or the relevant part of it) to ebean.

I checked the above example - this allows me to skip loading of application.yaml but does not allow me to load a different yaml file. I don't keep configurations in the classpath.

rbygrave commented 3 months ago

I'd like to avoid loading application.yaml file at all (and all other places that are possible) and load a specific file that is somewhere on the filesystem.

Right. So System.setProperty("load.properties", fileName); would almost work, in that currently (with that PR/fix) it would load that file but it currently would also by default load application.yaml etc and to get full control we don't want that.

but does not allow me to load a different yaml file.

We can add that capability, so that we can do

    var fileSource = new File("./src/test/resources/yaml/minimal.yaml");

    var configuration = Configuration.builder()
      .load(fileSource) // load from a file
      .load("hi.properties") // load from a resource
      .build();

... and this is the way to get full control over it. This is closer to what you were looking for?

vnnv commented 3 months ago

Yes, thanks :) it seems quite good to me :)

rbygrave commented 3 months ago

Yes, thanks :) it seems quite good to me :)

Ok cool. I'll release that then as version 3.13 and it will also have the fix from @SentryMan, I'll do that now.

vnnv commented 3 months ago

:) Thank you Rob :) will update my deps :)

rbygrave commented 3 months ago

Ok, I'll close this as fixed in 3.13. Comment back here if it doesn't work as you expect etc. Cheers !!