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