开发者

XML DTD/Schema validation in Maven

开发者 https://www.devze.com 2022-12-08 01:02 出处:网络
How can I validate XML documents 开发者_高级运维during a Maven build against a DTD or a XSD Schema?The validate goal of the xml-maven-plugin will check for well-formedness and optionally validate agai

How can I validate XML documents 开发者_高级运维during a Maven build against a DTD or a XSD Schema?


The validate goal of the xml-maven-plugin will check for well-formedness and optionally validate against a schema. The build will fail if the validation fails.

The plugin does not produce any report, what would you want in a report out of interest? information about the invalid files?

Here is an example usage:

  <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>


There is a xml-maven-plugin that can check whether XML files are matching an XML schema but I don't think it can generate reports.


I have used the xml-maven-plugin for some time (thanks to Pascal Thivent and Rick Seller for introducing me to this), but had some problems with it.

I was validating an XML document. At some point we split the XML document in two files, both in their own subdirectory. At that point the xml-maven-plugin did not validate anything anymore because the file was moved, but also did not complain about it. Also personally I found the configuration not too intuitive and a bit hard to debug if it does not do you expect.

So for me I was happy to rediscover the schemavalidate Ant task combined with the maven-antrun-plugin. Did everything I needed and more.

In the example below I check that files are actually selected. Off course you can tailor this for your specific needs. As a bonus (tough a bit off topic) an example of how I grab the path of an xsd that is downloaded as a dependency.

<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>

Granted, this does not really fit in the maven way of working, but it worked for me and maybe somebody else is helped by knowing this option.

0

精彩评论

暂无评论...
验证码 换一张
取 消