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.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugin.changes.schema.ChangesSchemaValidator;
28  import org.apache.maven.plugin.changes.schema.SchemaValidatorException;
29  import org.apache.maven.plugin.changes.schema.XmlValidationHandler;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.xml.sax.SAXParseException;
34  
35  /**
36   * Goal which validate the <code>changes.xml</code> file.
37   * 
38   * @author Olivier Lamy
39   * @version $Id: ChangesValidatorMojo.java 1685894 2015-06-16 19:29:09Z khmarbaise $
40   * @since 2.1
41   */
42  @Mojo( name = "changes-validate", threadSafe = true )
43  public class ChangesValidatorMojo
44      extends AbstractChangesMojo
45  {
46  
47      /**
48       */
49      @Component( role = ChangesSchemaValidator.class, hint = "default" )
50      private ChangesSchemaValidator changesSchemaValidator;
51  
52      /**
53       * The changes xsd version.
54       */
55      @Parameter( property = "changes.xsdVersion", defaultValue = "1.0.0" )
56      private String changesXsdVersion;
57  
58      /**
59       * Mojo failure if validation failed. If not and validation failed only a warning will be logged.
60       */
61      @Parameter( property = "changes.validate.failed", defaultValue = "false" )
62      private boolean failOnError;
63  
64      /**
65       * The path of the <code>changes.xml</code> file that will be converted into an HTML report.
66       */
67      @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
68      private File xmlPath;
69  
70      /**
71       * @see org.apache.maven.plugin.Mojo#execute()
72       */
73      public void execute()
74          throws MojoExecutionException, MojoFailureException
75      {
76          // Run only at the execution root
77          if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
78          {
79              getLog().info( "Skipping the changes validate in this project because it's not the Execution Root" );
80          }
81          else
82          {
83              if ( !xmlPath.exists() )
84              {
85                  getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." );
86                  return;
87              }
88  
89              try
90              {
91                  XmlValidationHandler xmlValidationHandler =
92                      changesSchemaValidator.validateXmlWithSchema( xmlPath, changesXsdVersion, failOnError );
93                  boolean hasErrors = !xmlValidationHandler.getErrors().isEmpty();
94                  if ( hasErrors )
95                  {
96                      logSchemaValidation( xmlValidationHandler.getErrors() );
97                      if ( failOnError )
98                      {
99                          throw new MojoExecutionException( "changes.xml file " + xmlPath.getAbsolutePath()
100                             + " is not valid, see previous errors." );
101                     }
102                     else
103                     {
104                         getLog().info( " skip previous validation errors due to failOnError=false." );
105                     }
106                 }
107             }
108             catch ( SchemaValidatorException e )
109             {
110                 if ( failOnError )
111                 {
112                     throw new MojoExecutionException( "failed to validate changes.xml file " + xmlPath.getAbsolutePath()
113                         + ": " + e.getMessage(), e );
114                 }
115             }
116         }
117     }
118 
119     private void logSchemaValidation( List<SAXParseException> errors )
120     {
121         getLog().warn( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() );
122         getLog().warn( "validation errors: " );
123         for ( SAXParseException error : errors )
124         {
125             getLog().warn( error.getMessage() );
126         }
127     }
128 
129 }