View Javadoc

1   package org.apache.maven.plugin.changes;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugin.changes.schema.ChangesSchemaValidator;
29  import org.apache.maven.plugin.changes.schema.SchemaValidatorException;
30  import org.apache.maven.plugin.changes.schema.XmlValidationHandler;
31  import org.apache.maven.plugins.annotations.Component;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.xml.sax.SAXParseException;
35  
36  /**
37   * 
38   * Goal which validate the <code>changes.xml</code> file.
39   * 
40   * @author <a href="mailto:olamy@apache.org">olamy</a>
41   * @version $Id: ChangesValidatorMojo.java 1355880 2012-07-01 13:13:15Z olamy $
42   * @since 2.1
43   */
44  @Mojo( name = "changes-validate", threadSafe = true )
45  public class ChangesValidatorMojo
46      extends AbstractMojo
47  {
48  
49      /**
50       */
51      @Component( role = ChangesSchemaValidator.class, hint = "default" )
52      private ChangesSchemaValidator changesSchemaValidator;
53  
54      /**
55       * The changes xsd version.
56       */
57      @Parameter( property = "changes.xsdVersion", defaultValue = "1.0.0" )
58      private String changesXsdVersion;
59  
60      /**
61       * Mojo failure if validation failed. If not and validation failed only a warning will be logged.
62       */
63      @Parameter( property = "changes.validate.failed", defaultValue = "false" )
64      private boolean failOnError;
65  
66      /**
67       * The path of the <code>changes.xml</code> file that will be converted into an HTML report.
68       */
69      @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
70      private File xmlPath;
71  
72      /** 
73       * @see org.apache.maven.plugin.Mojo#execute()
74       */
75      public void execute()
76          throws MojoExecutionException, MojoFailureException
77      {
78  
79          if ( !xmlPath.exists() )
80          {
81              getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." );
82              return;
83          }
84  
85          try
86          {
87              XmlValidationHandler xmlValidationHandler = changesSchemaValidator
88                  .validateXmlWithSchema( xmlPath, changesXsdVersion, failOnError );
89              boolean hasErrors = !xmlValidationHandler.getErrors().isEmpty();
90              if ( hasErrors )
91              {
92                  logSchemaValidation( xmlValidationHandler.getErrors() );
93                  if ( failOnError )
94                  {
95                      throw new MojoExecutionException( "changes.xml file " + xmlPath.getAbsolutePath()
96                          + " is not valid, see previous errors." );
97                  }
98                  else
99                  {
100                     getLog().info( " skip previous validation errors due to failOnError=false." );
101                 }
102             }
103         }
104         catch ( SchemaValidatorException e )
105         {
106             if ( failOnError )
107             {
108                 throw new MojoExecutionException( "failed to validate changes.xml file " + xmlPath.getAbsolutePath()
109                     + ": " + e.getMessage(), e );
110             }
111         }
112     }
113 
114     private void logSchemaValidation( List<SAXParseException> errors )
115     {
116         getLog().warn( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() );
117         getLog().warn( "validation errors: " );
118         for ( SAXParseException error : errors )
119         {
120             getLog().warn( error.getMessage() );
121         }
122     }
123 
124 }