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.Locale;
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
56
57
58 @Parameter( property = "changes.releaseDateLocale" )
59 private String releaseDateLocale;
60
61
62
63
64 @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true )
65 private String version;
66
67
68
69
70 @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
71 private File xmlPath;
72
73
74
75
76
77
78 @Parameter( property = "changes.skipSnapshots", defaultValue = "false" )
79 private boolean skipSnapshots;
80
81 private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
82
83
84
85
86
87
88 public void execute()
89 throws MojoExecutionException
90 {
91
92 if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
93 {
94 getLog().info( "Skipping the changes check in this project because it's not the Execution Root" );
95 }
96 else
97 {
98 if ( this.version.endsWith( "-SNAPSHOT" ) && this.skipSnapshots )
99 {
100 getLog().info( "Skipping snapshot version '" + this.version + "'." );
101 }
102 else if ( xmlPath.exists() )
103 {
104 ChangesXML xml = new ChangesXML( xmlPath, getLog() );
105 ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
106 Release release =
107 releaseUtils.getLatestRelease( releaseUtils.convertReleaseList( xml.getReleaseList() ), version );
108
109 if ( !isValidDate( release.getDateRelease(), releaseDateFormat, releaseDateLocale ) )
110 {
111 throw new MojoExecutionException(
112 "The file " + xmlPath.getAbsolutePath() + " has an invalid release date." );
113
114 }
115 }
116 else
117 {
118 getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does not exist." );
119 }
120 }
121 }
122
123
124
125
126
127
128
129
130
131 protected static boolean isValidDate( String string, String pattern )
132 {
133 return isValidDate( string, pattern, null );
134 }
135
136
137
138
139
140
141
142
143
144
145 protected static boolean isValidDate( String string, String pattern, String locale )
146 {
147 if ( StringUtils.isEmpty( string ) )
148 {
149 return false;
150 }
151
152 if ( StringUtils.isEmpty( pattern ) )
153 {
154 return false;
155 }
156
157 try
158 {
159 Locale usedLocale = null;
160
161 if ( StringUtils.isEmpty( locale ) )
162 {
163
164
165 usedLocale = Locale.getDefault();
166 }
167 else
168 {
169
170 Locale[] locales = Locale.getAvailableLocales();
171 for ( int i = 0 ; i < locales.length ; i++ )
172 {
173 if ( locales[i].toString().equals( locale ) )
174 {
175 usedLocale = locales[i];
176 break;
177 }
178 }
179
180 if ( usedLocale == null )
181 {
182
183
184 usedLocale = Locale.getDefault();
185 }
186 }
187
188 SimpleDateFormat df = new SimpleDateFormat( pattern, usedLocale );
189 df.parse( string );
190 return true;
191 }
192 catch ( ParseException e )
193 {
194 return false;
195 }
196 }
197 }