一个jar内的classpath资源通过sbt打包 [英] Classpath resources inside one-jar packaged via sbt

查看:149
本文介绍了一个jar内的classpath资源通过sbt打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用SBT构建的项目,它使用one-jar插件打包一个jar。该项目在src / main / resources / fixture中包含一堆json文件,我曾经通过

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 which I used to access via

new java.io.File(App.getClass.getResource("/fixture").getFile

不幸的是,这不是因为没有返回资源,所以更长时间工作。我认为one-jar使用特殊的类加载机制?最好的解决方法是什么?

Unfortunately this doesn't work any longer since no Resource is returned. I think one-jar uses a special classloading mechanism? Whats the best way to solve this?

推荐答案

one-jar在输出jar中的 main dir下打包你的资源。当使用sbt时,我发现最好自己配置资源包。
通常,我会做这样的事情:

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")

所以你的资源 filename .J儿子将被打包到你想要的一罐罐子里。
当你想在运行时使用资源时,只需使用:

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")

查看这篇文章。它可能有助于如何打包 src / main / resources ...

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

这篇关于一个jar内的classpath资源通过sbt打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆