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 javax.inject.Inject;
22  
23  import java.io.IOException;
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  import java.util.Date;
27  
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.scm.ChangeSet;
32  import org.apache.maven.scm.ScmBranch;
33  import org.apache.maven.scm.ScmException;
34  import org.apache.maven.scm.ScmVersion;
35  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
36  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
37  import org.apache.maven.scm.command.changelog.ChangeLogSet;
38  import org.apache.maven.scm.manager.ScmManager;
39  import org.apache.maven.scm.provider.ScmProvider;
40  import org.apache.maven.scm.repository.ScmRepository;
41  import org.apache.maven.settings.crypto.SettingsDecrypter;
42  
43  /**
44   * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
45   *
46   * @author <a href="dantran@gmail.com">Dan Tran</a>
47   * @author Olivier Lamy
48   */
49  @Mojo(name = "changelog", aggregator = true)
50  public class ChangeLogMojo extends AbstractScmMojo {
51  
52      private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
53  
54      /**
55       * Start Date.
56       */
57      @Parameter(property = "startDate")
58      private String startDate;
59  
60      /**
61       * End Date.
62       */
63      @Parameter(property = "endDate")
64      private String endDate;
65  
66      /**
67       * Start Scm Version.
68       */
69      @Parameter(property = "startScmVersion")
70      private String startScmVersion;
71  
72      /**
73       * End Scm Version.
74       */
75      @Parameter(property = "endScmVersion")
76      private String endScmVersion;
77  
78      /**
79       * Start Scm Version Type.
80       */
81      @Parameter(property = "startScmVersionType")
82      private String startScmVersionType;
83  
84      /**
85       * End Scm Version Type.
86       */
87      @Parameter(property = "endScmVersionType")
88      private String endScmVersionType;
89  
90      /**
91       * Date Format in changelog output of scm tool.
92       */
93      @Parameter(property = "dateFormat")
94      private String dateFormat;
95  
96      /**
97       * Date format to use for the specified startDate and/or endDate.
98       */
99      @Parameter(property = "userDateFormat", defaultValue = "yyyy-MM-dd")
100     private String userDateFormat = DEFAULT_DATE_FORMAT;
101 
102     /**
103      * The version type (branch/tag) of scmVersion.
104      */
105     @Parameter(property = "scmVersionType")
106     private String scmVersionType;
107 
108     /**
109      * The version (revision number/branch name/tag name).
110      */
111     @Parameter(property = "scmVersion")
112     private String scmVersion;
113 
114     /**
115      * The branch name (TODO find out what this is for).
116      */
117     @Parameter(property = "scmBranch")
118     private String scmBranch;
119 
120     /**
121      * The number of change log items to return.
122      */
123     @Parameter(property = "limit")
124     private Integer limit;
125 
126     /**
127      * The number of days to look back for change log items to return.
128      */
129     @Parameter(property = "numDays")
130     private Integer numDays;
131 
132     @Inject
133     public ChangeLogMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
134         super(manager, settingsDecrypter);
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     public void execute() throws MojoExecutionException {
141         super.execute();
142 
143         SimpleDateFormat localFormat = new SimpleDateFormat(userDateFormat);
144 
145         try {
146             ScmRepository repository = getScmRepository();
147 
148             ScmProvider provider = getScmManager().getProviderByRepository(repository);
149 
150             ChangeLogScmRequest request = new ChangeLogScmRequest(repository, getFileSet());
151 
152             request.setDatePattern(dateFormat);
153 
154             if (startDate != null && !startDate.isEmpty()) {
155                 request.setStartDate(parseDate(localFormat, startDate));
156             }
157 
158             if (endDate != null && !endDate.isEmpty()) {
159                 request.setEndDate(parseDate(localFormat, endDate));
160             }
161 
162             if (startScmVersion != null && !startScmVersion.isEmpty()) {
163                 ScmVersion startRev = getScmVersion(
164                         (startScmVersionType == null || startScmVersionType.isEmpty())
165                                 ? VERSION_TYPE_REVISION
166                                 : startScmVersionType,
167                         startScmVersion);
168                 request.setStartRevision(startRev);
169             }
170 
171             if (endScmVersion != null && !endScmVersion.isEmpty()) {
172                 ScmVersion endRev = getScmVersion(
173                         (endScmVersionType == null || endScmVersionType.isEmpty())
174                                 ? VERSION_TYPE_REVISION
175                                 : endScmVersionType,
176                         endScmVersion);
177                 request.setEndRevision(endRev);
178             }
179 
180             request.setLimit(limit);
181 
182             if (numDays != null) {
183                 request.setNumDays(numDays);
184             }
185 
186             if (scmVersion != null && !scmVersion.isEmpty()) {
187                 ScmVersion rev = getScmVersion(
188                         (scmVersionType == null || scmVersionType.isEmpty()) ? VERSION_TYPE_REVISION : scmVersionType,
189                         scmVersion);
190                 request.setRevision(rev);
191             }
192 
193             if (scmBranch != null && !scmBranch.isEmpty()) {
194                 request.setScmBranch(new ScmBranch(scmBranch));
195             }
196 
197             ChangeLogScmResult result = provider.changeLog(request);
198 
199             checkResult(result);
200 
201             ChangeLogSet changeLogSet = result.getChangeLog();
202 
203             for (ChangeSet changeSet : changeLogSet.getChangeSets()) {
204                 getLog().debug(changeSet.toString());
205             }
206 
207         } catch (IOException | ScmException e) {
208             throw new MojoExecutionException("Cannot run changelog command : ", e);
209         }
210     }
211 
212     /**
213      * Converts the localized date string pattern to date object.
214      *
215      * @return a date
216      */
217     private Date parseDate(SimpleDateFormat format, String date) throws MojoExecutionException {
218         if (date == null || date.trim().length() == 0) {
219             return null;
220         }
221 
222         try {
223             return format.parse(date);
224         } catch (ParseException e) {
225             throw new MojoExecutionException("Please use this date pattern: " + format.toLocalizedPattern(), e);
226         }
227     }
228 }