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.List;
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 create a date.
48       */
49      @Parameter( property = "changes.releaseDateFormat", defaultValue = "yyyy-MM-dd" )
50      private String releaseDateFormat;
51  
52      /**
53       * Version of the artifact.
54       */
55      @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true )
56      private String version;
57  
58      /**
59       * The path of the <code>changes.xml</code> file that will be checked.
60       */
61      @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
62      private File xmlPath;
63  
64      /**
65       * Flag controlling snapshot processing. If set, versions ending with <code>-SNAPSHOT</code> won't be checked.
66       *
67       * @since 2.7
68       */
69      @Parameter( property = "changes.skipSnapshots", defaultValue = "false" )
70      private boolean skipSnapshots;
71  
72      private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
73  
74      /**
75       * Check that the latest release contains a valid release date.
76       *
77       * @throws MojoExecutionException
78       */
79      public void execute()
80          throws MojoExecutionException
81      {
82          // Run only at the execution root
83          if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
84          {
85              getLog().info( "Skipping the changes check in this project because it's not the Execution Root" );
86          }
87          else
88          {
89              if ( this.version.endsWith( "-SNAPSHOT" ) && this.skipSnapshots )
90              {
91                  getLog().info( "Skipping snapshot version '" + this.version + "'." );
92              }
93              else if ( xmlPath.exists() )
94              {
95                  ChangesXML xml = new ChangesXML( xmlPath, getLog() );
96                  ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
97                  Release release =
98                      releaseUtils.getLatestRelease( releaseUtils.convertReleaseList( xml.getReleaseList() ), version );
99  
100                 if ( !isValidDate( release.getDateRelease(), releaseDateFormat ) )
101                 {
102                     throw new MojoExecutionException(
103                         "The file " + xmlPath.getAbsolutePath() + " has an invalid release date." );
104 
105                 }
106             }
107             else
108             {
109                 getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does not exist." );
110             }
111         }
112     }
113 
114     /**
115      * Use the pattern to try to parse a Date from the given string.
116      *
117      * @param string A date as text
118      * @param pattern A pattern that can be used by {@link SimpleDateFormat}
119      * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code>
120      */
121     protected static boolean isValidDate( String string, String pattern )
122     {
123         if ( StringUtils.isEmpty( string ) )
124         {
125             return false;
126         }
127 
128         if ( StringUtils.isEmpty( pattern ) )
129         {
130             return false;
131         }
132 
133         try
134         {
135             SimpleDateFormat df = new SimpleDateFormat( pattern );
136             df.parse( string );
137             return true;
138         }
139         catch ( ParseException e )
140         {
141             return false;
142         }
143     }
144 }