我一直试图弄清楚如何通过配置文件运行嵌入式数据库,并能够通过postman运行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 :execute@my-execution: 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,我漏掉了什么,才能让驱动程序和url有效?谢谢你的帮助。
Update:使用mvn -PdevelopRest sql:execute@my-execution jetty:run消除驱动程序和url错误,但仍然停留在:
### Error querying database. Cause: org.h2.jdbc.JdbcSQLException: Table "myTable" not found; SQL statement:给邮递员打电话的时候。有什么想法吗?
发布于 2016-11-19 09:29:31
我很难相信当您调用REST方法(Failed to execute goal ...)时会出现Maven错误。
除此之外,我认为您真正的问题是:您正在使用H2作为内存中的数据库,这意味着只要应用程序运行,它就可以使用。当应用程序消失时,数据库也会消失。
在执行多个插件的Maven上下文中,数据库不会超过单个插件的执行时间。您的my-execution实例化内存中的数据库,然后该数据库就会消失。jetty-maven-plugin创建了自己的内存数据库,该数据库没有进入前一个数据库的任何DDL/SQL。
解决这个问题的方法可能有很多,比如:
jdbc:h2:/data/test,或者,因为您使用的是Maven:jdbc:h2:${project.build.directory}/data/testsql-maven-plugin初始化数据库,而是直接在应用程序中进行初始化。你可以这么做: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](http://h2database.com/html/features.html#execute_sql_on_connection)"), like so:jdbc:h2:mem:test;INIT=runscript从'~/table-ddl.sql'\;runscript从‘~/插入到-table.sql’“;
H2是一个很棒的数据库。祝好运!
https://stackoverflow.com/questions/40682724
复制相似问题