How can I validate XML documents during a Maven build against a DTD or a XSD Schema?
如何针对DTD或XSD模式在Maven构建期间验证XML文档?
3 个解决方案
#1
3
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.
有一个XML -maven-plugin可以检查XML文件是否与XML模式匹配,但我认为它不能生成报告。
#2
4
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.
xml-maven-plugin的验证目标将检查格式是否良好,并根据模式进行可选的验证。如果验证失败,构建将失败。
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/xsd</dir>
<systemId>src/main/xmlschema.xml</systemId>
</validationSet>
</validationSets>
</configuration>
</plugin>
#3
0
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.
我已经使用了xml-maven-plugin一段时间了(感谢Pascal Thivent和Rick Seller介绍我),但也有一些问题。
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.
我正在验证一个XML文档。在某些时候,我们将XML文档分成两个文件,都在各自的子目录中。此时,xml-maven-plugin不再验证任何内容,因为文件被移动了,但也没有抱怨。另外,我个人认为配置不是很直观,如果您不希望这样做,就很难调试。
So for me I was happy to rediscover the schemavalidate Ant task combined with the maven-antrun-plugin. Did everything I needed and more.
所以对我来说,我很高兴重新发现schemavalidate Ant任务与maven-antrun-plugin结合在一起。做了我需要的一切,甚至更多。
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.
在下面的示例中,我检查文件是否被实际选中。当然,您可以根据自己的具体需求来定制它。作为一个附加的例子(有点脱离主题),说明我如何获取作为依赖项下载的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>
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.
当然,这并不适合maven的工作方式,但是它对我很有用,也许其他人知道这个选项会有所帮助。
#1
3
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.
有一个XML -maven-plugin可以检查XML文件是否与XML模式匹配,但我认为它不能生成报告。
#2
4
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.
xml-maven-plugin的验证目标将检查格式是否良好,并根据模式进行可选的验证。如果验证失败,构建将失败。
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/xsd</dir>
<systemId>src/main/xmlschema.xml</systemId>
</validationSet>
</validationSets>
</configuration>
</plugin>
#3
0
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.
我已经使用了xml-maven-plugin一段时间了(感谢Pascal Thivent和Rick Seller介绍我),但也有一些问题。
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.
我正在验证一个XML文档。在某些时候,我们将XML文档分成两个文件,都在各自的子目录中。此时,xml-maven-plugin不再验证任何内容,因为文件被移动了,但也没有抱怨。另外,我个人认为配置不是很直观,如果您不希望这样做,就很难调试。
So for me I was happy to rediscover the schemavalidate Ant task combined with the maven-antrun-plugin. Did everything I needed and more.
所以对我来说,我很高兴重新发现schemavalidate Ant任务与maven-antrun-plugin结合在一起。做了我需要的一切,甚至更多。
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.
在下面的示例中,我检查文件是否被实际选中。当然,您可以根据自己的具体需求来定制它。作为一个附加的例子(有点脱离主题),说明我如何获取作为依赖项下载的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>
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.
当然,这并不适合maven的工作方式,但是它对我很有用,也许其他人知道这个选项会有所帮助。