Gradle脚本:ant.copy任务

时间:2019-07-06 22:24:25

标签: gradle groovy ant

我有一个Gradle脚本,在其中需要将一堆文件从srcPath复制到tgtPath,同时指定包含和排除模式。

通常,我将使用Gradle的内置复制任务来执行此操作,但是在这里,我还需要同时转换字符编码。因此,我尝试使用ant.copy方法,因为它支持“ encoding”和“ outputEncoding”参数,它们应该完全支持这种转换。

所以我给我定义了一种方法,如下:

private void copy(String srcPath, String tgtPath, includePatterns = ['**/*'], excludePatterns = []) {
    println "copying from '${srcPath}' to '${tgtPath}' (incl:'${includePatterns}' / excl:'${excludePatterns}'):"

    new AntBuilder().copy(todir: tgtPath, 
                          encoding: StandardCharsets.ISO_8859_1, 
                          outputEncoding: StandardCharsets.UTF_8) {
        fileset(dir: srcPath, 
                includes: includePatterns, 
                excludes: excludePatterns)
    }
}

当我将其作为Gradle构建的一部分执行时(实际上是在.groovy文件中,这就是为什么我在这里使用“ new AntBuilder.copy(...)”而不是仅使用“ ant.copy(...)”的原因“我收到以下异常:

  

java.lang.ClassCastException:org.apache.xerces.parsers.XIncludeAwareParserConfiguration无法转换为org.apache.xerces.xni.parser.XMLParserConfiguration

???这与XML解析有什么关系?我根本不明白为什么会收到此错误。 我仅从Groovy中找到了几个示例,但没有有关如何使用此方法的真实文档。 Ant的文档声称“包含”和“排除”接受参数列表,这就是我在此处传递的内容。任何想法,我在这里做错了什么,或者为什么这不起作用?

关于在Gradle中复制文件时如何转换字符编码的任何其他建议?

1 个答案:

答案 0 :(得分:1)

我建议您使用Gradle的built in ant integration

例如:

project.ant.copy(todir: tgtPath, 
                          encoding: StandardCharsets.ISO_8859_1, 
                          outputEncoding: StandardCharsets.UTF_8) {
        fileset(dir: srcPath, 
                includes: includePatterns, 
                excludes: excludePatterns)
    }
相关问题