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 | 3 | 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 | 3 | if ( !xmlPath.exists() ) |
80 | |
{ |
81 | 0 | getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." ); |
82 | 0 | return; |
83 | |
} |
84 | |
|
85 | |
try |
86 | |
{ |
87 | 3 | XmlValidationHandler xmlValidationHandler = changesSchemaValidator |
88 | |
.validateXmlWithSchema( xmlPath, changesXsdVersion, failOnError ); |
89 | 2 | boolean hasErrors = !xmlValidationHandler.getErrors().isEmpty(); |
90 | 2 | if ( hasErrors ) |
91 | |
{ |
92 | 1 | logSchemaValidation( xmlValidationHandler.getErrors() ); |
93 | 1 | if ( failOnError ) |
94 | |
{ |
95 | 0 | throw new MojoExecutionException( "changes.xml file " + xmlPath.getAbsolutePath() |
96 | |
+ " is not valid, see previous errors." ); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 1 | getLog().info( " skip previous validation errors due to failOnError=false." ); |
101 | |
} |
102 | |
} |
103 | |
} |
104 | 1 | catch ( SchemaValidatorException e ) |
105 | |
{ |
106 | 1 | if ( failOnError ) |
107 | |
{ |
108 | 1 | throw new MojoExecutionException( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() |
109 | |
+ ": " + e.getMessage(), e ); |
110 | |
} |
111 | 2 | } |
112 | 2 | } |
113 | |
|
114 | |
private void logSchemaValidation( List<SAXParseException> errors ) |
115 | |
{ |
116 | 1 | getLog().warn( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() ); |
117 | 1 | getLog().warn( "validation errors: " ); |
118 | 1 | for ( SAXParseException error : errors ) |
119 | |
{ |
120 | 1 | getLog().warn( error.getMessage() ); |
121 | |
} |
122 | 1 | } |
123 | |
|
124 | |
} |