Classpath resources inside one-jar packaged via sbt

别等时光非礼了梦想. 提交于 2019-12-01 21:04:09

I think one-jar uses a special classloading mechanism?

Yes, this must be true since there is no standardized way to load classes that packaged into dependency jar that is in turn packaged into your application jar. This usually is implemented with additional classloader trickery.

Loading resources when using One-JAR is documented here.

gilad hoch

one-jar packages your resources under the main dir in the output jar. when using sbt, I find it best to configure the packaging of resources myself. usually, I would do something like this:

unmanagedResources in Compile := Seq() //we don't want to add resources from "src/main/resources" to inner jar

mappings in oneJar += (file("src/main/resources/filename.json"),"path/to/resource/in/onejar")

so your resource filename.json will be packaged where you want it in the one-jar jar. when you want the resource at runtime, simply use:

Thread.currentThread.getContextClassLoader.getResourceAsStream("path/to/your/resource")

have a look at this post. it may help with how to package all the resources under src/main/resources...

I found that Spring's core PathMatchingResourcePatternResolver can do this. It's also can find files according to pattern.

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
resolver.getResources("classpath*:some/package/name/**/*.xml");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!