我找不到任何有关如何使用 ant 任务转储 MySQL 数据库的信息。
我是否必须创建自己的任务才能做到这一点?
ANT script ===generate==> myDataBase.sql
创建一个运行“mysqldump”命令的目标,如下所示:
<target name="dump-database">
<exec executable="mysqldump" output="database-dump.sql">
<arg value="--user=username" />
<arg value="--password=password" />
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="mydatabase" />
</exec>
</target>
现在您可以通过执行ant dump-database进行转储
并使用 ant导入一些 sql 文件,这也很有用:
<exec executable="mysql" failonerror="true" input="file.sql">
<arg value="-u ${user}" />
<arg value="-p${pass}" />
<arg value="-h ${host}" />
<arg value="-P 3306" />
<arg value="-D ${database}" />
</exec>
*注意正确的是-ppassword 而不是-p密码
或者:
<exec executable="mysql" failonerror="true" input="file.sql">
<arg value="--user=${user}" />
<arg value="--password=${pass}" />
<arg value="--host=${host}" />
<arg value="--port=3306" />
<arg value="--database=${database}" />
</exec>
这也很好,因为它不使用像 org.gjt.mm.mysql.Driver 这样的外部 libs/sql 驱动程序。
您可以使用 Exec 任务,它将启动您的脚本,该脚本将执行转储(或其他)所需的所有操作。
如果你想让它成为数据驱动的,试试这个人使用 ant sql 任务:
<macrodef name="sql-retrieve-table-schema">
<attribute name="schema-name"/>
<attribute name="table-name"/>
<attribute name="connection-url"/>
<attribute name="output-file"/>
<sequential>
<sql userid="username" url="@{connection-url}" classpathref="compile.classpath"
password="apassword" driver="com.mysql.jdbc.Driver" print="true" output="@{output-file}"
showheaders="false" showtrailers="false">
SHOW CREATE TABLE @{table-name};
</sql>
</sequential>
</macrodef>