0

我想在ant中使用ddlutils工具导出mysql数据库

 <target name="export-source-db" description="Dumps db structure and data">
      <taskdef name="databaseToDdl"
              classname="org.apache.ddlutils.task.DatabaseToDdlTask">
    <classpath refid="libraries"/>
    <classpath refid="mysqlclasspath"/>
      </taskdef>

      <databaseToDdl modelName="bwfla">
         <database url="jdbc:mysql://localhost:3306/"
                 driverClassName="com.mysql.jdbc.Driver"
                 username="root"
                 password="sriram"/>
         <writeSchemaToFile outputFile="db-schema.xml"/>
         <writeDataToFile outputFile="data.xml"/>
      </databaseToDdl>

   </target>

但是如果我检查 db-schema.xml

<?xml version="1.0"?>
<!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database">
  <database name="bwfla"/>

和 data.xml 是

<?xml version='1.0' encoding='UTF-8'?>
<data>
</data>

它不是在导出数据。谁能帮助我。

4

1 回答 1

1

我认为您的问题是数据库 URL:

jdbc:mysql://localhost:3306/

您尚未指定要连接的数据库。应该是这样的:

jdbc:mysql://localhost:3306/my_db_name_goes_here

我还建议不要以“root”身份连接。创建一个可以访问您的数据库的 mysql 用户。

旁白:结帐liquibase。我认为它是管理数据库模式的更强大的工具。

工作示例

我使用 apache ivy来管理我的第 3 方依赖项。只需忽略“引导”和“解决”目标。

<project name="ddutils" default="create" xmlns:ivy="antlib:org.apache.ivy.ant">

   <property name="db.driver"   value="com.mysql.jdbc.Driver"/>
   <property name="db.url"      value="jdbc:mysql://localhost:3306/example1"/>
   <property name="db.username" value="example1"/>
   <property name="db.password" value="pleasechangeme"/>

   <target name="bootstrap" description="Install ivy">
      <mkdir dir="${user.home}/.ant/lib"/>
      <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
   </target>

   <target name="resolve" description="Resolve 3rd party dependencies">
      <ivy:cachepath pathid="build.path">
         <!-- Database -->
         <dependency org="mysql" name="mysql-connector-java" rev="5.1.25" conf="default"/>

         <!-- ddlutils plus dependency fixes -->
         <dependency org="org.apache.ddlutils" name="ddlutils" rev="1.0" conf="default"/>
         <dependency org="xml-apis" name="xml-apis" rev="1.0.b2" conf="default" force="true"/>
         <dependency org="xerces" name="xercesImpl" rev="2.11.0" conf="default"/>
         <exclude org="xerces" module="xerces"/>

         <!-- logging libraries -->
         <dependency org="org.slf4j" name="slf4j-simple" rev="1.7.5" conf="default"/>
         <dependency org="org.slf4j" name="log4j-over-slf4j" rev="1.7.5" conf="default"/>
      </ivy:cachepath>
   </target>

   <target name="create" depends="resolve" description="Create tables and data">
      <sql driver="${db.driver}" url="${db.url}" userid="${db.username}" password="${db.password}" classpathref="build.path">
         CREATE TABLE example1 (id INT, one VARCHAR(30), two VARCHAR(30), PRIMARY KEY(id));
         INSERT INTO example1 VALUES (0, 'hello', 'world');
         INSERT INTO example1 VALUES (1, 'hello', 'world');
         INSERT INTO example1 VALUES (2, 'hello', 'world');
         INSERT INTO example1 VALUES (3, 'hello', 'world');
         INSERT INTO example1 VALUES (4, 'hello', 'world');
         INSERT INTO example1 VALUES (5, 'hello', 'world');
         INSERT INTO example1 VALUES (6, 'hello', 'world');
         INSERT INTO example1 VALUES (7, 'hello', 'world');
         INSERT INTO example1 VALUES (8, 'hello', 'world');
         INSERT INTO example1 VALUES (9, 'hello', 'world');

         CREATE TABLE example2 (id INT, one VARCHAR(30), two VARCHAR(30), PRIMARY KEY(id));
         INSERT INTO example2 VALUES (0, 'hello', 'world');
         INSERT INTO example2 VALUES (1, 'hello', 'world');
         INSERT INTO example2 VALUES (2, 'hello', 'world');
         INSERT INTO example2 VALUES (3, 'hello', 'world');
         INSERT INTO example2 VALUES (4, 'hello', 'world');
         INSERT INTO example2 VALUES (5, 'hello', 'world');
         INSERT INTO example2 VALUES (6, 'hello', 'world');
         INSERT INTO example2 VALUES (7, 'hello', 'world');
         INSERT INTO example2 VALUES (8, 'hello', 'world');
         INSERT INTO example2 VALUES (9, 'hello', 'world');
      </sql>
   </target>

   <target name="extract" depends="resolve" description="Use DDLUtils to extract schema and data">
      <taskdef classname="org.apache.ddlutils.task.DatabaseToDdlTask" name="databaseToDdl" classpathref="build.path" />

      <databaseToDdl usedelimitedsqlidentifiers="true" modelname="example">
         <database driverclassname="${db.driver}" url="${db.url}" username="${db.username}" password="${db.password}"/> 
         <writeschematofile outputfile="build/schema.xml"/> 
         <writedatatofile   outputfile="build/data.xml" encoding="ISO-8859-1"/> 
      </databaseToDdl> 
   </target>

   <target name="clean" description="Cleanup project files">
      <delete dir="build"/>
   </target>

   <target name="clean-all" depends="clean" description="Cleanup project files">
     <ivy:cleancache/>
   </target>

</project>
于 2013-06-30T09:09:05.033 回答