简体   繁体   中英

DDLUtils not exporting Mysql data using ant script

I want to export mysql database using ddlutils tool in ant

 <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>

but if I check db-schema.xml

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

and data.xml is

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

it is not exporting data. could anyone help me.

I think your problem is the database URL:

jdbc:mysql://localhost:3306/

You haven't specified the database to connect to. Should be something like:

jdbc:mysql://localhost:3306/my_db_name_goes_here

I would also suggest not connecting as "root". Create a mysql user with access to your database.

Aside: Checkout liquibase . I think it's a more powerful tool for managing db schemas.

Working Example

I use apache ivy to manage my 3rd party dependencies. Just ignore the "bootstrap" and "resolve" targets.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM