1 package org.apache.maven.scm.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.scm.ChangeSet;
24 import org.apache.maven.scm.ScmBranch;
25 import org.apache.maven.scm.ScmException;
26 import org.apache.maven.scm.ScmVersion;
27 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
28 import org.apache.maven.scm.command.changelog.ChangeLogSet;
29 import org.apache.maven.scm.provider.ScmProvider;
30 import org.apache.maven.scm.repository.ScmRepository;
31 import org.codehaus.plexus.util.StringUtils;
32
33 import java.io.IOException;
34 import java.text.ParseException;
35 import java.text.SimpleDateFormat;
36 import java.util.Date;
37
38
39
40
41
42
43
44
45
46
47 public class ChangeLogMojo
48 extends AbstractScmMojo
49 {
50 private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
51
52
53
54
55
56
57 private String startDate;
58
59
60
61
62
63
64 private String endDate;
65
66
67
68
69
70
71 private String startScmVersion;
72
73
74
75
76
77
78 private String endScmVersion;
79
80
81
82
83
84
85 private String startScmVersionType;
86
87
88
89
90
91
92 private String endScmVersionType;
93
94
95
96
97
98
99 private String dateFormat;
100
101
102
103
104
105
106 private String userDateFormat = DEFAULT_DATE_FORMAT;
107
108
109
110
111
112
113 private String scmVersionType;
114
115
116
117
118
119
120 private String scmVersion;
121
122
123
124
125 public void execute()
126 throws MojoExecutionException
127 {
128 super.execute();
129
130 SimpleDateFormat localFormat = new SimpleDateFormat( userDateFormat );
131
132 try
133 {
134 ScmRepository repository = getScmRepository();
135
136 ScmProvider provider = getScmManager().getProviderByRepository( repository );
137
138 ScmVersion startRev =
139 getScmVersion( StringUtils.isEmpty( startScmVersionType ) ? "revision" : startScmVersionType,
140 startScmVersion );
141 ScmVersion endRev =
142 getScmVersion( StringUtils.isEmpty( endScmVersionType ) ? "revision" : endScmVersionType,
143 endScmVersion );
144
145 ChangeLogScmResult result;
146 if ( startRev != null || endRev != null )
147 {
148 result = provider.changeLog( repository, getFileSet(), startRev, endRev, dateFormat );
149 }
150 else
151 {
152 result = provider.changeLog( repository, getFileSet(), this.parseDate( localFormat, this.startDate ),
153 this.parseDate( localFormat, this.endDate ), 0,
154 (ScmBranch) getScmVersion( scmVersionType, scmVersion ), dateFormat );
155 }
156 checkResult( result );
157
158 ChangeLogSet changeLogSet = result.getChangeLog();
159
160 for ( ChangeSet changeSet : changeLogSet.getChangeSets() )
161 {
162 getLog().info( changeSet.toString() );
163 }
164
165 }
166 catch ( IOException e )
167 {
168 throw new MojoExecutionException( "Cannot run changelog command : ", e );
169 }
170 catch ( ScmException e )
171 {
172 throw new MojoExecutionException( "Cannot run changelog command : ", e );
173 }
174 }
175
176
177
178
179
180
181 private Date parseDate( SimpleDateFormat format, String date )
182 throws MojoExecutionException
183 {
184 if ( date == null || date.trim().length() == 0 )
185 {
186 return null;
187 }
188
189 try
190 {
191 return format.parse( date.toString() );
192 }
193 catch ( ParseException e )
194 {
195 throw new MojoExecutionException( "Please use this date pattern: " + format.toLocalizedPattern().toString(),
196 e );
197 }
198 }
199 }