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