View Javadoc
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  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   * Goal which checks that the changes.xml file has the necessary data to
35   * generate an announcement or a report for the current release.
36   *
37   * @author Justin Edelson
38   * @author Dennis Lundberg
39   * @since 2.4
40   */
41  @Mojo( name = "changes-check", threadSafe = true )
42  public class ChangesCheckMojo
43      extends AbstractChangesMojo
44  {
45      /**
46       * The format that a correct release date should have. This value will be
47       * used as a pattern to try to parse a date.
48       */
49      @Parameter( property = "changes.releaseDateFormat", defaultValue = "yyyy-MM-dd" )
50      private String releaseDateFormat;
51  
52      /**
53       * The locale that a correct release date should have. This value will be
54       * used as a locale to try to parse a date.
55       *
56       * @since 2.10
57       */
58      @Parameter( property = "changes.releaseDateLocale" )
59      private String releaseDateLocale;
60  
61      /**
62       * Version of the artifact.
63       */
64      @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true )
65      private String version;
66  
67      /**
68       * The path of the <code>changes.xml</code> file that will be checked.
69       */
70      @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
71      private File xmlPath;
72  
73      /**
74       * Flag controlling snapshot processing. If set, versions ending with <code>-SNAPSHOT</code> won't be checked.
75       *
76       * @since 2.7
77       */
78      @Parameter( property = "changes.skipSnapshots", defaultValue = "false" )
79      private boolean skipSnapshots;
80  
81      private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
82  
83      /**
84       * Check that the latest release contains a valid release date.
85       *
86       * @throws MojoExecutionException
87       */
88      public void execute()
89          throws MojoExecutionException
90      {
91          // Run only at the execution root
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      * Use the pattern to try to parse a Date from the given string. Kept for
125      * backward compatibility with existing unit tests.
126      *
127      * @param string A date as text
128      * @param pattern A pattern that can be used by {@link SimpleDateFormat}
129      * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code>
130      */
131     protected static boolean isValidDate( String string, String pattern )
132     {
133         return isValidDate( string, pattern, null );
134     }
135 
136     /**
137      * Use the pattern to try to parse a Date from the given string using the
138      * given Locale.
139      *
140      * @param string A date as text
141      * @param pattern A pattern that can be used by {@link SimpleDateFormat}
142      * @param locale A locale that can be used by {@link SimpleDateFormat}
143      * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code>
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                 // No locale specified, use the default locale as default value
164                 // The same behavior as before the locale parameter was added
165                 usedLocale = Locale.getDefault();
166             }
167             else
168             {
169                 // Try to find the specified locale on this system
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                     // The use specified locale was not found on this system,
183                     // use the default locale as default value
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 }