【问题标题】:Set one third-party JAR's classpath to include another one with Ant使用 Ant 设置一个第三方 JAR 的类路径以包含另一个
【发布时间】:2012-08-08 18:52:34
【问题描述】:

如何设置一个第三方 JAR 的类路径以在 Ant 中包含第二个第三方 JAR?

我需要这个来运行来自this site 的香草 Using Schematron with ANT 示例。 (我 99% 确定您不需要知道 Schematron 是什么来回答这个问题。) 该示例的说明位于页面顶部附近链接的简短 PDF 中。我将所有文件的内容直接从 PDF 复制粘贴到 Eclipse 中。

然后我从上述站点下载了ant-schematron-2010-04-14.jar,并从 SAXON SourceForge 页面获得了saxon9he.jar。我还更改了构建文件以匹配。有行号,没有 cmets,它看起来像这样:

09 <project name="schematron-ant-sample" default="validate">
14     <taskdef name="schematron"
15              classname="com.schematron.ant.SchematronTask"
16              classpath="lib/ant-schematron-2010-04-14.jar; lib/saxon9he.jar" />
22     <target name="validate" description="Test with a Fileset">
23         <schematron schema="sch/sample.sch" failonerror="false">
24             <fileset dir="xml" includes="*.xml" />
25         </schematron>
26     </target>
27 </project>

当我运行构建文件时,出现以下错误:

validate:

BUILD FAILED
C:\Users\gdawes\Documents\workspace\SchematronAntExample\build.xml:23: /
javax.xml.transform.TransformerFactoryConfigurationError: Provider /
net.sf.saxon.TransformerFactoryImpl not found

Total time: 265 milliseconds

如果 SAXON JAR 未列在 schematron 的类路径中,则会发生类似错误。我已经确认 TransformerFactoryImpl 存在于 SAXON JAR 中,但我不知道如何让 Schematron 代码识别它。我应该如何设置类路径? (或者,如果我错了,这不是类路径问题,我该如何让构建成功?)

【问题讨论】:

    标签: java ant classpath


    【解决方案1】:

    &lt;taskdef&gt; 中的classpath 应仅包含:lib/ant-schematron-2010-04-14.jar,类似于 Using Schematron for Ant by example 中提供的示例。

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="schematron-ant-sample" default="validate">
      <taskdef name="schematron"
          classname="com.schematron.ant.SchematronTask"
          classpath="lib/ant-schematron-2010-04-14.jar" />
      <target name="validate" description="Test with a Fileset">
        <schematron schema="sch/sample.sch" failonerror="false">
          <fileset dir="xml" includes="*.xml" />
        </schematron>
      </target>
    </project>
    

    除了更正&lt;taskdef&gt; 之外,将saxon9he.jar 放入Ant 库文件夹使net.sf.saxon.TransformerFactoryImpl 可用并解决错误(在Ubuntu Linux 上使用Ant 1.8.2 测试)。

    【讨论】:

    • @LordTorgamus,答案已更新,并添加了关于将 saxon9he.jar 放在 Ant 库文件夹中的注释,以便 net.sf.saxon.TransformerFactoryImpl 可用。
    • @LordTorgamus,此外,PDF Using Schematron for Ant by example 提供了一个示例,表明 &lt;taskdef&gt; 应该只包含 @ 的路径987654333@jar文件。
    • 你指的是{ANT_HOME}\lib吗?
    • 我认为 Ant 会自动拾取其 ${ant.home}/lib 目录中的条目,而 Eclipse 会等待您使用External Tools Configuration 窗口的Classpath 选项卡添加它们。跨度>
    • @davidfmatheson 啊哈!我的 Classpath 选项卡(通过 Run > External Tools > External Tools Configurations 访问)显示 Eclipse 认为 ANT_HOME 表示 C:\Users\gdawes\MyEclipse\Common\plugins\org.apache.ant_1.7.1\lib 的内容。将 SAXON JAR 添加到该位置可以在 Eclipse 中工作。
    【解决方案2】:

    如果我没记错的话,taskdef 上的类路径只是用于查找您尝试加载的类的类路径,而不是任务实际运行的类路径。尝试将所需的 jars 添加到 Eclipse 中 Ant 构建配置的 Classpath 选项卡的 User Entries 部分。

    【讨论】:

    • 做到了。将 SAXON JAR 放在 taskdef 的类路径中是不必要的,尽管无害。为了将来参考,可以通过右键单击构建文件并选择“运行方式 > 外部工具配置...”或运行 > 外部工具 > 外部工具配置来访问 Classpath 选项卡
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多