admin管理员组

文章数量:1123376

I have a use case where I need to refer to configuration files from various nested JARs within a Spring Boot fat JAR

 public void searchConfFiles() throws Exception {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    // Search for all .conf files in the nested JARs
    Resource[] resources = resolver.getResources("classpath:/BOOT-INF/lib/**/*.conf");


    System.out.println("resoucre length"+resources.length);
    for (Resource resource : resources) {
        if (resource.exists()) {
            try (InputStream inputStream = resource.getInputStream()) {
                String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
                System.out.println("Found .conf file: " + resource.getURI());
                System.out.println(content);
            }
        }
    }
}

It is displaying a resource length of 0, even though the .conf files are included. The approach should work when running the application using the jar command, gradlew bootRun, or the maven command.

Does Spring provide a solution for this ?

Any other alternatives ?

I have a use case where I need to refer to configuration files from various nested JARs within a Spring Boot fat JAR

 public void searchConfFiles() throws Exception {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    // Search for all .conf files in the nested JARs
    Resource[] resources = resolver.getResources("classpath:/BOOT-INF/lib/**/*.conf");


    System.out.println("resoucre length"+resources.length);
    for (Resource resource : resources) {
        if (resource.exists()) {
            try (InputStream inputStream = resource.getInputStream()) {
                String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
                System.out.println("Found .conf file: " + resource.getURI());
                System.out.println(content);
            }
        }
    }
}

It is displaying a resource length of 0, even though the .conf files are included. The approach should work when running the application using the jar command, gradlew bootRun, or the maven command.

Does Spring provide a solution for this ?

Any other alternatives ?

Share Improve this question asked 13 hours ago Deepu VarmaDeepu Varma 214 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Those jars are in classpath so you can use resolver.getResources("classpath:/*.conf").

本文标签: javaReading files from Resources folder of a Nested jars of a spring boot JarStack Overflow