是否有任何 gradle 插件来验证 XML?

Is there any gradle plugin to validate XML?

我在一个项目中工作,我们有 xml 个用于代码生成的文件,我们使用 gradle 来构建它。

我是 gradle 的新手,但我听说有很多 plugins 可以帮助完成日常任务,我想知道是否有一些插件可以 xml简单验证(缺少引号和括号)。

我想得到文件名和缺失列表作为结果。

PS 试图在 google 中搜索,但找不到类似的东西。

UPD 如果在不久的将来需要对 xml 文件(标签、参数)进行全面验证,我应该怎么做?

自己编写非常简单

class XmlValidate extends DefaultTask {
    @InputFiles
    private FileCollection xmlFiles

    @InputFile
    File xsd

    void xml(Object files) {
       FileCollection fc = project.files(files)
       this.xmlFiles = this.xmlFiles == null ?  fc : this.xmlFiles.add(fc)
    }

    @TaskAction
    public void validateXml() {
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        Validator validator = null
        if (xsd != null) {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            Schema schema = factory.newSchema(new StreamSource(xsd))
            validator = schema.newValidator()
        } 
        Set<File> failures = [] as Set
        xmlFiles.forEach {
            Document document = null
            try {
                document = parser.parse(it)
            } catch (Exception e) {
                logger.error("Error parsing $it", e) 
                failures << it
            } 
            if (document && validator) {
                try {
                    validator.validate(new DOMSource(document))
                } catch (Exception e) {
                    logger.error("Error validating $it", e) 
                    failures << it
                } 
            } 
        }
        if (failures) throw new BuildException("xml validation failures $failures") 
    } 
}

在build.gradle中的用法

task validateXml(type: XmlValidate) {
    xml ['foo.xml', 'bar.xml']
    xml fileTree(dir: 'src/main/resources/baz', include: '*.xml')
    xsd = file('path/to.xsd')
}