3

我目前正在开发一个 Gradle 插件,该插件的 JAR 包含一个 XSL 文件,我想在xsltGradle 调用的 Ant 任务中使用该文件:

ant.xslt(in: reports.xml.destination,
         out: new File(reports.xml.destination.parent, basename + '.html')) {
    style {
         // From https://svn.apache.org/repos/asf/hive/trunk/checkstyle/checkstyle-noframes-sorted.xsl.
        javaresource(name: 'checkstyle-noframes-sorted.xsl')
    }
}

但是,尽管我已经checkstyle-noframes-sorted.xsl将插件 JAR 中的几乎每个目录都包含在内,但我仍然得到

Caused by: : stylesheet checkstyle-noframes-sorted.xsl doesn't exist.
    at org.apache.tools.ant.taskdefs.XSLTProcess.handleError(XSLTProcess.java:1413)

所以我猜要么 theclasspath要么 theloaderRef要么 两者都是错误的。在阅读了类似问题的几个 答案后,我尝试了几种组合,但它们都产生了相同的错误。

为了让 Gradle 插件发现嵌入到其 JAR 中的资源,我需要指定什么?

4

1 回答 1

0

它不能开箱即用的原因是我错过了ant.xslt调用被antBuilder.withClasspath(getCheckstyleClasspath()).execute进一步包裹在一个闭包中,该闭包显式地改变了类路径。所以最简单的解决方案是简单地ant.xslt离开那个封闭。

如果这适用于您的情况,另一种解决方案是将 XSL 文件加载到 Groovy 代码中的变量并将其作为字符串传递给 Ant:

def xsl = Checkstyle.getClassLoader().getResourceAsStream('checkstyle-noframes-sorted.xsl')
// ...
style {
   string(value: xsl.text)
}
于 2015-08-19T12:08:49.357 回答