1 package org.apache.maven.plugin.changes;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugins.changes.model.Release;
30
31 /**
32 * Goal which checks that the changes.xml file has the necessary data to
33 * generate an announcement or a report for the current release.
34 *
35 * @goal changes-check
36 * @author Justin Edelson
37 * @author Dennis Lundberg
38 * @since 2.4
39 * @threadSafe
40 */
41 public class ChangesCheckMojo extends AbstractMojo
42 {
43 /**
44 * The format that a correct release date should have. This value will be
45 * used as a pattern to try to create a date.
46 *
47 * @parameter expression="${changes.releaseDateFormat}" default-value="yyyy-MM-dd"
48 */
49 private String releaseDateFormat;
50
51 /**
52 * Version of the artifact.
53 *
54 * @parameter expression="${changes.version}" default-value="${project.version}"
55 * @required
56 */
57 private String version;
58
59 /**
60 * The path of the <code>changes.xml</code> file that will be checked.
61 *
62 * @parameter expression="${changes.xmlPath}" default-value="src/changes/changes.xml"
63 */
64 private File xmlPath;
65
66 private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
67
68 /**
69 * Check that the latest release contains a valid release date.
70 *
71 * @throws MojoExecutionException
72 */
73 public void execute()
74 throws MojoExecutionException
75 {
76 if ( xmlPath.exists() )
77 {
78 ChangesXML xml = new ChangesXML( xmlPath, getLog() );
79 Release release = releaseUtils.getLatestRelease( xml.getReleaseList(), version );
80 if ( !isValidDate( release.getDateRelease(), releaseDateFormat ) )
81 {
82 throw new MojoExecutionException(
83 "The file " + xmlPath.getAbsolutePath() + " has an invalid release date." );
84 }
85 }
86 else
87 {
88 getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does not exist." );
89 }
90 }
91
92 /**
93 * Use the pattern to try to parse a Date from the given string.
94 *
95 * @param string A date as text
96 * @param pattern A pattern that can be used by {@link SimpleDateFormat}
97 * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code>
98 */
99 protected static boolean isValidDate( String string, String pattern )
100 {
101 if ( StringUtils.isEmpty( string ) )
102 {
103 return false;
104 }
105
106 if ( StringUtils.isEmpty( pattern ) )
107 {
108 return false;
109 }
110
111 try
112 {
113 SimpleDateFormat df = new SimpleDateFormat( pattern );
114 df.parse( string );
115 return true;
116 }
117 catch ( ParseException e )
118 {
119 return false;
120 }
121 }
122 }