简体   繁体   中英

How to load resources from classpath matching a pattern with Quarkus

I'm trying to load all resources containing a certain pattern (eg *.properties) from classpath. Therefore I've tried to use

ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ArrayList<URL> resources = Collections.list(classLoader.getResources("")); 
resources.add(classLoader.getResource("org"));

In the next step I iterated through the list of URL-resources, create a proper java.nio.FileSystem and use a PathMatcher to find the required resources.

This solution does not work for the following reason:

  • resources in the root-directory will not be found since classloader.getResource() does not work with "" , null or /
  • the prod-profile of course behaves completly different to the dev-profile

Is there a way in Quarkus to load a list of resources from classpath using a pattern.

thanks in advance & best regards

ClassLoader.getResources("name") enumerates all resources with name. But not enumerates "files" in the directory named as "name".

For example you can have to jars which contain files /my.properties . In this case method ClassLoader.getResources("my.properties") returns two URL for each file.

I solved the problem the following way:

First of all I add this maven dependency to my project:

        <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.10.2</version>
        </dependency>

The library provides a class called ClasspathHelper which has been used as described in the following code snippet.

Collection<URL> classpathURLs = ClasspathHelper.forPackage("org", (ClassLoader[]) null);

Path path = null;
FileSystem fileSystem = null;
        
for (URL classpathURL : classpathURLs) {
    try {
        if (classpathURL.toString().startsWith("jar:file:/")) {
            fileSystem = FileSystems.newFileSystem(classpathURL.toURI(), new HashMap<>());
            path = fileSystem.getPath("/");
        } else {
            fileSystem = FileSystems.getDefault();
            path = fileSystem.getPath(classpathURL.getPath());
        }
               
        PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:**/*.properties");
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(path)) {
                    String resourceName = (path.getClass().getName().contains("ZipPath") ? path.toString() : path.toFile().getName());
                    // resource found - do further processing
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (URISyntaxException | IOException ex) {
        // handle exceptions
    }
}

As you can see, a list of classpathURL 's will be returned from the classpathHelper . These URL's starts either with jar:file:/ or with file:/ . For each case a proper FileSystem and Path will be created. Finally the java.nio.PathMatcher in conjunction with a SimpleFileVisitor will be used to find the required resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM