Using ant to exec mysql .sql files via a fileset

In trying to automate our DB development, we decided to use ant and Jenkins to run our SQL scripts for a deploy. We had a hard time getting ant to work and after much trial and error (and cussing and fussing) we finally got it to work. The key really was the addsourcefile=”false” attribute and the redirector. Since mysql can run sql from a file using redirection (< ) you have to use a redirector (which is poorly documented). The ant apply task can apply an executable to every file in a fileset, so this will use the mysql executable to exec every *.sql file it finds.

<target name="test">
  <apply executable="mysql" dir="." output="antlog.txt" error="anterr.txt" append="true" 
        verbose="true" addsourcefile="false">
    <arg value="-h${host}"></arg>
    <arg value="-u${user}"></arg>
    <arg value="-p${pass}"></arg>
    <arg value="${dbname}"></arg>
    <fileset dir=".">
      <include name="src/production/**/*.sql"></include>
    </fileset>
    <redirector>
      <inputmapper type="glob" from="*" to="*"></inputmapper>
    </redirector>
  </apply>
</target>

Leave a Reply

Your email address will not be published.