View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.plugin;
20  
21  import java.io.IOException;
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Date;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.scm.ChangeSet;
30  import org.apache.maven.scm.ScmBranch;
31  import org.apache.maven.scm.ScmException;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
34  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
35  import org.apache.maven.scm.command.changelog.ChangeLogSet;
36  import org.apache.maven.scm.provider.ScmProvider;
37  import org.apache.maven.scm.repository.ScmRepository;
38  
39  /**
40   * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
41   *
42   * @author <a href="dantran@gmail.com">Dan Tran</a>
43   * @author Olivier Lamy
44   */
45  @Mojo(name = "changelog", aggregator = true)
46  public class ChangeLogMojo extends AbstractScmMojo {
47  
48      private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
49  
50      /**
51       * Start Date.
52       */
53      @Parameter(property = "startDate")
54      private String startDate;
55  
56      /**
57       * End Date.
58       */
59      @Parameter(property = "endDate")
60      private String endDate;
61  
62      /**
63       * Start Scm Version.
64       */
65      @Parameter(property = "startScmVersion")
66      private String startScmVersion;
67  
68      /**
69       * End Scm Version.
70       */
71      @Parameter(property = "endScmVersion")
72      private String endScmVersion;
73  
74      /**
75       * Start Scm Version Type.
76       */
77      @Parameter(property = "startScmVersionType")
78      private String startScmVersionType;
79  
80      /**
81       * End Scm Version Type.
82       */
83      @Parameter(property = "endScmVersionType")
84      private String endScmVersionType;
85  
86      /**
87       * Date Format in changelog output of scm tool.
88       */
89      @Parameter(property = "dateFormat")
90      private String dateFormat;
91  
92      /**
93       * Date format to use for the specified startDate and/or endDate.
94       */
95      @Parameter(property = "userDateFormat", defaultValue = "yyyy-MM-dd")
96      private String userDateFormat = DEFAULT_DATE_FORMAT;
97  
98      /**
99       * The version type (branch/tag) of scmVersion.
100      */
101     @Parameter(property = "scmVersionType")
102     private String scmVersionType;
103 
104     /**
105      * The version (revision number/branch name/tag name).
106      */
107     @Parameter(property = "scmVersion")
108     private String scmVersion;
109 
110     /**
111      * The branch name (TODO find out what this is for).
112      */
113     @Parameter(property = "scmBranch")
114     private String scmBranch;
115 
116     /**
117      * The number of change log items to return.
118      */
119     @Parameter(property = "limit")
120     private Integer limit;
121 
122     /**
123      * The number of days to look back for change log items to return.
124      */
125     @Parameter(property = "numDays")
126     private Integer numDays;
127 
128     /**
129      * {@inheritDoc}
130      */
131     public void execute() throws MojoExecutionException {
132         super.execute();
133 
134         SimpleDateFormat localFormat = new SimpleDateFormat(userDateFormat);
135 
136         try {
137             ScmRepository repository = getScmRepository();
138 
139             ScmProvider provider = getScmManager().getProviderByRepository(repository);
140 
141             ChangeLogScmRequest request = new ChangeLogScmRequest(repository, getFileSet());
142 
143             request.setDatePattern(dateFormat);
144 
145             if (startDate != null && !startDate.isEmpty()) {
146                 request.setStartDate(parseDate(localFormat, startDate));
147             }
148 
149             if (endDate != null && !endDate.isEmpty()) {
150                 request.setEndDate(parseDate(localFormat, endDate));
151             }
152 
153             if (startScmVersion != null && !startScmVersion.isEmpty()) {
154                 ScmVersion startRev = getScmVersion(
155                         (startScmVersionType == null || startScmVersionType.isEmpty())
156                                 ? VERSION_TYPE_REVISION
157                                 : startScmVersionType,
158                         startScmVersion);
159                 request.setStartRevision(startRev);
160             }
161 
162             if (endScmVersion != null && !endScmVersion.isEmpty()) {
163                 ScmVersion endRev = getScmVersion(
164                         (endScmVersionType == null || endScmVersionType.isEmpty())
165                                 ? VERSION_TYPE_REVISION
166                                 : endScmVersionType,
167                         endScmVersion);
168                 request.setEndRevision(endRev);
169             }
170 
171             request.setLimit(limit);
172 
173             if (numDays != null) {
174                 request.setNumDays(numDays);
175             }
176 
177             if (scmVersion != null && !scmVersion.isEmpty()) {
178                 ScmVersion rev = getScmVersion(
179                         (scmVersionType == null || scmVersionType.isEmpty()) ? VERSION_TYPE_REVISION : scmVersionType,
180                         scmVersion);
181                 request.setRevision(rev);
182             }
183 
184             if (scmBranch != null && !scmBranch.isEmpty()) {
185                 request.setScmBranch(new ScmBranch(scmBranch));
186             }
187 
188             ChangeLogScmResult result = provider.changeLog(request);
189 
190             checkResult(result);
191 
192             ChangeLogSet changeLogSet = result.getChangeLog();
193 
194             for (ChangeSet changeSet : changeLogSet.getChangeSets()) {
195                 getLog().info(changeSet.toString());
196             }
197 
198         } catch (IOException | ScmException e) {
199             throw new MojoExecutionException("Cannot run changelog command : ", e);
200         }
201     }
202 
203     /**
204      * Converts the localized date string pattern to date object.
205      *
206      * @return A date
207      */
208     private Date parseDate(SimpleDateFormat format, String date) throws MojoExecutionException {
209         if (date == null || date.trim().length() == 0) {
210             return null;
211         }
212 
213         try {
214             return format.parse(date.toString());
215         } catch (ParseException e) {
216             throw new MojoExecutionException(
217                     "Please use this date pattern: "
218                             + format.toLocalizedPattern().toString(),
219                     e);
220         }
221     }
222 }