运行 使用 mvn 嵌入 H2 数据库 jetty:run

Running embedded H2 database with mvn jetty:run

我一直在尝试弄清楚如何通过配置文件 运行 嵌入式数据库,并能够通过邮递员 运行 REST 调用。 这是我目前所拥有的:

<profile>
        <id>developRest</id>
        <build>
        <plugins>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
            <artifactId>sql-maven-plugin</artifactId>
            <version>1.5</version>
            <dependencies>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <version>${h2.version}</version>
                </dependency>
            </dependencies>
            <configuration>
                <driver>org.h2.Driver</driver>
                <url>jdbc:h2:mem:test</url>
                <username>sa</username>
                <password>sa</password>
            </configuration>
            <executions>
                <execution>
                    <id>my-execution</id>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <autocommit>true</autocommit>
                        <srcFiles>
                            <srcFile>src/test/resources/table-ddl.sql</srcFile>
                            <srcFile>src/test/resources/insert-into-table.sql</srcFile>
                        </srcFiles>
                    </configuration>
                </execution>
            </executions>
        </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <webApp>
                        <descriptor>src/main/webapp/WEB-INF/jetty.xml</descriptor>
                    </webApp>
                    <stopKey></stopKey>
                    <stopPort></stopPort>
                </configuration>
            </plugin>
        </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>${h2.version}</version>
            </dependency>
        </dependencies>
    </profile>

我玩过阶段,但似乎没有什么真正坚持的。当我 运行 使用 mvn sql:execute@my-execution jetty:run 时,servlet 运行s 但是一旦我调用休息方法我得到

Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (my-execution) on project myProject: The parameters 'driver', 'url' for goal org.codehaus.mojo:sql-maven-plugin:1.5:execute are missing or invalid 我错过了什么能让驱动程序和 url 有效?谢谢你的帮助。

更新:使用 mvn -PdevelopRest sql:execute@my-execution jetty:run 摆脱驱动程序和 url 错误但仍然坚持:

### Error querying database.  Cause: org.h2.jdbc.JdbcSQLException: Table "myTable" not found; SQL statement:

从邮递员调用 GET 时。有什么想法吗?

我很难相信您在调用 REST 方法时会遇到 Maven 错误 (Failed to execute goal ...)。

除此之外,我认为您真正的问题是:您将 H2 用作内存数据库,这意味着只要您的应用程序运行,它就可用。当您的应用程序消失时,您的数据库也会消失。

在 Maven 的上下文中,您有多个插件在执行,数据库的寿命不会超过单个插件的执行时间。您的 my-execution 实例化了一个内存数据库,然后它就消失了。 jetty-maven-plugin 创建自己的内存数据库,然后没有进入前一个的任何 DDL/SQL。

可能有多种方法可以解决此问题,例如:

  1. 不要使用内存数据库,而是让 H2 写出文件,例如jdbc:h2:/data/test,或者,因为您使用的是 Maven:jdbc:h2:${project.build.directory}/data/test
  2. 不要使用 sql-maven-plugin 初始化数据库,而是直接在应用程序内部。你可以这样做:

    1. 使用一些自定义代码,您只需将其放在测试类路径中
    2. 通过将 DDL/SQL 添加到应用程序的连接字符串 ("Execute SQL on Connection"),如下所示:

      jdbc:h2:mem:test;INIT=runscript from '~/table-ddl.sql'\;runscript from '~/insert-into-table.sql'";
      

H2 是一个很棒的数据库。祝你好运!