跳轉到內容

Apache Ant/儲存 XML 資料

來自華夏公益教科書

您想將檔案或檔案層次結構上傳到 eXist。

我們將使用 xdb:store 函式,並演示如何使用其選項載入子資料夾。

示例程式碼

[編輯 | 編輯原始碼]

每個構建檔案必須有四個關鍵元件

  1. 對硬碟驅動器上內部檔案的引用(理想情況下在屬性檔案中)
  2. 您 Ant eXist 擴充套件的 typedef
  3. 一條路徑告訴它從哪裡獲取 jar 檔案
  4. 一個目標來執行載入
<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://: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>

使用 local.properties 檔案載入 XML 資料

[編輯 | 編輯原始碼]

如果您只使用一組本地檔案,上面的指令碼將正常工作。但是,如果您有多個使用者,每個使用者可能將本地檔案放在不同的位置。如果是這種情況,您需要將所有本地檔案引用隔離到一個名為 local.properties 的檔案中。

以下示例來自 eXist 文件專案,用於在埠 8080 上執行的伺服器,上下文設定為“/”

# 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://: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>

參考資料

[編輯 | 編輯原始碼]

eXist store 任務的文件在這裡:http://exist-db.org/exist/apps/doc/ant-tasks.xml#D2.2.10

華夏公益教科書