简体   繁体   English

如何将H2数据库嵌入到传递给客户端的jar文件中?

[英]How to embed H2 database into jar file delivered to the client?

I use H2 database for a desktop application in embedded mode. 我在嵌入模式下使用H2数据库作为桌面应用程序。 When I compress the application into jar file the database file is omitted. 当我将应用程序压缩到jar文件时,省略了数据库文件。 So, when I run MyApplication.jar nothing works. 所以,当我运行MyApplication.jar时,没有任何作用。 What is the correct way to embed/include/connect h2.jar file with MyApplication.jar? 使用MyApplication.jar嵌入/包含/连接h2.jar文件的正确方法是什么? Or maybe there is another way to deliver database and application in the bundle? 或者也许还有另一种方法可以在捆绑中提供数据库和应用程序?

One common scheme is to put h2.jar in a lib directory relative to your application and include a Class-Path entry in your JAR's manifest with an entry for it: 一种常见的方案是将h2.jar放在相对于您的应用程序的lib目录中,并在JAR的清单中包含一个Class-Path条目,其中包含一个条目:

Class-Path: lib/h2.jar lib/…

Addendum: This small project includes a link to the JAR specification and a handy utility for examining the manifest in situ . 附录:这个小项目包括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. 如果你使用Maven来构建你的项目,只需使用maven-shade-plugin ...很棒的结果和控制,我一直在使用它。

For embedding all your dependencies you would write something like: 要嵌入所有依赖项,您可以编写如下内容:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.4</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Then with just a normal "mvn package" you'll have all your dependencies' jars included in your application jar. 然后只需一个普通的“mvn包”,您就可以将所有依赖项的jar包含在应用程序jar中。

With further configuration you can control what gets included and what not. 通过进一步配置,您可以控制包含的内容和不包含的内容。

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 如果您想将数据库本身放在jar文件中,那么这个解释可能有所帮助: http//www.h2database.com/html/features.html#database_in_zip

This is also discussed in this thread in the H2 forum . 这也在H2论坛的这个主题中讨论过。

If you're using maven to build you project take a look maven-assembly-plugin (jar-with-dependencies). 如果您正在使用maven构建项目,请查看maven-assembly-plugin(jar-with-dependencies)。 This would produce single jar with all dependencies packed into it. 这将生成包含所有依赖项的单个jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM