如何将H2数据库嵌入到交付给客户端的jar文件中?
我在嵌入式模式下使用 H2 数据库作为桌面应用程序。当我将应用程序压缩到 jar 文件中时,数据库文件被省略。因此,当我运行 MyApplication.jar 时,没有任何效果。将 h2.jar 文件嵌入/包含/连接 MyApplication.jar 的正确方法是什么?或者也许还有另一种方式来捆绑提供数据库和应用程序?
I use H2 database for a desktop application in embedded mode. When I compress the application into jar file the database file is omitted. So, when I run MyApplication.jar nothing works. What is the correct way to embed/include/connect h2.jar file with MyApplication.jar? Or maybe there is another way to deliver database and application in the bundle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种常见的方案是将
h2.jar
放在与您的应用程序相关的lib
目录中,并在 JAR 清单中包含一个Class-Path
条目:其条目:附录:这个小型项目包含一个指向JAR 规范 和用于检查清单的便捷实用程序就地。
One common scheme is to put
h2.jar
in alib
directory relative to your application and include aClass-Path
entry in your JAR's manifest with an entry for it:Addendum: This small project includes a link to the JAR specification and a handy utility for examining the manifest in situ.
如果您使用 Maven 构建项目,只需使用 maven-shade-plugin...效果很好,控制也很好,我已经经常使用它了。
为了嵌入所有依赖项,您可以编写如下内容:
然后,只需使用普通的“mvn package”,您就可以将所有依赖项的 jar 包含在应用程序 jar 中。
通过进一步配置,您可以控制包含哪些内容和不包含哪些内容。
If you are using Maven to build your project, just use maven-shade-plugin... great results and control, I've been using it a lot.
For embedding all your dependencies you would write something like:
Then with just a normal "mvn package" you'll have all your dependencies' jars included in your application jar.
With further configuration you can control what gets included and what not.
如果您想将数据库本身放入 jar 文件中,那么以下解释可能会有所帮助:
http://www.h2database.com/html/features.html#database_in_zip
H2 论坛中的此帖子。
If you want to put the database itself in your jar file, then this explanation might help:
http://www.h2database.com/html/features.html#database_in_zip
This is also discussed in this thread in the H2 forum.
如果您使用 Maven 构建项目,请查看 maven- assembly-plugin (jar-with-dependencies)。这将生成一个包含所有依赖项的 jar。
If you're using maven to build you project take a look maven-assembly-plugin (jar-with-dependencies). This would produce single jar with all dependencies packed into it.