View Javadoc

1   package org.apache.maven.scm.plugin;
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 org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.scm.ChangeSet;
26  import org.apache.maven.scm.ScmBranch;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmVersion;
29  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
30  import org.apache.maven.scm.command.changelog.ChangeLogSet;
31  import org.apache.maven.scm.provider.ScmProvider;
32  import org.apache.maven.scm.repository.ScmRepository;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import java.io.IOException;
36  import java.text.ParseException;
37  import java.text.SimpleDateFormat;
38  import java.util.Date;
39  
40  /**
41   * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
42   *
43   * @author <a href="dantran@gmail.com">Dan Tran</a>
44   * @author Olivier Lamy
45   */
46  @Mojo( name = "changelog", aggregator = true )
47  public class ChangeLogMojo
48      extends AbstractScmMojo
49  {
50      private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
51  
52      /**
53       * Start Date.
54       */
55      @Parameter( property = "startDate" )
56      private String startDate;
57  
58      /**
59       * End Date.
60       */
61      @Parameter( property = "endDate" )
62      private String endDate;
63  
64      /**
65       * Start Scm Version.
66       */
67      @Parameter( property = "startScmVersion" )
68      private String startScmVersion;
69  
70      /**
71       * End Scm Version.
72       */
73      @Parameter( property = "endScmVersion" )
74      private String endScmVersion;
75  
76      /**
77       * Start Scm Version Type.
78       */
79      @Parameter( property = "startScmVersionType" )
80      private String startScmVersionType;
81  
82      /**
83       * End Scm Version Type.
84       */
85      @Parameter( property = "endScmVersionType" )
86      private String endScmVersionType;
87  
88      /**
89       * Date Format in changelog output of scm tool.
90       */
91      @Parameter( property = "dateFormat" )
92      private String dateFormat;
93  
94      /**
95       * Date format to use for the specified startDate and/or endDate.
96       */
97      @Parameter( property = "userDateFormat", defaultValue = "yyyy-MM-dd" )
98      private String userDateFormat = DEFAULT_DATE_FORMAT;
99  
100     /**
101      * The version type (branch/tag) of scmVersion.
102      */
103     @Parameter( property = "scmVersionType" )
104     private String scmVersionType;
105 
106     /**
107      * The version (revision number/branch name/tag name).
108      */
109     @Parameter( property = "scmVersion" )
110     private String scmVersion;
111 
112     /**
113      * {@inheritDoc}
114      */
115     public void execute()
116         throws MojoExecutionException
117     {
118         super.execute();
119 
120         SimpleDateFormat localFormat = new SimpleDateFormat( userDateFormat );
121 
122         try
123         {
124             ScmRepository repository = getScmRepository();
125 
126             ScmProvider provider = getScmManager().getProviderByRepository( repository );
127 
128             ScmVersion startRev =
129                 getScmVersion( StringUtils.isEmpty( startScmVersionType ) ? "revision" : startScmVersionType,
130                                startScmVersion );
131             ScmVersion endRev =
132                 getScmVersion( StringUtils.isEmpty( endScmVersionType ) ? "revision" : endScmVersionType,
133                                endScmVersion );
134 
135             ChangeLogScmResult result;
136             if ( startRev != null || endRev != null )
137             {
138                 result = provider.changeLog( repository, getFileSet(), startRev, endRev, dateFormat );
139             }
140             else
141             {
142                 result = provider.changeLog( repository, getFileSet(), this.parseDate( localFormat, this.startDate ),
143                                              this.parseDate( localFormat, this.endDate ), 0,
144                                              (ScmBranch) getScmVersion( scmVersionType, scmVersion ), dateFormat );
145             }
146             checkResult( result );
147 
148             ChangeLogSet changeLogSet = result.getChangeLog();
149 
150             for ( ChangeSet changeSet : changeLogSet.getChangeSets() )
151             {
152                 getLog().info( changeSet.toString() );
153             }
154 
155         }
156         catch ( IOException e )
157         {
158             throw new MojoExecutionException( "Cannot run changelog command : ", e );
159         }
160         catch ( ScmException e )
161         {
162             throw new MojoExecutionException( "Cannot run changelog command : ", e );
163         }
164     }
165 
166     /**
167      * Converts the localized date string pattern to date object.
168      *
169      * @return A date
170      */
171     private Date parseDate( SimpleDateFormat format, String date )
172         throws MojoExecutionException
173     {
174         if ( date == null || date.trim().length() == 0 )
175         {
176             return null;
177         }
178 
179         try
180         {
181             return format.parse( date.toString() );
182         }
183         catch ( ParseException e )
184         {
185             throw new MojoExecutionException( "Please use this date pattern: " + format.toLocalizedPattern().toString(),
186                                               e );
187         }
188     }
189 }