How to load data into data base using dbunit in Maven
Below is my pom.xml
file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/test</url>
<username>usernamet</username>
<password>password</password>
<dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>
<metadataHandlerName>org.dbunit.ext.mysql.MySqlMetadataHandler</metadataHandlerName>
<encoding>utf-8</encoding>
<src>target/dbunit/export.xml</src><!--compare 和 operation 要用到它 -->
<type>CLEAN_INSERT</type><!--operation 要用到它-->
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
</execution>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>operation</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
</dependencies>
</plugin>
开发者_如何转开发 </plugins>
</build>
</project>
I run mvn dbunit:operation
on command line.
Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app
[INFO] task-segment: [dbunit:operation]
[INFO] ------------------------------------------------------------------------
[INFO] [dbunit:operation {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Thu Feb 10 23:02:41 PST 2011
[INFO] Final Memory: 6M/81M
It says build successful. But there is no data in the database.
Found the answer here
If your data file is in flat format (FlatXmlDataSet), adding flat will perform the update.
For example: This doesn't work:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>dbunit-maven-plugin</artifactId> ... <executions> <execution> <phase>test-compile</phase> <goals> <goal>operation</goal> </goals> <configuration> <type>CLEAN_INSERT</type> <src>src/main/resources/opc1.xml</src> </configuration> </execution> </executions> </plugin>
But this works (at least it did for me):
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>dbunit-maven-plugin</artifactId> <version>1.0-beta-3</version> <dependencies> <dependency> <groupId>com.sybase</groupId> <artifactId>jConnect</artifactId> ... <executions> <execution> <phase>test-compile</phase> <goals> <goal>operation</goal> </goals> <configuration> <format>flat</format> <type>CLEAN_INSERT</type> <src>src/main/resources/opc1.xml</src> </configuration> </execution> </executions> </plugin>
- Create the database(schema) in respective user.
- You should have the proper perperties files in XML form in the project.
- Go to the project for and type the command e.g.
c:>project_name>mvn dbunit:operation
. This will add all the properties
精彩评论