2

I will use the following command to import a set of sql files using the mysql commnd.

<apply dir="${basedir}" executable="mysql" failonerror="true" parallel="true">
    <arg value="-u${db.main.user}" />
    <arg value="-p${db.main.password}" />
    <arg value="-P${db.main.port}" />
    <arg value="-h${db.main.host}" />
    <arg value="-D${db.main.database}" />
    <srcfile/>

    <fileset dir="${db.main.path_to_dump}">
        <filename name="keyword_category_rel.sql"/>
        <filename name="keyword_classification.sql"/>
    </fileset>
 </apply>

The problem is that the mysql command execept the file as command input not as a parameter. So is there a way to provide the file from the fileset as input not as parameter?

Another option is to use the -e argument it accept the sql code from the file, but how can i read the data from the file of the fileset list into a property?

3 Answers3

4

To pass the content of each 'iterator' file in an apply task you can use an input redirector. For example:

<apply executable="${mysql}" addsourcefile="false">
    <fileset dir="${sql.dir}" />
    <redirector>
      <inputmapper type="glob" from="*" to="${sql.dir}/*" />
    </redirector>
</apply>

will process each file found under the sql.dir as the input stream of a mysql invocation. I've omitted all the mysql credential args; they would be needed.

In your example you specify two filename elements for the fileset, but neither contains any wildcard - note that in the fileset docs it says:

If any of the selectors within the FileSet do not select the file, the file is not considered part of the FileSet. This makes a FileSet equivalent to an <and> selector container.

So your example fileset will actually match zero files.

You might also look into using the Ant sql task for this.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
0

Another user asked about Ant run command with pipes for the same reason. Maybe the solutions there will solve your problem.

Community
  • 1
  • 1
ewan.chalmers
  • 16,145
  • 43
  • 60
  • my problem is that i will use the fileset. the problem is that i can't access the srcfile property so i can't use the bash execution – Gordon Franke Oct 11 '11 at 16:18
0

Try the exec task combined with a for loop provided by Ant addon Flaka like that :

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">

 <!-- define your fileset -->
 <fileset dir="${db.main.path_to_dump}" id="foobar">
  <filename name="keyword_category_rel.sql"/>
  <filename name="keyword_classification.sql"/>
 </fileset>

 <!-- call exec for every file in your fileset -->
 <fl:for var="file" in="split('${toString:foobar}', ';')">
  <exec executable="mysql" dir="${basedir}/${build}" input="#{file}">
   <arg value="-u${db.main.user}"/>
   <arg value="-p${db.main.password}"/>
   <arg value="-P${db.main.port}"/>
   <arg value="-h${db.main.host}"/>
   <arg value="-D${db.main.database}"/>
  </exec>
 </fl:for>

</project>
Rebse
  • 10,307
  • 2
  • 38
  • 66