Update 2011-09-16: a better solution now is the Missing Link http task, which provides a one-stop full-featured http implementation for Ant.

Erik Hatcher, Steve Loughran and Thorsten Scherler on the solr-usr user list helped me to a method for posting batches of files into Solr from Ant. To start with, you need the ant-contrib package and the Jakarta commons-codec package; put them on Ant’s classpath, most conveniently in Ant’s lib directory.

Then, in your build.xml, declare the ant-contrib tasks:

<taskdef resource="net/sf/antcontrib/antlib.xml"/>

Now you’re ready to post some files. We use the foreach task to iterate through a bunch of files, and the postMethod task to send them up.

<target name="update" description="update all solr files">
  <foreach target="dopost" param="filename">
      <fileset dir="path/to/records">
        <include name="*.xml"/>
<target name="dopost" description="do post">
  <postMethod url="http://localhost:8983/solr/update">
    <!-- note: filename contains absolute path -->
    <file path="${filename}" contentType="text/xml;charset=utf-8"/>

Run ant update to send all the files up to Solr (or to another REST-oriented service). The update task passes each file to the dopost task in the filename parameter.

Why not set up targets to commit and optimize as well:

<target name="commit" description="commit">
  <postmethod url="http://localhost:8983/solr/update">
    <text value="<commit/>"/>
<target name="optimize" description="optimize">
  <postmethod url="http://localhost:8983/solr/update">
    <text value="<optimize/>"/>

This will come in very handy in my Ant-based approach to large xslt projects.