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.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
38
39
40
41
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
55
56 @Parameter( property = "changes.xsdVersion", defaultValue = "1.0.0" )
57 private String changesXsdVersion;
58
59
60
61
62 @Parameter( property = "changes.validate.failed", defaultValue = "false" )
63 private boolean failOnError;
64
65
66
67
68 @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
69 private File xmlPath;
70
71
72
73
74 public void execute()
75 throws MojoExecutionException, MojoFailureException
76 {
77
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 }