可执行Jar在classpath上找不到typesafe / config application.conf

时间:2015-06-13 19:13:35

标签: java maven executable-jar typesafe-config

我有一个命令行应用程序,可以下载一些报告,处理它们,然后将数据上传到Google云端硬盘。我正在使用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");

我正在使用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(我想这也可能是类型安全代码中的错误)。这一切在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

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

2 个答案:

答案 0 :(得分:4)

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

-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。

答案 1 :(得分:1)

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

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