Posting XML from Ant « Quædam cuiusdam
Posting XML from Ant
Thursday 1 February 2007 @ 9:01 pm
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">
    <path>
      <fileset dir="path/to/records">
        <include name="*.xml"/>
      </fileset>
    </path>
  </foreach>
</target>
<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"/>
  </postMethod>
</target>

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/>"/>
  </postmethod>
</target>
<target name="optimize" description="optimize">
<postmethod url="http://localhost:8983/solr/update">
    <text value="<optimize/>"/>
  </postmethod>
</target>

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

Comments (1) - Posted in Search,XML by  



 One response to “Posting XML from Ant”

  •   Steve wrote:

    Apparently, ant-contrib has changed since your entry was posted.

    ant-contrib no longer has a postMethod task, although there is a post task, but it doesn’t allow nested file elements.

    Any idea how to make it work now?

Leave a comment