可执行 Jar 在类路径上找不到 typesafe/config application.conf

dus*_*van 4 java executable-jar maven typesafe-config

我有一个命令行应用程序,可以下载一些报告,处理它们,然后将数据上传到 Google Drive。我正在将Typesafe-Config用于我需要的所有魔术字符串。Typesafe-Config 查看我的 application.conf 文件的类路径,并使用 HOCON 将配置对象映射到我的类中的字段,如下所示:

来自~/configs/application.conf

my.homePageUrl = "https://my.home.com"

来自MyClass.java

private static Config config = ConfigFactory.load();
private static final String HOME_URL = config.getString("my.homePageUrl");
Run Code Online (Sandbox Code Playgroud)

我正在使用maven-shade-plugin来构建一个可执行的 .jar,以便在远程服务器上轻松部署。该插件如下所示:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <manifestEntries>
                                <Main-Class>com.my.reports.ReportRunner</Main-Class>
                                <Class-Path>~/configs/application.conf</Class-Path>
                            </manifestEntries>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

问题是,当我运行可执行文件 .jar 时,application.conf在我的类路径中找不到(我想这也可能是类型安全代码中的错误)。所有这些在 Intellij 中都运行良好。

dustinevan@iMac:~/bin$ java -jar my-reports-1.0-SNAPSHOT.jar 
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'my'
    at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:147)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)
    at com.typesafe.config.impl.SimpleConfig.getList(SimpleConfig.java:212)
    at com.typesafe.config.impl.SimpleConfig.getHomogeneousUnwrappedList(SimpleConfig.java:271)
    at com.typesafe.config.impl.SimpleConfig.getStringList(SimpleConfig.java:329)
    at com.stellarliving.reports.ecp.ReportRunner.<clinit>(ReportRunner.java:19)

dustinevan@iMac:~/configs$ ls
total 8
-rw-r--r--@  1 dustinevan  staff  1520 Jun 13 01:16 application.conf
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多排列,并做了大量阅读来解决这个问题,任何帮助将不胜感激。

小智 5

因为我刚刚遇到了同样的问题...

-jar覆盖所有类路径设置,所以只坛子看到。-Dconfig.trace=loads将显示java看到的内容。

我们想要类路径上的 application.conf 以及 jar,所以: java -cp .:my-reports-1.0-SNAPSHOT.jar full.path.to.main.Main 为我做了诀窍。application.conf 找到并覆盖了 jar 中的 reference.conf。