1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
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
56
57 @Parameter(property = "startDate")
58 private String startDate;
59
60
61
62
63 @Parameter(property = "endDate")
64 private String endDate;
65
66
67
68
69 @Parameter(property = "startScmVersion")
70 private String startScmVersion;
71
72
73
74
75 @Parameter(property = "endScmVersion")
76 private String endScmVersion;
77
78
79
80
81 @Parameter(property = "startScmVersionType")
82 private String startScmVersionType;
83
84
85
86
87 @Parameter(property = "endScmVersionType")
88 private String endScmVersionType;
89
90
91
92
93 @Parameter(property = "dateFormat")
94 private String dateFormat;
95
96
97
98
99 @Parameter(property = "userDateFormat", defaultValue = "yyyy-MM-dd")
100 private String userDateFormat = DEFAULT_DATE_FORMAT;
101
102
103
104
105 @Parameter(property = "scmVersionType")
106 private String scmVersionType;
107
108
109
110
111 @Parameter(property = "scmVersion")
112 private String scmVersion;
113
114
115
116
117 @Parameter(property = "scmBranch")
118 private String scmBranch;
119
120
121
122
123 @Parameter(property = "limit")
124 private Integer limit;
125
126
127
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
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
214
215
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 }