Debian Codegraph @

maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/ChangeLogMojo.java

raw
1 package org.apache.maven.scm.plugin;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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.ChangeLogScmRequest;
30 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
31 import org.apache.maven.scm.command.changelog.ChangeLogSet;
32 import org.apache.maven.scm.provider.ScmProvider;
33 import org.apache.maven.scm.repository.ScmRepository;
34 import org.codehaus.plexus.util.StringUtils;
35
36 import java.io.IOException;
37 import java.text.ParseException;
38 import java.text.SimpleDateFormat;
39 import java.util.Date;
40
41 /**
42 * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
43 *
44 * @author <a href="[email protected]">Dan Tran</a>
45 * @author Olivier Lamy
46 */
47 @Mojo( name = "changelog", aggregator = true )
48 public class ChangeLogMojo
49 extends AbstractScmMojo
50 {
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 /**
133 * {@inheritDoc}
134 */
135 public void execute()
136 throws MojoExecutionException
137 {
138 super.execute();
139
140 SimpleDateFormat localFormat = new SimpleDateFormat( userDateFormat );
141
142 try
143 {
144 ScmRepository repository = getScmRepository();
145
146 ScmProvider provider = getScmManager().getProviderByRepository( repository );
147
148 ChangeLogScmRequest request = new ChangeLogScmRequest( repository, getFileSet() );
149
150 request.setDatePattern( dateFormat );
151
152 if ( StringUtils.isNotEmpty( startDate ) )
153 {
154 request.setStartDate( parseDate( localFormat, startDate ) );
155 }
156
157 if ( StringUtils.isNotEmpty( endDate ) )
158 {
159 request.setEndDate( parseDate( localFormat, endDate ) );
160 }
161
162 if ( StringUtils.isNotEmpty( startScmVersion ) )
163 {
164 ScmVersion startRev =
165 getScmVersion( StringUtils.isEmpty( startScmVersionType ) ? VERSION_TYPE_REVISION
166 : startScmVersionType, startScmVersion );
167 request.setStartRevision( startRev );
168 }
169
170 if ( StringUtils.isNotEmpty( endScmVersion ) )
171 {
172 ScmVersion endRev =
173 getScmVersion( StringUtils.isEmpty( endScmVersionType ) ? VERSION_TYPE_REVISION
174 : endScmVersionType, endScmVersion );
175 request.setEndRevision( endRev );
176 }
177
178 request.setLimit( limit );
179
180 if ( numDays != null )
181 {
182 request.setNumDays( numDays );
183 }
184
185 if ( StringUtils.isNotEmpty( scmVersion ) )
186 {
187 ScmVersion rev =
188 getScmVersion( StringUtils.isEmpty( scmVersionType ) ? VERSION_TYPE_REVISION
189 : scmVersionType, scmVersion );
190 request.setRevision( rev );
191 }
192
193 if ( StringUtils.isNotEmpty( scmBranch ) )
194 {
195 request.setScmBranch( new ScmBranch( scmBranch ) );
196 }
197
198 ChangeLogScmResult result = provider.changeLog( request );
199
200 checkResult( result );
201
202 ChangeLogSet changeLogSet = result.getChangeLog();
203
204 for ( ChangeSet changeSet : changeLogSet.getChangeSets() )
205 {
206 getLog().info( changeSet.toString() );
207 }
208
209 }
210 catch ( IOException | ScmException e )
211 {
212 throw new MojoExecutionException( "Cannot run changelog command : ", e );
213 }
214 }
215
216 /**
217 * Converts the localized date string pattern to date object.
218 *
219 * @return A date
220 */
221 private Date parseDate( SimpleDateFormat format, String date )
222 throws MojoExecutionException
223 {
224 if ( date == null || date.trim().length() == 0 )
225 {
226 return null;
227 }
228
229 try
230 {
231 return format.parse( date.toString() );
232 }
233 catch ( ParseException e )
234 {
235 throw new MojoExecutionException( "Please use this date pattern: " + format.toLocalizedPattern().toString(),
236 e );
237 }
238 }
239 }