创建可再分发的打包标记文件

Creating a redistributable packaged tagfile

背景:

我有几个项目应该共享一个打包的标记文件(请参阅 Packaged tag files 部分)。在一个独立的项目中,我创建了这个打包的标记文件,它实际上只是一个 JAR 文件(请参阅下面的 'JAR file structure' 部分),我可以将其重新分发到我的所有项目中。我正在使用 Eclipse IDE。

问题:

根据 Oracle 文档,我应该 能够将我的 .tld 放入 JAR 文件的 /META-INF/ 目录中。但是,只有当 .tld 也在项目的 /WEB-INF/ 目录中并且我用 <%@taglib prefix="t" uri="/WEB-INF/tagfiles.tld" %> 引用它时,Eclipse 才会成功验证我的代码。如何在不将 .tld 放入 /WEB-INF/ 目录的情况下在 Eclipse 中引用我的标签?这是 Oracle 文档,说明它可以在 JAR 的 META-INF 中:

Tag library descriptor file names must have the extension .tld and must be packaged in the /WEB-INF/ directory or subdirectory of the WAR file or in the /META-INF/ directory or subdirectory of a tag library packaged in a JAR. If a tag is implemented as a tag file and is packaged in /WEB-INF/tags/ or a subdirectory, a TLD will be generated automatically by the web container, though you can provide one if you wish.

我不想将 .tld 放入 WEB-INF 的原因是如果它包含在 JAR 中则更容易分发。这样我就不必在每次进行更改时都将其重新复制到所有依赖项目中。

我的 JAR 文件结构:

我创建了一个名为 wrapper.tag 的 JSP 标签文件。我创建了另一个名为 tagfiles.tld 的文件,其中包含一个 tag-files 元素,该元素具有一个子元素 <path>/META-INF/tags/wrapper.tag</path>。我创建了一个 ant 任务,将这些 JAR。它创建一个包含以下内容的 JAR:META-INF、META-INF/tags、META-INF/tld、META-INF/MANIFEST.MF、tags/wrapper.tag 和 tld/tagfiles.tld .

成功了。我升级到 Eclipse Luna(在撰写本文时是最新版本)。我的主项目在包含标记文件 JAR 的项目的 Java 构建路径上有一个 'Projects' 引用。 JAR 本身具有我上面 post 中指示的文件结构。我的 .tld 文件包含以下内容:

<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
    <tlib-version>2.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>mysname</short-name>
    <uri>http://anyuri.com/anything</uri>
    <description>
     Your description here
    </description>
  <tag-file>
    <description>This is just a test</description>
    <display-name>wrapper</display-name>
    <name>wrapper</name>
    <path>/META-INF/tags/wrapper.tag</path>
  </tag-file>
</taglib>

我还应该注意到,我有一个 ANT 构建,它将标记文件 JAR 打包到 Web 应用程序的 WEB-INF/lib 目录中。根据 documentation:

这是必需的

Tag files can be packaged in the /META-INF/tags/ directory in a JAR file installed in the /WEB-INF/lib/ directory of the web application. Tags placed here are typically part of a reusable library of tags that can be used easily in any web application.