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