Classpath resources inside one-jar packaged via sbt

前端 未结 3 2056
我在风中等你
我在风中等你 2021-01-19 06:27

I have a project that is build using SBT which packages a single jar using the one-jar plugin. That project contains a bunch of json files in src/main/resources/fixture whic

3条回答
  •  执念已碎
    2021-01-19 06:28

    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...

提交回复
热议问题