跳轉到內容

Apache Ant/建立 .xar 檔案

來自 Wikibooks,開放世界中的開放書籍

這個例子正在開發中!

您想直接從您的原始碼中建立一個 XML 歸檔檔案(.xar 檔案),該檔案可用於將庫模組或應用程式載入到本機 XML 資料庫中。這使得使用者更容易安裝您的模組或應用程式。打包過程會將您的檔案上傳到正在執行的 eXist 伺服器上的正確位置,並自動設定所有 XQuery 檔案(.xq)的許可權。

我們需要建立一個包含所有正確元件的“zip”檔案。

包的格式如下

http://expath.org/spec/pkg

eXist 特定包文件位於此處

http://demo.exist-db.org/exist/apps/doc/repo.xml

GUI 包 vs. 磁碟庫 vs. 資料庫庫

[編輯 | 編輯原始碼]

有三種類型的安裝包

  1. 不在資料庫中的外部庫
  2. 載入到資料庫中的庫
  3. 具有 GUI 的完整應用程式

對於所有沒有 GUI 但部署到資料庫中的庫應用程式,您必須使用兩個屬性,一個用於目標,型別為“library”,使用以下結構

 target="some /db path" + type="library"

對於僅需要在 eXist 中註冊但未部署在 exist 資料庫中的簡單 XQuery 庫包,不應使用 target 屬性。

 no target + type="library"

示例包結構

[編輯 | 編輯原始碼]

歸檔檔案必須在根目錄中包含兩個 XML 描述符檔案:expath-pkg.xml 和 repo.xml

示例 expath-pkg.xml 檔案

<package xmlns="http://expath.org/ns/pkg" name="http://example.com/apps/myapp" 
         abbrev="myapp" version="0.1" spec="1.0">
    <title>My Cool Application</title>
    <dependency package="http://exist-db.org/apps/xsltforms"/>
</package>

請注意,檔名和名稱空間中的字串是“pkg”,但元素名稱和依賴項中的屬性是“package”。確保將這些內容區分開。

此 XML 檔案的格式在 EXPath 文件中描述。

包含 eXist 特定打包說明的示例 repo.xml 檔案

<meta xmlns="http://exist-db.org/xquery/repo">
    <description>My eXist application</description>
    <author>Dan McCreary</author>
    <website>http://danmccreary.com</website>
    <status>alpha</status>
    <license>GNU-LGPL</license>
    <copyright>true</copyright>
    <!-- set this to "application" (without quotes) for system that have a GUI -->
    <type>application</type>
    <target>myapp</target>
    <prepare>pre-install.xql</prepare>
    <finish>post-install.xql</finish>
    <permissions user="admin" password="" group="dba" mode="rw-rw-r--"/>
    <!-- this element is automatically added by the deployment tool -->
    <deployed>2012-11-28T23:15:39.646+01:00</deployed>
</meta>

生成應用程式 .xar 檔案的示例 Apache Ant 目標

[編輯 | 編輯原始碼]

此 ant 目標需要以下輸入

 source-dir - the place you keep your source code
 package-dir - a temp dir such as /tmp/my-package to store temporary files
 app-name - the name of your application
 app-version - the version of your application
  1. 驗證 repo.xml 和 expath-package.xml 是否存在於源目錄中,並將它們複製到 temp.dir
  2. 將所有應用程式檔案複製到 temp.dir
  3. 從 temp.dir 中的內容建立 zip 檔案,位於 packages 區域,如果需要,將其上傳到儲存庫
<target name="generate-app-xar" description="Generate Application xar archive file">
   <echo>Making Package for ${app-name} use source from ${source-dir}</echo>
   <zip destfile="${package-dir}/${app-name}-${app-version}.xar">
         <fileset dir="${source-dir}">
            <include name="**/*.*" />
            <exclude name="**/.svn" />
      </fileset>
   </zip>
   <echo>Package is stored at ${package-dir}/${app-name}-${app-version}.xar</echo>
</target>

生成庫 .xar 檔案的示例 Apache Ant 目標

[編輯 | 編輯原始碼]

此指令碼依賴於以下 Ant 屬性

 ant.project.name - the name of the project
 xslt.dir - the directory that the XSLT script are stored
 temp.dir - a temp dir such as /tmp to store temporary files
 web.specs.dir - the place to put the results
<target name="generate-xar" description="Generate xar archive">
        <echo>Making ${ant.project.name}.xar...</echo>

        <!-- run a transform in the input specification file to create the a.xml file -->
        <xslt force="true" style="${xslt.dir}/generate-xar-descriptors.xsl" 
              in="${web.specs.dir}/${ant.project.name}/${ant.project.name}.xml" 
              out="${temp.dir}/files/a.xml">
            <param name="module-version" expression="${module-version}" />
            <param name="eXist-main-class-name" expression="${eXist-main-class-name}" />
        </xslt>
        <delete file="${temp.dir}/files/a.xml" />
        
        <!-- now create the .xar file with all our files in the right place -->
        <zip destfile="${temp.dir}/archives/${ant.project.name}-${module-version}.xar">
            <fileset dir="${temp.dir}/files">
                <include name="**/*.*" />
                <exclude name="*-tests.jar" />
            </fileset>
        </zip>
    </target>

示例 XSLT 指令碼

[編輯 | 編輯原始碼]
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

	<xsl:output method="xml" />

	<xsl:param name="module-version" />
	<xsl:param name="eXist-main-class-name" />

	<xsl:template match="/">
		<xsl:variable name="module-namespace">
			<xsl:copy-of select="//element()[@id = 'module-namespace']" />
		</xsl:variable>
		<xsl:variable name="module-prefix">
			<xsl:copy-of select="//element()[@id = 'module-prefix']" />
		</xsl:variable>
		<xsl:variable name="spec-title">
			<xsl:copy-of select="concat('EXPath ', //element()[local-name() = 'title'])" />
		</xsl:variable>
		<xsl:variable name="author">
			<xsl:copy-of select="//element()[local-name() = 'author'][1]/element()[1]" />
		</xsl:variable>

		<xsl:result-document href="target/files/expath-pkg.xml">
			<package xmlns="http://expath.org/ns/pkg" name="http://expath.org/lib/{$module-prefix}" abbrev="{concat('expath-', $module-prefix)}"
				version="{$module-version}" spec="1.0">
				<title>
					<xsl:value-of select="$spec-title" />
				</title>
				<dependency processor="http://exist-db.org/" />
			</package>
		</xsl:result-document>

		<xsl:result-document href="target/files/repo.xml">
			<meta xmlns="http://exist-db.org/xquery/repo">
				<description>
					<xsl:value-of select="$spec-title" />
				</description>
				<author>
					<xsl:value-of select="$author" />
				</author>
				<website />
				<status>stable</status>
				<license>GNU-LGPL</license>
				<copyright>true</copyright>
				<type>library</type>
			</meta>
		</xsl:result-document>

		<xsl:result-document href="target/files/exist.xml">
			<package xmlns="http://exist-db.org/ns/expath-pkg">
				<jar>
					<xsl:value-of select="concat('expath-', $module-prefix, '.jar')" />
				</jar>
				<java>
					<namespace>
						<xsl:value-of select="$module-namespace" />
					</namespace>
					<class>
						<xsl:value-of select="concat('org.expath.exist.', $eXist-main-class-name)" />
					</class>
				</java>
			</package>
		</xsl:result-document>

		<xsl:result-document href="target/files/cxan.xml">
			<package xmlns="http://cxan.org/ns/package" id="{concat('expath-', $module-prefix, '-exist')}" name="http://expath.org/lib/{$module-prefix}"
				version="{$module-version}">
				<author id="{$author/element()/@id}">
					<xsl:value-of select="$author" />
				</author>
				<category id="libs">Libraries</category>
				<category id="exist">eXist extensions</category>
				<tag>
					<xsl:value-of select="$module-prefix" />
				</tag>
				<tag>expath</tag>
				<tag>library</tag>
				<tag>exist</tag>
			</package>
		</xsl:result-document>

	</xsl:template>
</xsl:stylesheet>

示例 XQuery 指令碼

[編輯 | 編輯原始碼]

Apache Ant 目標和 XSLT 指令碼由 Claudius Teodorescu 提供。

華夏公益教科書