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

Executable Jar cannot find typesafe/config application.conf on classpath

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

来自~/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");

我正在使用 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>

问题是,当我 运行 可执行文件 .jar 时,application.conf 在我的 class 路径中找不到(我想这也可能是类型安全中的错误代码)。所有这些在 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

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

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

-jar 覆盖所有类路径设置,因此只能看到 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。

我也有这个问题。我注意到您在阴影配置中使用了 Class-Path 声明,因此我将 与您的信息合并并添加:

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <manifestEntries>
        <Main-Class>com.my.reports.ReportRunner</Main-Class>
            <Class-Path>.</Class-Path>
    </manifestEntries>
</transformer>