Resource does not load into Spring boot jar executable - How does executable jar load resources?

I am trying to run spring jar executable based on the documentation provided here . I expect the resources to be copied to the executable jar at startup

mvn clean package

This is how I run .jar in my project folder

./my-application.jar

In my project, I have a batch process that starts at startup, where I try to load a resource defined in

src/main/resources/batch/request_customers.csv

This is how I load application.properties resource

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

the class

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);
    }


}

error log

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 

I also tried the following way

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

When I launch the application using my IDE and spring plugin, resources are loaded without any problems. But crash when starting an executable bank using. / my -application.jar

  • , , jar, ?
  • , maven ?
  • , ? ?

, , .

UPDATE , . .

BOOT-INF/classes/batch/request_customers.csv

jar , - , . .

StringBuffer = StringBuffer();

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

String line = null;

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

, ( ).

, , , :

ResourceUtils.getURL(usersExistingData).openStream()
+2

ResourceUtil ResourceLoader, ResourceUtil doc

Consider using Spring Resource abstraction in the core package for handling all kinds of file resources in a uniform manner. org.springframework.core.io.ResourceLoader getResource() method can resolve any location to a org.springframework.core.io.Resource object, which in turn allows one to obtain a java.io.File in the file system through its getFile() method. 

, :

import org.springframework.core.io.ResourceLoader;
@Component
public class DataManager {

    @Autowired
    ResourceLoader resourceloader;

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

    public File jsonCustomerData() throws IOException{
        return  resourceloader.getResource(usersExistingData).getFile();
    }


}
+4

Source: https://habr.com/ru/post/1655310/


All Articles