Start Dropwizard with config.yaml from resources
Asked Answered
U

3

9

I have a dropwizard question. I use Dropwizard with SBT (which works pretty fine). If I run my application i package it with:

$ sbt clean assembly

And than run the application with:

$ java -jar APPLICATION.jar server

The problem is with this command Dropwizard doesnt load my config file (config.yaml), which is in the resources located. Regarding the Dropwizard Docs I always have to give the config file as parameter like:

$ java -jar APPLICATION.jar server config.yaml

This works fine and it loads the application but is there any possibility to tell Dropwizard to load directly the config.yaml file, because my configuration in the config.yaml file is static and it is always the same. Settings like Database etc which are changing from Server Stage to Server Stage are made as Enviroment Variable which I load with EnvironmentVariableSubstitutor.

Thanks

Uxorial answered 13/3, 2018 at 17:12 Comment(3)
You can give default values to your properties in your Configuration objects, or make them not required. I think if they have default values or are not specified as @NotEmpty / @NotNull, then Dropwizard will not complain if you don't load a config file.Unlettered
Have a look at: #46682779Penn
@Penn thanks for the link, maybe my question was unclear, I meant the default in dropwizard is a yaml file in resources folder. Is there no default configuration. So that if I start the application without passing the config.yaml as parameter that he looks if there is a config file in the resource folder?Uxorial
P
8

Use class ResourceConfigurationSourceProvider:

@Override
public void initialize(final Bootstrap<ExampleConfiguration> bootstrap) {  
  bootstrap.setConfigurationSourceProvider(new ResourceConfigurationSourceProvider());
  // The rest of initialize...
}

And then invoke the application like:

java -jar APPLICATION.jar server /resource-config.yaml

(note the initial /)

Penn answered 15/3, 2018 at 14:52 Comment(0)
M
0

While this answer is very late, just thought I'd put this here. There is a dirty little hack to make it work so that you don't have to provide config.yaml in your application arguments.

Basically, you can submit a new String[] args to the run() method in the dropwizard application.

public class ApplicationServer extends Application<Config> {

  public static void main(String[] args) {
    String[] appArgs = new String[2];
    appArgs[0] = args[0]; // This will be the usual server argument
    appArgs[1] = "config.yaml";
    
    new ApplicationServer().run(appArgs);
  }
  
  @Override
  public void run(Config configuration, Environment environment) {
    // Configure your resources and other application related things
  }
  
}

I used this little trick to specify which config file I wanted to run with. So instead of specifying config.yaml, I would give my second argument as DEV/UAT/STAGE/PROD and pass on the appropriate config file to the run method.

Modiste answered 27/1, 2022 at 18:17 Comment(0)
O
0

Also interesting to have a look at: earlye/dropwizard-multi-config

<dependency>
  <groupId>com.thenewentity</groupId>
  <artifactId>dropwizard-multi-config</artifactId>
  <version>{version}</version>
</dependency>

It allows overriding and merging multiple config-files passed on the java command-line like:

java -jar sample.jar server -- sample.yaml override.yaml

Here you pass (1) sample.yaml as the primary configuration (e.g. having default values) and (2) override.yaml as the override. The effective config is a result from merging both in order of appearance: (1) defaults will be overwritten and merged with (2).

Ornelas answered 27/9, 2022 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.