使用maven的ant插件
xmlns:artifact="antlib:org.apache.maven.artifact.ant"
使用maven管理的依赖库
<artifact:dependencies pathId="project.lib"> <pom file="../pom.xml" /> </artifact:dependencies>
初始化数据库
<target name="init-db"> <sql driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}" src="${sql.dir}/schema.sql" onerror="continue"> <classpath refid="project.lib" /> </sql> <dbunit driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}"> <dbconfig> <property name="datatypeFactory" value="${dbunit.datatype}" /> </dbconfig> <classpath refid="project.lib" /> <operation type="CLEAN_INSERT" src="${dbunit.data.dir}/default-data.xml" format="flat" /> </dbunit> </target>
整个内容
<?xml version="1.0" encoding="UTF-8"?> <project name="mini-service" default="init-db" xmlns:artifact="antlib:org.apache.maven.artifact.ant"> <artifact:dependencies pathId="project.lib"> <pom file="../pom.xml" /> </artifact:dependencies> <property file="../src/main/resources/application.properties" /> <property name="sql.type" value="h2" /> <property name="dbunit.datatype" value="org.dbunit.ext.h2.H2DataTypeFactory" /> <!-- <property name="sql.type" value="oracle" /> <property name="dbunit.datatype" value="org.dbunit.ext.oracle.Oracle10DataTypeFactory" /> --> <!-- <property name="sql.type" value="mysql" /> <property name="dbunit.datatype" value="org.dbunit.ext.mysql.MySqlDataTypeFactory" /> --> <property name="sql.dir" value="../src/main/resources/sql/${sql.type}" /> <property name="dbunit.data.dir" value="../src/test/resources/data" /> <taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="project.lib" /> <target name="init-db"> <sql driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}" src="${sql.dir}/schema.sql" onerror="continue"> <classpath refid="project.lib" /> </sql> <dbunit driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}"> <dbconfig> <property name="datatypeFactory" value="${dbunit.datatype}" /> </dbconfig> <classpath refid="project.lib" /> <operation type="CLEAN_INSERT" src="${dbunit.data.dir}/default-data.xml" format="flat" /> </dbunit> </target> <target name="exp-db"> <dbunit driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}"> <dbconfig> <property name="datatypeFactory" value="${dbunit.datatype}" /> </dbconfig> <classpath refid="project.lib" /> <export dest="${dbunit.data.dir}/export-data.xml" format="flat" /> </dbunit> </target> <target name="convert.mysql.to.h2" description="convert mysql sql to h2 for testing"> <copy todir="../src/main/resources/sql/h2" overwrite="true"> <fileset dir="../src/main/resources/sql/mysql"> <include name="**/*.sql" /> </fileset> </copy> <replace dir="../src/main/resources/sql/h2" includes="**/*.sql" token="ENGINE=InnoDB" value="" /> <replace dir="../src/main/resources/sql/h2" includes="**/*.sql" token="ENGINE=MyISAM" value="" /> <replace dir="../src/main/resources/sql/h2" includes="**/*.sql" token="bigint not null auto_increment" value="bigint generated by default as identity" /> <replace dir="../src/main/resources/sql/h2" includes="**/*.sql" token="drop foreign key" value="drop constraint" /> <replaceregexp byline="true"> <regexp pattern="INDEX (.*)" /> <substitution expression=" " /> <fileset dir="../src/main/resources/sql/h2"> <include name="*.sql" /> </fileset> </replaceregexp> </target> </project>
application.properties
#h2 version database settings #jdbc.driver=org.h2.Driver #jdbc.url=jdbc:h2:tcp://localhost/~/mini-web #jdbc.username=sa #jdbc.password= #hibernate.dialect=org.hibernate.dialect.H2Dialect #oracle version database settings #jdbc.driver=oracle.jdbc.driver.OracleDriver #jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:XE #jdbc.username=miniweb #jdbc.password=miniweb #hibernate.dialect=org.hibernate.dialect.Oracle10gDialect #mysql version database setting jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mini-web?useUnicode=true&characterEncoding=utf-8 jdbc.username=discuz jdbc.password=discuz hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect #hibernate settings hibernate.show_sql=false hibernate.format_sql=false #dbcp settings dbcp.initialSize=5 dbcp.maxActive=20 dbcp.maxIdle=10