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.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   * Goal which checks that the changes.xml file has the necessary data to
36   * generate an announcement or a report for the current release.
37   *
38   * @author Justin Edelson
39   * @author Dennis Lundberg
40   * @since 2.4
41   */
42  @Mojo( name = "changes-check", threadSafe = true )
43  public class ChangesCheckMojo
44      extends AbstractMojo
45  {
46      /**
47       * The format that a correct release date should have. This value will be
48       * used as a pattern to try to create a date.
49       */
50      @Parameter( property = "changes.releaseDateFormat", defaultValue = "yyyy-MM-dd" )
51      private String releaseDateFormat;
52  
53      /**
54       * Version of the artifact.
55       */
56      @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true )
57      private String version;
58  
59      /**
60       * The path of the <code>changes.xml</code> file that will be checked.
61       */
62      @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
63      private File xmlPath;
64  
65      /**
66       * Flag controlling snapshot processing. If set, versions ending with <code>-SNAPSHOT</code> won't be checked.
67       *
68       * @since 2.7
69       */
70      @Parameter( property = "changes.skipSnapshots", defaultValue = "false" )
71      private boolean skipSnapshots;
72  
73      private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
74  
75      /**
76       * Check that the latest release contains a valid release date.
77       *
78       * @throws MojoExecutionException
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      * Use the pattern to try to parse a Date from the given string.
109      *
110      * @param string A date as text
111      * @param pattern A pattern that can be used by {@link SimpleDateFormat}
112      * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code>
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 }