2009-10-06 127 views

回答

4

xml-maven-plugin的validate goal將檢查格式是否正確,並根據模式進行驗證。如果驗證失敗,構建將失敗。

該插件不會生成任何報告,您希望報告中出於什麼興趣?有關無效文件的信息?

下面是一個例子用法:

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

是的,驗證錯誤報告將幫助指出xml文件(如PMD或Findbug爲java代碼執行)中的可能錯誤。我注意到許多開發人員在處理項目時跳過了xml或jsp驗證錯誤/警告,這可能會導致潛在的運行時錯誤。 – cetnar 2009-10-06 21:06:53

+0

同意。對構建失敗並不有用,但是失敗構建並打印架構驗證錯誤......現在這是非常有用的。尤其是當你需要調試某些東西時只使用IDE。 – avgvstvs 2015-11-10 16:07:19

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> 

當然,這並不真正適合在工作的行家方式,但它的工作對我來說,也許別人是通過了解此選項幫助。