1 package org.apache.maven.plugin.changes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40
41
42
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
56
57 @Parameter( property = "changes.xsdVersion", defaultValue = "1.0.0" )
58 private String changesXsdVersion;
59
60
61
62
63 @Parameter( property = "changes.validate.failed", defaultValue = "false" )
64 private boolean failOnError;
65
66
67
68
69 @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
70 private File xmlPath;
71
72
73
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 }