user8080556
user8080556

Reputation:

Start Dropwizard with config.yaml from resources

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

Upvotes: 9

Views: 3906

Answers (3)

hc_dev
hc_dev

Reputation: 9377

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).

Upvotes: 0

condit
condit

Reputation: 10962

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 /)

Upvotes: 8

Paul Jose
Paul Jose

Reputation: 23

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.

Upvotes: 0

Related Questions