0

我使用包含属性文件的 maven assemble 创建了一个远 jar。它适用于我的 IDE。但是,在我完成package所有操作后,我的应用程序无法正常工作。

我有我所有的属性文件src/main/resources,我可以通过使用来确认jar tf farjar.jar我在根文件夹中看到了属性文件

但是,当我从那个胖 jar 运行我的程序时,我得到了这个错误

Exception in thread "main" java.lang.IllegalArgumentException: resource ci.properties not found.
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:119)
    at com.google.common.io.Resources.getResource(Resources.java:191)
    at io.conde.config.ConfigReader.read(ConfigReader.java:23)
    at io.conde.SparrowHalToS3.main(SparrowHalToS3.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
    at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)

这是我的pom。

 <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

这是我使用番石榴的代码。

public Properties read(String environment) throws IOException {
        final URL url = Resources.getResource(format("%s.properties", environment));
        final ByteSource byteSource = Resources.asByteSource(url);
        InputStream inputStream = null;
        try {
            inputStream = byteSource.openBufferedStream();
            properties.load(inputStream);

            return properties;
        } catch (final IOException ioException) {
            LOG.error(ioException.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (final IOException ioException) {
                    LOG.error(ioException.getMessage());
                }
            }
        }
        return null;
    }
4

1 回答 1

0

你试过SomeClassThatShouldBeInTheSameJar.class.getResource(<whatever>)吗?Resources.getResource基本上只是一个包装器,它使用当前线程的上下文类加载器或Resources.class自身的类加载器来查找资源。虽然在许多情况下它会起作用,但在某些情况下,您需要做一些不同的事情。

于 2017-03-01T20:18:14.050 回答