2016-11-18 78 views
0

我一直在想如何通过配置文件运行嵌入式数据库,并且能够通过邮递员运行REST调用。 这是我到目前为止有:用mvn码头运行嵌入式H2数据库:运行

<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:执行@我-执行码头:运行时,servlet运行,但一旦我所说的休息方法,我得到

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:[email protected] jetty:run摆脱了驱动程序和URL错误的,但还是坚持了:

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

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

回答

0

我觉得很难相信当你调用一个REST方法时(Failed to execute goal ...)你会得到一个Maven错误。除此之外,我认为你真正的问题是这样的:你使用H2作为内存数据库,这意味着只要你的应用程序运行就可以使用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是一个真棒数据库。祝你好运!

+0

我最终没有使用内存数据库。我真的希望能够(因为它非常方便!)。但你说得对,因为它不会持续下去,我希望我能弄清楚。谢谢你的建议。由于尝试在其余呼叫中连接到数据库,Maven错误弹出。但是,因为我切换到使用H2服务器,并连接到该数据库,我不再因为数据库持续存在而出现错误。再次感谢! – Kevin