Ant Tasks
eXist-db provides a library for the Ant build tool to automate common system administration and
operation tasks, like backup/restore or importing files. The latest version of eXist-db's
Ant Tasks can be found on Maven Central as the artifact exist-ant.
Basic configuration
To prepare your Ant build script for working with eXist-db, do the
following:
Add the eXist-db
Anttasks namespace to your script, preferably on the root<project>element and with prefixxdb:<project xmlns:xdb="http://exist-db.org/ant" … >
Import the eXist-db tasks with Ivy and then add the following code:
<typedef resource="org/exist/ant/antlib.xml" uri="http://exist-db.org/ant"/>
Common attributes
All tasks share the following common attributes:
uriAn XMLDB URI specifying the database collection through XMLRPC. For instance
xmldb:exist://localhost:8080/exist/xmlrpc/db/my-collection-my-resource.xml.All collection and resource paths specified in the tasks are relative to this.
sslUse SSL encryption when communicating with the database (default:
false).userThe user to connect with (default:
guest).passwordPassword for the user (default:
guest).failonerrorWhether or not a error should stop the build execution (default:
true).
Storing Documents
The store task uploads and stores documents into the database. Documents
are specified as single source file or with Ant filesets:
<xdb:store xmlns:xdb="http://exist-db.org/ant"
uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays"
createcollection="true">
<fileset dir="samples/shakespeare">
<include name="*.xml"/>
<include name="*.xsl"/>
</fileset>
</xdb:store>
<xdb:store xmlns:xdb="http://exist-db.org/ant"
uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays"
createcollection="true" srcfile="samples/shakespeare/hamlet.xml"/>createcollectionIf set to
true, non-existing base collections are automatically created.createsubcollectionsIf set to
true, non-existing sub-collections are automatically created.typeThe type of the resource: either
xmlorbinary. Ifbinary, documents will be stored as binary resources. If unset, the type will be guessed from identified MIME type.defaultmimetypeThe default MIME type to use when the resource's MIME type cannot be identified. Default:
binary(application/octet-stream).forcemimetypeUse this attribute when you want to force the MIME type. Also set
typeattribute (because resource kind guessing is disabled in this mode).mimetypesfileThe
mime-types.xmlfile used to identify the resource's kind (binaryorxml) and MIME type. If not set eXist-db's default MIME types file will be used. An example MIME types file can be found in the eXist-db installation directory.srcfilea single source file to store. Use either this or
Antfilesets.permissionsThe permissions for the resource, expressed in a Unix-style form, e.g.
rwxr-xr-x. Permissions will be applied to the resource/collection after it is created.
Removing Documents/Collections
The remove task removes a single resource or a collection:
<xdb:remove xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays" resource="hamlet.xml"/><xdb:remove xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare" collection="plays"/>Attributes (use either collection or resource, not
both):
collectionThe name of the collection to remove.
resourceThe name of the resource which should be removed.
Creating Empty Collections
The create task creates a single, empty, collection:
<xdb:create xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare" collection="plays"/>collectionThe name of the collection to create.
permissionsThe permissions to be applied to the collection, expressed in a Unix-style form, e.g.
rwxr-xr-x. Permissions will be applied to the collection after it is created.
Check Existence of Resource/Collection
The exist task is a condition that checks whether a resource or
collection as specified in the uri attribute exists or not. An ant target can be
executed conditionally, depending on the property set or not set by the
condition.
<condition property="exists">
<xdb:exist xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays" resource="hamlet.xml"/>
</condition>resourceThe name of the resource to check. If you don't specify this, the existence of the collection in the
uriattribute is checked.
List Resources/Collections
The list task returns a list of resources and/or collections:
<xdb:list xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays" resources="true" outputproperty="resources"/><xdb:list xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare" collections="true" outputproperty="collections"/>Attributes:
resourcesIf
truereturns a list of resources.collectionsIf
truereturns a list of collections.separatorSeparator character for the returned list. Default: "
,".outputpropertyName of the (new)
Antproperty for the result
Copy a Resource/Collection
The copy task copies a resource or collection:
<xdb:copy xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays" resource="hamlet.xml" destination="sub"/><xdb:copy xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare" collection="plays" destination="sub"/>Attributes (use either collection or resource, not
both):
resourceTakes the name of the resource to copy.
collectionTakes the name of the collection to copy.
destinationTakes the name of the destination collection to copy to.
nameTakes the new name of the resource or collection.
Move a Resource/Collection
The move task moves a resource or collection:
<xdb:move xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays" resource="hamlet.xml" destination="sub"/><xdb:move xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare" collection="plays" destination="sub"/>Attributes (use either collection or resource, not
both):
resourceTakes the name of the resource to move.
collectionTakes the name of the collection to move.
destinationTakes the name of the destination collection to move to.
nameTakes the new name of the resource or collection.
Process an XPath Expression
The xpath task executes an XPath expression on a single or a collection
of XML documents.
<xdb:xpath xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db" query="/example-queries/query"/>Attributes:
queryTakes the XPath expression to process.
resourceRun the XPath query on this resource instead of a collection.
countIf
true, the number of results is returned (instead of the results themselves).outputpropertyName of the (new) property for returning the results of the query in.
destDirWrite the results of the query to this destination file. Care should be taken to get a well-formed document (one root tag!).
namespaceOptional XML namespace for the query.
If you don't specify an output property or destination file, the results are discarded.
Process an XQuery Expression
The xquery task executes an XQuery expression:
<loadfile property="xquery" srcFile="wzb.xq"/>
<xdb:xquery xmlns:xdb="http://exist-db.org/ant"
uri="xmldb:exist://localhost:8080/exist/xmlrpc/db" query="${xquery}"
user="guest" password="guest-passwd">
<variable name="alpha" value="aaa-alep" />
</xdb:xquery><xdb:xquery xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db" queryUri="http://www.example.com/query/xquery-task.xql" user="guest" password="guest-passwd">
<variable name="beta" value="ϐ"/>
</xdb:xquery>Attributes:
queryTakes the full query that should be processed.
queryUriTakes the query to process, specified as a URI.
outputpropertyName of the (new) property for returning the results of the query in.
destfileWrite the results of the query to this destination file. Care should be taken to get a well-formed document (one root tag!).
queryfileThe query to process, specified as a filename.
The XQuery may also be specified as text content of the element.
Passing variables to the XQuery script can be done using one or more nested
<variable> child elements. Declare these variables in your XQuery script as
external. For instance, a variable passed as <variable name="test"
value="Yup"/> can be accessed in the XQuery script as declare variable
$beta external;:
Extract a Resource/Collection
The extract task extracts XML resources or collections with XML resources
to disk. Extraction of binary resources is not supported.
<xdb:extract xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays" resource="hamlet.xml" destfile="/tmp/hamlet.xml"/><xdb:extract xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc/db/shakespeare/plays" destdir="/tmp" subcollections="true" createdirectories="true"/>Attributes:
resourceThe name of the resource to extract.
sub-collectionsIf
trueall sub-collections of the specified collection are extracted as well.destfileThe name of the destination file to extract to on extracting a resource.
destdirThe name of a destination directory to extract to on extracting a collection.
createdirectoriesIf
truedirectories for sub-collections will be created. Otherwise all extracted resources are written to the same destination directory.typeType of the resource to extract. Only
xmlis supported currently.overwriteSet to
trueto force overwriting of files.
Backup
The backup task creates a backup of the specified database collection on
disk:
<xdb:backup xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db/system" dir="${backup.dir}" user="${backup.user}" password="${backup.pass}"/>Attributes:
dirThe directory where the backup files will be stored.
Restore
The restore task restores database contents from a backup.
<xdb:restore xmlns:xdb="http://exist-db.org/ant" uri="xmldb:exist://localhost:8080/exist/xmlrpc" user="admin" dir="${backup.dir}/db/home"/>Attributes (use either dir or file, not both):
dirThe directory (containing a
__content__.xmlfile) to restore.fileA zip file which contains the backup to be restored.
List User Groups
The groups task lists all user groups defined in eXist-db:
<xdb:groups xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db/system" outputproperty="groups"/>Attributes:
outputpropertyName of (new) property to write the output to.
separatorSeparator character for the returned list. Default: "
,".
List Users
The users task lists all users defined in eXist-db:
<xdb:users xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db/system" outputproperty="users"/>Attributes:
outputpropertyName of (new) property to write the output to.
separatorSeparator character for the returned list. Default: "
,".
Lock Resource
The lock task locks a resource for a specific user:
<xdb:lock xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db/shakespeare/plays" resource="hamlet.xml" name="guest"/>Attributes:
resourceTakes the name of the resource to lock.
nameTakes the name of the user to lock the resource for.
Add User
The adduser task adds a user:
<xdb:adduser xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db" name="guest" secret="ToPSecreT" primaryGroup="users"/>Attributes:
nameTakes the username of the new user.
homeName of the new user's home collection.
secretTakes the password for the new user.
primaryGroupName of primary user group for the new user.
Remove User
The rmuser task removes a user:
<xdb:rmuser xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db" name="guest"/>Attributes:
nameTakes the username of the user to remove.
Change User Password
The password task changes a user's password.
<xdb:password xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db" user="dba-user" password="dba-user-SecreT" name="guest" secret="Guest-Changed-Secret"/>Attributes:
nameTakes the username of the user to change the password for.
secretTakes the new password for the user.
Add User Group
The addgroup task adds a user group.
<xdb:addgroup xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db" name="guest"/>Attributes:
nameTakes the groupname of the new group.
Remove User Group
The rmgroup task removes a user group.
<xdb:rmgroup xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db" name="guest"/>Attributes:
nameTakes the groupname of the group to remove.
Change Resource Permissions
The chmod task changes the permissions of a resource or
collection.
<xdb:chmod xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db/shakespear/plays" resource="hamlet.xml" mode="group=-write,other=-write"/>Attributes:
resourceName of the resource to modify. If no resource has been specified,
chmodwill operate on the collection specified by theuriattribute.permissionsPermission modification string. Use either Unix-style syntax like
rwxrwx---or additive/subtractive syntax like[user|group|other]=[+|-][read|write|execute], comma separated.For example, to set read and write permissions for the group, but not for others:
group=+read,+write,other=-read,-write
The new settings are OR-ed with the existing settings.
modeDEPRECATED: Permission modification string. Use
permissionsinstead.
Change Owner of resource/collection (chown)
The chown task changes the owner of a resource or collection.
<xdb:chown xmlns:xdb="http://exist-db.org/ant" uri="${backup.uri}/db/shakespeare/plays" resource="hamlet.xml" name="guest" group="guest"/>Attributes:
nameName of the user to own the resource/collection.
groupName of the user group to own the resource/collection.
Database Shutdown
The shutdown task will shut down the database instance listening at the
provided URI.
Example Ant script
This example Ant script shows how to copy data from two different instances of eXist-db (remote or local).
Supply your own values for the source and target user, pass,
and url properties. You can find this Ant script under the
$EXIST_HOME/exist-samples/src/main/resources/org/exist/samples/ant
directory.
<project xmlns:xdb="http://exist-db.org/ant" name="exist-data-migrate" default="migrate">
<description>
Migrate data from one instance of eXist-db to another
</description>
<!-- edit these properties // -->
<property name="p.exist.dir" value="/opt/eXist-1.0"/>
<property name="p.source.exist.url" value="xmldb:exist://www.example.org:8080/exist/xmlrpc/db/"/>
<property name="p.source.user" value="myusername"/>
<property name="p.source.pass" value="myuserpass"/>
<property name="p.target.exist.url" value="xmldb:exist://www.example.org:8680/exist/xmlrpc/db/"/>
<property name="p.target.user" value="myotherusername"/>
<property name="p.target.pass" value="myotheruserpass"/>
<property name="p.export.dir" location="export"/>
<!-- import eXist-db tasks -->
<typedef resource="org/exist/ant/antlib.xml" uri="http://exist-db.org/ant">
<classpath>
<fileset dir="${p.exist.dir}/lib/core">
<include name="*.jar"/>
</fileset>
<fileset dir="${p.exist.dir}/lib/endorsed">
<include name="*.jar"/>
</fileset>
<fileset dir="${p.exist.dir}/lib/optional">
<include name="*.jar"/>
</fileset>
<pathelement location="${p.exist.dir}/exist.jar"/>
<pathelement location="${p.exist.dir}/exist-optional.jar"/>
</classpath>
</typedef>
<target name="clean">
<delete dir="${p.export.dir}"/>
<mkdir dir="${p.export.dir}"/>
</target>
<target name="migrate" depends="extract-source, load-target">
<echo message="migration complete"/>
</target>
<target name="extract-source" depends="clean" description="export xml from source eXist-db instance">
<xdb:extract uri="${p.source.exist.url}" user="${p.source.user}" password="${p.source.pass}" destdir="${p.export.dir}"/>
</target>
<target name="load-target" description="store xml to target eXist-db instance">
<xdb:store uri="${p.target.exist.url}" user="${p.target.user}" password="${p.target.pass}" createcollection="true" createsubcollections="true">
<fileset dir="${p.export.dir}"/>
</xdb:store>
</target>
<target name="check-env" description="check env and dependencies are installed">
</target>
</project>