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 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
41
42
43
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
52
53 @Parameter(property = "startDate")
54 private String startDate;
55
56
57
58
59 @Parameter(property = "endDate")
60 private String endDate;
61
62
63
64
65 @Parameter(property = "startScmVersion")
66 private String startScmVersion;
67
68
69
70
71 @Parameter(property = "endScmVersion")
72 private String endScmVersion;
73
74
75
76
77 @Parameter(property = "startScmVersionType")
78 private String startScmVersionType;
79
80
81
82
83 @Parameter(property = "endScmVersionType")
84 private String endScmVersionType;
85
86
87
88
89 @Parameter(property = "dateFormat")
90 private String dateFormat;
91
92
93
94
95 @Parameter(property = "userDateFormat", defaultValue = "yyyy-MM-dd")
96 private String userDateFormat = DEFAULT_DATE_FORMAT;
97
98
99
100
101 @Parameter(property = "scmVersionType")
102 private String scmVersionType;
103
104
105
106
107 @Parameter(property = "scmVersion")
108 private String scmVersion;
109
110
111
112
113 @Parameter(property = "scmBranch")
114 private String scmBranch;
115
116
117
118
119 @Parameter(property = "limit")
120 private Integer limit;
121
122
123
124
125 @Parameter(property = "numDays")
126 private Integer numDays;
127
128
129
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
205
206
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 }