简体   繁体   English

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

[英]Running embedded H2 database with mvn jetty:run

I've been trying to figure out how to run an embedded database through a profile and be able to run REST calls through postman. 我一直在尝试找出如何通过配置文件运行嵌入式数据库,以及如何通过邮递员运行REST调用。 This is what I have so far: 这是我到目前为止的内容:

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

I've played around with phases but nothing really seems to stick. 我玩过阶段游戏,但是似乎没有任何东西可以坚持。 When I run this with mvn sql:execute@my-execution jetty:run, the servlet runs but once I call a rest method I get 当我使用mvn sql:execute @ my-execution jetty:run运行此程序时,该servlet运行,但是一旦我调用rest方法,我就会得到

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 What am I missing that will get the driver and url to be valid? 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有效? Thanks for your help. 谢谢你的帮助。

Update: Used mvn -PdevelopRest sql:execute@my-execution jetty:run to get rid of the driver and url error but still stuck with: 更新:使用mvn -PdevelopRest sql:execute@my-execution jetty:run摆脱了驱动程序和url错误,但仍然存在:

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

When calling a GET from postman. 从邮递员拨打GET时。 Any Thoughts? 有什么想法吗?

I find it hard to believe that you get a Maven error when you call a REST method ( Failed to execute goal ... ). 我很难相信您在调用REST方法时会遇到Maven错误( Failed to execute goal ... )。

That aside, I think your real problem is this: you are using H2 as an in-memory database, that means that it's available as long as your application runs. 除此之外,我认为您的真正问题是:您正在使用H2作为内存数据库,这意味着只要您的应用程序运行,它就可以使用。 When your application goes away, so does your database. 当您的应用程序消失时,您的数据库也消失了。

In the context of Maven, where you have multiple plugins executing, the database does not outlive the execution of a single plugin. 在您执行多个插件的Maven上下文中,数据库不会超过单个插件的执行时间。 Your my-execution instantiates an in-memory database, which then goes away. my-execution实例化了一个内存数据库,然后该数据库消失了。 The jetty-maven-plugin creates its own in-memory database, which then does not have any of the DDL/SQL that went into the previous one. jetty-maven-plugin创建自己的内存数据库,该数据库没有上一个数据库中的任何DDL / SQL。

There is probably a number of ways to fix this, like these: 可能有多种方法可以解决此问题,例如:

  1. Don't use an in-memory database, rather have H2 write out files, eg jdbc:h2:/data/test , or, since you're using Maven: jdbc:h2:${project.build.directory}/data/test 不要使用内存数据库,而是使用H2来写出文件,例如jdbc:h2:/data/test ,或者,因为您正在使用Maven: jdbc:h2:${project.build.directory}/data/test
  2. Don't initialize the database using the sql-maven-plugin , but directly inside the application. 不要使用sql-maven-plugin初始化数据库,而是直接在应用程序内部。 You could do that: 您可以这样做:

    1. With some custom code, that you only put on the test classpath 使用一些自定义代码,您只需放在测试类路径上
    2. By adding the DDL/SQL to the connection string of the application (" Execute SQL on Connection "), like so: 通过将DDL / SQL添加到应用程序的连接字符串(“ 在连接上执行SQL ”),如下所示:

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

H2 is an awesome database. H2是一个了不起的数据库。 Good luck! 祝好运!

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

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