8

如何在 Maven 构建期间针对 DTD 或 XSD Schema 验证 XML 文档?

4

3 回答 3

5

xml-maven-plugin的验证目标将检查格式是否正确,并可选择根据模式进行验证。如果验证失败,构建将失败。

该插件不生成任何报告,出于兴趣,您希望在报告中包含什么?有关无效文件的信息?

这是一个示例用法:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>validate</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <validationSets>
        <validationSet>
          <dir>src/main/xml</dir>
          <systemId>src/main/xmlschema.xsd</systemId>
        </validationSet>
      </validationSets>
    </configuration>
  </plugin>
于 2009-10-06T20:23:13.263 回答
3

有一个xml-maven-plugin可以检查 XML 文件是否与 XML 模式匹配,但我认为它不能生成报告。

于 2009-10-06T20:15:16.430 回答
0

我已经使用xml-maven-plugin有一段时间了(感谢Pascal ThiventRick Seller向我介绍这个),但是遇到了一些问题。

我正在验证一个 XML 文档。在某些时候,我们将 XML 文档拆分为两个文件,都在它们自己的子目录中。那时 xml-maven-plugin 不再验证任何东西,因为文件被移动了,但也没有抱怨它。我个人也发现配置不太直观,如果不符合您的预期,调试起来有点困难。

所以对我来说,我很高兴重新发现schemavalidate Ant 任务与maven-antrun-plugin相结合。做了我需要的一切以及更多。

在下面的示例中,我检查文件是否被实际选中。当然,您可以根据您的特定需求进行定制。作为奖励(有点题外话)我如何获取作为依赖项下载的 xsd 路径的示例。

<build>
    <plugins>
        <plugin><groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId><version>1.8</version>
            <executions><execution>
                <id>validate-xml-document-files-against-schema</id>
                <phase>test</phase>
                <goals><goal>run</goal></goals>
                <configuration>
                    <target>
                        <copy file="${maven.dependency.com.mycompany.some-schema.xsd.path}" tofile="${xml-validation-dir}/some-schema.xsd" />
                        <resourcecount property="xml.count">
                            <fileset dir="${xml-validation-dir}" includes="**/*.xml" />
                        </resourcecount>
                        <fail message="fileset does not match any xml file (use same fileset for actual validation)">
                            <condition><equals arg1="${xml.count}" arg2="0" /></condition>
                        </fail>
                        <echo message="validating ${xml.count} xml files against some-schema" />
                        <schemavalidate>
                            <schema namespace="http://mycompany.com/some-namespace" file="${xml-validation-dir}/some-schema.xsd" />
                            <fileset dir="${xml-validation-dir}" includes="**/*.xml" />
                        </schemavalidate>
                        <echo message="all ${xml.count} xml documents are valid" />
                    </target>
                </configuration>
            </execution></executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>some-schema-artifact</artifactId>
        <version>1.2.3</version>
        <type>xsd</type>
    </dependency>
</dependencies>

当然,这并不真正适合 maven 的工作方式,但它对我有用,也许知道这个选项会帮助其他人。

于 2016-02-02T10:57:15.193 回答