[英]Spring boot batch MultiResourceItemReader: how to read files using @Value() from jar location
I am trying to get the input files using @Value annotation and set it as a Resource array for MultiResourceItemReader as bellow;我正在尝试使用 @Value 注释获取输入文件并将其设置为 MultiResourceItemReader 的资源数组,如下所示;
@Value("classpath*:/input/userdata*.csv")
private Resource[] inputFiles;
It is working fine in IDE when I place input files folder in resources folder.当我将输入文件夹放在资源文件夹中时,它在 IDE 中工作正常。 The issues is when I run the jar file.问题是当我运行 jar 文件时。 It is also working fine if the files are within the jar as in IDE.如果文件在 jar 中(如 IDE 中),它也可以正常工作。 But it is not picking up when I place the input folder in the same directory of jar file.但是当我将输入文件夹放在 jar 文件的同一目录中时,它并没有出现。 Here is my Configuration;这是我的配置;
@Value("classpath*:/input/userdata*.csv")
private Resource[] inputFiles;
@Bean
public MultiResourceItemReader<UserInfo> multiResourceItemreader() {
MultiResourceItemReader<UserInfo> reader = new MultiResourceItemReader<>();
reader.setDelegate(userDataItemReader());
reader.setResources(inputFiles);
return reader;
}
It did not give any error but just the bellow message in console它没有给出任何错误,只是控制台中的以下消息
2021-02-04 16:31:42.959 WARN 15772 --- [ main] o.s.b.item.file.MultiResourceItemReader : No resources to read. Set strict=true if this should be an error condition.
I am not sure what I am missing here.我不确定我在这里缺少什么。 Tried without classpath prefix in @Value but it gives me bellow exception尝试在 @Value 中不使用类路径前缀,但它给了我以下异常
java.lang.IllegalArgumentException: Could not resolve resource location pattern [/input/userdata*.csv]
Not sure is there any other way to set the resources for MultiResourceItemReader other than @Value annotation.不确定除了 @Value 注释之外还有其他方法可以为 MultiResourceItemReader 设置资源。 A help would be greatly appreciated!非常感谢您的帮助!
Edit:- This is a Spring Boot batch application and here is the directory structure编辑:-这是一个 Spring 引导批处理应用程序,这里是目录结构
apps
├── input
│ ├── userdata6.csv
│ ├── userdata7.csv
│ ├── userdata8.csv
│ └── userdata9.csv
└── multisource-batch.jar
I run the jar file as below from the same app directory我从同一个应用程序目录运行 jar 文件如下
java -jar multisource-batch.jar
build.gradle build.gradle
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.thomsoncodes.batch'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
ext {
set('springCloudVersion', "2020.0.0")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-batch'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.batch:spring-batch-test'
implementation 'org.projectlombok:lombok:1.18.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
UPDATE:- Finally I got it working.. Even though I removed the Web dependency the spring was still considering this as web: Did a clean build and updated @Value("file./input/userdata*.csv") as Mahmoud Ben Hassine suggested!更新:-最后我得到了它的工作..即使我删除了 Web 依赖项 spring 仍然认为这是 web:“Benuser 输入数据*”(“@csv”)作为Mahmo./uddata*。哈辛建议!
it is not picking up when I place the input folder in the same directory of jar当我将输入文件夹放在 jar 的同一目录中时,它没有出现
If you are you running your app with java -jar myapp.jar
where the input
directory is in the same directory of myapp.jar
, then you need to use the file:
prefix and not the classpath:
prefix.如果您使用java -jar myapp.jar
运行您的应用程序,其中input
目录与myapp.jar
位于同一目录中,那么您需要使用file:
前缀而不是classpath:
前缀。
@Value("file:input/userdata*.csv")
should work as it will resolve resources from the relative directory where you launched your JVM. @Value("file:input/userdata*.csv")
应该可以工作,因为它将从您启动 JVM 的相对目录中解析资源。
EDIT: You can find a complete example here .编辑:你可以在这里找到一个完整的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.