java - 资源未加载到可执行 Spring boot jar 中 - 可执行 jar 如何加载资源?

标签 java spring spring-boot

我正在尝试根据提供的文档运行可执行的 boot spring jar here .我期望资源将在运行时复制到可执行 jar

mvn clean package

这是我在项目文件夹下运行 .jar 的方式

./my-application.jar

在我的项目中,我有一个在启动时运行的批处理进程,我试图加载在

下定义的资源
src/main/resources/batch/request_customers.csv

这是我加载资源的方式 application.properties

data.customers.input=classpath:batch/request_customers.csv

import org.springframework.util.ResourceUtils;
@Component
public class DataManager {

    @Value("${data.customers.input}")
    private String usersExistingData;

    public File jsonCustomerData() throws FileNotFoundException {
        return  ResourceUtils.getFile(usersExistingData);
    }


}

错误日志

s]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.groupon.batch.CustomerItemReader]: Factory method 'reader' threw exception; nested exception is java.io.FileNotFoundException: class path resource [batch/request_customers.csv] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/xxx/projects/myproject/target/myproject-0.0.2-SNAPSHOT.jar!/BOOT-INF/classes!/batch/request_customers.csv; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'job' defined in class path resource [com/mycompany/batch/BatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'job' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1' defined in class path resource [com/mycompany/batch/BatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'step1' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reader' defined in class path resource 

我也尝试过以下路径

data.customers.input=src/resources/batch/request_customers.csv
data.customers.input=batch/request_customers.csv

当我使用我的 IDE 和 spring 插件运行应用程序时,资源加载没有任何问题。但是当我使用 ./my-application.jar 运行可执行 jar 时失败

  • 问题是当 我打包它?
  • 我是否需要做一些其他配置来告诉 maven 按结构复制资源?
  • 如何检查资源是否已打包到 jar 中?由于某些原因我无法提取 jar?

如果知道我究竟如何构建一个基本上像可执行一样工作的可执行 jar,那就太好了。

更新 该文件存在于应加载的 jar 中。我不是从外部文件路径加载。

BOOT-INF/classes/batch/request_customers.csv

这是解决方案

当您构建一个 jar 文件并部署到一个路径时,您需要将资源视为文件系统上的资源 - 相对路径不会神奇地加载资源,这正是我所期望的。您必须以流的形式打开并阅读它们。这里有一种方法可以做到这一点

StringBuffer buffer = new StringBuffer();

BufferedReader inputStream =
        new BufferedReader(new InputStreamReader(resourceloader.getResource(file).getInputStream()));

String line = null;

while ((line = inputStream.readLine()) != null){
    buffer.append(line);
}

最佳答案

您无法获取文件引用,因为该文件在 jar(而不是文件系统)中。

您最好的办法是使用以下方法将其作为流读取:

ResourceUtils.getURL(usersExistingData).openStream()

关于java - 资源未加载到可执行 Spring boot jar 中 - 可执行 jar 如何加载资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39613848/

相关文章:

mysql - Hibernate Spring Boot中如何实现IS-A关系?

mysql - 在 Spring Boot 和 Hibernate 应用程序中使用 XtraDB 作为存储引擎

Java使用notepad++和nppexec编译运行

javascript - 使用JsConstructor处理多个构造函数

java - 这个 contains(T Entry) 方法有什么问题? java 。链表

java - BeanCreationException : Error creating bean with name 'springApplicationAdminRegistrar' . InstanceAlreadyExistsException

java - 在另一个实例中从事务性方法调用 spring 非事务性方法时,事务是否得到传播?

java - Grails Unresolved 依赖错误

spring - org.springframework.web.client.ResourceAccessException : I/O error on GET request for in Microservices

java - FieldError 对象中的参数代码是什么?