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.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.List;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugins.annotations.Mojo;
30 import org.apache.maven.plugins.annotations.Parameter;
31 import org.apache.maven.plugins.changes.model.Release;
32
33
34
35
36
37
38
39
40
41 @Mojo( name = "changes-check", threadSafe = true )
42 public class ChangesCheckMojo
43 extends AbstractChangesMojo
44 {
45
46
47
48
49 @Parameter( property = "changes.releaseDateFormat", defaultValue = "yyyy-MM-dd" )
50 private String releaseDateFormat;
51
52
53
54
55 @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true )
56 private String version;
57
58
59
60
61 @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
62 private File xmlPath;
63
64
65
66
67
68
69 @Parameter( property = "changes.skipSnapshots", defaultValue = "false" )
70 private boolean skipSnapshots;
71
72 private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
73
74
75
76
77
78
79 public void execute()
80 throws MojoExecutionException
81 {
82
83 if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
84 {
85 getLog().info( "Skipping the changes check in this project because it's not the Execution Root" );
86 }
87 else
88 {
89 if ( this.version.endsWith( "-SNAPSHOT" ) && this.skipSnapshots )
90 {
91 getLog().info( "Skipping snapshot version '" + this.version + "'." );
92 }
93 else if ( xmlPath.exists() )
94 {
95 ChangesXML xml = new ChangesXML( xmlPath, getLog() );
96 ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
97 Release release =
98 releaseUtils.getLatestRelease( releaseUtils.convertReleaseList( xml.getReleaseList() ), version );
99
100 if ( !isValidDate( release.getDateRelease(), releaseDateFormat ) )
101 {
102 throw new MojoExecutionException(
103 "The file " + xmlPath.getAbsolutePath() + " has an invalid release date." );
104
105 }
106 }
107 else
108 {
109 getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does not exist." );
110 }
111 }
112 }
113
114
115
116
117
118
119
120
121 protected static boolean isValidDate( String string, String pattern )
122 {
123 if ( StringUtils.isEmpty( string ) )
124 {
125 return false;
126 }
127
128 if ( StringUtils.isEmpty( pattern ) )
129 {
130 return false;
131 }
132
133 try
134 {
135 SimpleDateFormat df = new SimpleDateFormat( pattern );
136 df.parse( string );
137 return true;
138 }
139 catch ( ParseException e )
140 {
141 return false;
142 }
143 }
144 }