Question

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>
Was it helpful?

Solution 2

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.

OTHER TIPS

Straight from akka documentation:

If you are using Maven to package your application, you can also make use of the Apache Maven Shade Plugin support for Resource Transformers to merge all the reference.confs on the build classpath into one.

This resolved the issue for me.

 <transformers>
  <transformer
   implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
   <resource>reference.conf</resource>
  </transformer>
  <transformer
   implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
   <manifestEntries>
    <Main-Class>akka.Main</Main-Class>
   </manifestEntries>
  </transformer>
</transformers>

That's right! Just in order to add a bit more information related to that reference.conf I would say that you should go to: Akka Documentation: http://akka.io/docs/?_ga=1.90177882.150089464.1402497958 , pick the version you are using and in it look for General->Configuration inside that page look for 'Listing of the Reference Configuration', that's all the content you may need for that reference.conf. In my case I just copied it all.

Hope it helps to save sometime!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top