Apache Ant/Store XML data

From Wikibooks, open books for an open world
Jump to: navigation, search

Motivation[edit]

You want to upload a file or a hierarchy of files into eXist.

Method[edit]

We will use the xdb:store function and demonstrate how to use its options to load subfolders.

Sample Code[edit]

Each build file must have four key components

  1. a reference to internal files on your hard drive (ideally in a properties file)
  2. a typedef for your Ant eXist extensions
  3. a path to tell it where to get the jar files
  4. a target to do the load
<project xmlns:xdb="http://exist-db.org/ant" default="upload-collection-to-exist">
 
    <!-- This is where I put my copy of the eXist trunk code -->
    <!-- It is the result of a subversion checkout from https://exist.svn.sourceforge.net/svnroot/exist/trunk -->
    <property name="exist-home" value="C:\ws\exist-trunk"/>
 
    <!-- this tells us where to find the key jar files relative to the ${exist-home} property -->
    <path id="classpath.core">
        <fileset dir="${exist-home}/lib/core">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${exist-home}/exist.jar"/>
        <pathelement path="${exist-home}/exist-optional.jar"/>
    </path>
 
    <typedef resource="org/exist/ant/antlib.xml" uri="http://exist-db.org/ant">
        <classpath refid="classpath.core"/>
    </typedef>
 
    <target name="upload-collection-to-exist">
        <echo message="Loading Documents to eXist."/>
        <xdb:store 
            uri="xmldb:exist://localhost:8080/xmlrpc/db/my-project"
            createcollection="true"
            createsubcollections="true"
            user="admin" password="">
            <fileset dir="C:\ws\my-project\trunk\db\my-project"> 
                <include name="**/*.*"/>
            </fileset>
        </xdb:store>
    </target>
 
</project>

Using a local.properties File to Load XML Data[edit]

The script above will work fine if you have a single use with one set of local files. But if you have many user each user may put their local files in a different location. If that is the case then you will want to isolate all local file references in a file called local.properties.

The following example is from the eXist documentation project for a server running on port 8080 with the context being set to be "/":

# Local Property file for eXist documentation project
#
# this file is loaded into the build.xml file using the <property file="local.properties"/>
# it contains any local references to your
# Properties on a Windows system
exist-home=C:\\ws\\exist-trunk
exist-docs=C:\\ws\\exist-docs
user=admin
password=
uri=xmldb:exist://localhost:8080/xmlrpc/db/apps/exist-docs
<project xmlns:xdb="http://exist-db.org/ant" default="upload-exist-docs-app" 
   name="eXist Load Example">
 
    <!-- this is where we set our exist-home, user, password and the place that we will load the docs -->
    <property file="local.properties"/>
 
    <!-- this tells us where to find the key jar files relative to the ${exist-home} property -->
    <path id="classpath.core">
        <fileset dir="${exist-home}/lib/core">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${exist-home}/exist.jar"/>
        <pathelement path="${exist-home}/exist-optional.jar"/>
    </path>
    <typedef resource="org/exist/ant/antlib.xml" uri="http://exist-db.org/ant">
        <classpath refid="classpath.core"/>
    </typedef>
 
    <!-- upload app -->
    <target name="upload-exist-docs-app">
        <echo message="Loading eXist documentation system to eXist."/>
        <xdb:store uri="${uri}" createcollection="true" 
                 createsubcollections="true" user="admin" password="">
            <fileset dir="${exist-docs}">
                <include name="**/*.*"/>
            </fileset>
        </xdb:store>
    </target>
 
    <target name="show-properties">
        <echo message="exist-home=${exist-home}"/>
        <echo message="exist-docs=${exist-docs}"/>
        <echo message="uri=${uri}"/>
    </target>
</project>

References[edit]

The eXist store task is documented here: http://exist-db.org/exist/apps/doc/ant-tasks.xml#D2.2.10