以前一直是在pom.xml文件中調用ant 來執行dbunit,這樣有一個弊端就是ant 中的變量和pom中的變量不能很好的共享。
今天發現一個m2 的插件,就是 DbUnit Maven Plugin 。這個插件的使用方法也很簡單,主頁上就有例子。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<!--jar file that has the jdbc driver -->
<dependencies>
<!-- 是連接數據的jar包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<!-- common configurations -->
<!-- 連接數據庫的URL等信息-->
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/xiangyun</url>
<username>xiangyun</username>
<password>xiangyun</password>
</configuration>
<executions>
<execution>
<!-- 執行的階段 這個是在編譯測試文件時執行-->
<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
<configuration>
<type>CLEAN_INSERT</type>
<!-- 執行的文件的位置 同以前使用的dbunit 文件一樣-->
<src>src/test/resources/initData.xml</src>
</configuration>
</execution>
</executions>
</plugin>
這樣就會在執行編譯測試文件時執行DBunit的CLEAN_INSERT操作了。<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<!--jar file that has the jdbc driver -->
<dependencies>
<!-- 是連接數據的jar包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<!-- common configurations -->
<!-- 連接數據庫的URL等信息-->
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/xiangyun</url>
<username>xiangyun</username>
<password>xiangyun</password>
</configuration>
<executions>
<execution>
<!-- 執行的階段 這個是在編譯測試文件時執行-->
<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
<configuration>
<type>CLEAN_INSERT</type>
<!-- 執行的文件的位置 同以前使用的dbunit 文件一樣-->
<src>src/test/resources/initData.xml</src>
</configuration>
</execution>
</executions>
</plugin>
同時使用多個<src>filename</src>不好用,每個<execution>只能使用一個<src></src>,就是只能使用一個文件。