Akka配置覆盖

psi*_*yev 3 configuration maven-assembly-plugin akka

我正试图在我的应用程序中覆盖Akka配置.我为应用程序创建了额外的lib,它还有application.conf文件,因为它使用了Akka.所以我有两个:

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
}
Run Code Online (Sandbox Code Playgroud)

当我从Intellij Idea运行应用程序时,一切都很好,并且正在覆盖lib配置.要在我的应用程序中加载配置,我正在使用简单的ConfigFactory.load()操作.但是,当我创建一个我的应用程序的jar mvn clean compile assembly:single并尝试使用此命令运行它时:java -Xmx4048m -XX:MaxPermSize=512M -Xss256K -classpath myApp.jar com.myapp.example.MyMain我收到错误:

Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'something-else'
Run Code Online (Sandbox Code Playgroud)

所以我决定在我的应用程序中重命名conf文件,并以这种方式加载它:

  val defConfig = ConfigFactory load
  val myConfig = ConfigFactory load "myconf"
  val combined = myConfig.withFallback(defConfig)
  val config = ConfigFactory load combined
Run Code Online (Sandbox Code Playgroud)

它发现缺少设置,但遗憾的是我的应用程序中的配置不会覆盖我的lib中的配置.在我的lib中,我以默认方式加载配置:val settings = ConfigFactory load 此外,"my-conf.something"是一个重要设置,我想从我的应用程序覆盖它.

我做错了什么?提前致谢!

另外,我认为可能存在如何创建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>
Run Code Online (Sandbox Code Playgroud)

小智 6

直接来自akka文档:

如果您使用Maven打包应用程序,您还可以使用Apache Maven Shade Plugin对Resource Transformers的支持将构建类路径上的所有reference.conf合并为一个.

这解决了我的问题.

 <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>
Run Code Online (Sandbox Code Playgroud)