Search code examples
configurationakkamaven-assembly-plugin

Akka configuration overwritting


I'm trying to overwrite Akka configuration in my application. I have created additional lib for the application, which also has application.conf file as it uses Akka. So I have 2 of them:

application.conf in my lib: 
my-conf {
 something = 1
}

application.conf in my app, which uses the lib:
something-else = "foo"
my-conf {
 something = 1000
}

When I'm running the app from Intellij Idea, everything is fine, and lib configuration is being overwritten. To load the config in my app I'm using simple ConfigFactory.load() operation. But when I create a jar of my app with mvn clean compile assembly:single and try to run it with this command: java -Xmx4048m -XX:MaxPermSize=512M -Xss256K -classpath myApp.jar com.myapp.example.MyMain I get error:

Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'something-else'

So I decided to rename the conf file in my app, and load it in such way:

  val defConfig = ConfigFactory load
  val myConfig = ConfigFactory load "myconf"
  val combined = myConfig.withFallback(defConfig)
  val config = ConfigFactory load combined

It finds missing settings, but unfortunately config from my app doesn't override config in my lib. In my lib I load config in default way: val settings = ConfigFactory load Also, "my-conf.something" is an important setting, and I'd like to overwrite it from my app.

What I'm doing wrong? Thanks in advance!

Also, I thought there could be an issue how I create the jar:

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.myapp.example.MyMain</mainClass>
                    </manifest>
                </archive>
                <finalName>loadtest</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>dist-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Solution

  • As far as I understand, your library should create file called reference.conf. According to https://github.com/typesafehub/config :

    libraries should use a Config instance provided by the app, if any, and use ConfigFactory.load() if no special Config is provided. Libraries should put their defaults in a reference.conf on the classpath.

    So, i suggest putting a reference.conf into your library first, to make it clear that it is default configuration, and you won't need to have a withFallback - typesafe-config will handle it for you.

    Update: I don't remember how maven-assembly-plugin works - it may combine all of jar files and resources in a single file, meaning that lib/src/main/resources/application.conf will be overwritten by app/src/main/resources/application.conf in your case - so yet another reason to use reference.conf.