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