java - Unable to get file list from standard resource dir in Maven project with Spring PathMatchingResourcePatternResolver -
note: per accepted solution below, appears realated fact spring's defaultresourceloader not use classloader create url instances resources, custom classloaders ignored
https://jira.spring.io/browse/spr-8176
i have standard maven project, looks this
$ tree . ├── pom.xml └── src ├── main │ ├── java │ │ │ └── resources │ └── application.properties └── test └── java i have more resource files shortly in directory tree under resources directory, , loop on them. following several other posts, using pathmatchingresourcepatternresolver spring framework. function have written grab file names follows
public static list<file> getallresourcefiles() { pathmatchingresourcepatternresolver resolver = new pathmatchingresourcepatternresolver(); resource[] resources; try { resources = resolver.getresources("classpath*:.*"); system.out.println(jsonutils.objecttojson(resources)); } catch (ioexception e) { e.printstacktrace(); } return null; } where objecttojson function uses jackson serialize object. when compile , run application, not results. in particular, not see application.properties. how make work?
$ java -jar target/myapp.one-jar.jar [] update: noted in comment below, using simon tuffs' 1 jar maven plugin. however, usual way of reading files classpath works. namely
myclass.class.getresourceasstream("/application.properties"); update 2: tried following:
resources = resolver.getresources("classpath:**/*.*"); and stack trace
java.io.filenotfoundexception: class path resource [] cannot resolved url because not exist @ org.springframework.core.io.classpathresource.geturl(classpathresource.java:178) @ org.springframework.core.io.support.pathmatchingresourcepatternresolver.isjarresource(pathmatchingresourcepatternresolver.java:414) @ org.springframework.core.io.support.pathmatchingresourcepatternresolver.findpathmatchingresources(pathmatchingresourcepatternresolver.java:343) @ org.springframework.core.io.support.pathmatchingresourcepatternresolver.getresources(pathmatchingresourcepatternresolver.java:282) @ com.example.utils.resourceutils.getallresourcefiles(resourceutils.java:29) @ com.example.starter.main(starter.java:14) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ com.simontuffs.onejar.boot.run(boot.java:340) @ com.simontuffs.onejar.boot.main(boot.java:166) the code in starter.java
public static void main(string[] args) { system.out.println(environment.getproperty("com.example.applicationlogpath")); system.out.println(jsonutils.objecttojson(resourceutils.getallresourcefiles())); } the first line executes , prints string value of applicationlogpath property. second causes fnf stack trace. code property uses standard method getting input stream file in jar.
environment.class.getresourceasstream("/application.properties");
i think problem onejar. project runs fine if it's used outside "one-jar" jar, i've tested (after commented jsonutils parts breaking) , can see files it's scanning. , seems others have hit or similar issue, well. also, not sure spring (through jira issue) changed resources loaded please onejar.
Comments
Post a Comment