001 package org.apache.maven.scm.plugin;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.maven.plugin.MojoExecutionException;
023 import org.apache.maven.scm.ChangeSet;
024 import org.apache.maven.scm.ScmBranch;
025 import org.apache.maven.scm.ScmException;
026 import org.apache.maven.scm.ScmVersion;
027 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
028 import org.apache.maven.scm.command.changelog.ChangeLogSet;
029 import org.apache.maven.scm.provider.ScmProvider;
030 import org.apache.maven.scm.repository.ScmRepository;
031 import org.codehaus.plexus.util.StringUtils;
032
033 import java.io.IOException;
034 import java.text.ParseException;
035 import java.text.SimpleDateFormat;
036 import java.util.Date;
037
038 /**
039 * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
040 *
041 * @author <a href="dantran@gmail.com">Dan Tran</a>
042 * @author Olivier Lamy
043 *
044 * @goal changelog
045 * @aggregator
046 */
047 public class ChangeLogMojo
048 extends AbstractScmMojo
049 {
050 private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
051
052 /**
053 * Start Date.
054 *
055 * @parameter expression="${startDate}"
056 */
057 private String startDate;
058
059 /**
060 * End Date.
061 *
062 * @parameter expression="${endDate}"
063 */
064 private String endDate;
065
066 /**
067 * Start Scm Version.
068 *
069 * @parameter expression="${startScmVersion}"
070 */
071 private String startScmVersion;
072
073 /**
074 * End Scm Version.
075 *
076 * @parameter expression="${endScmVersion}"
077 */
078 private String endScmVersion;
079
080 /**
081 * Start Scm Version Type.
082 *
083 * @parameter expression="${startScmVersionType}"
084 */
085 private String startScmVersionType;
086
087 /**
088 * End Scm Version Type.
089 *
090 * @parameter expression="${endScmVersionType}"
091 */
092 private String endScmVersionType;
093
094 /**
095 * Date Format in changelog output of scm tool.
096 *
097 * @parameter expression="${dateFormat}"
098 */
099 private String dateFormat;
100
101 /**
102 * Date format to use for the specified startDate and/or endDate.
103 *
104 * @parameter expression="${userDateFormat}" default-value="yyyy-MM-dd"
105 */
106 private String userDateFormat = DEFAULT_DATE_FORMAT;
107
108 /**
109 * The version type (branch/tag) of scmVersion.
110 *
111 * @parameter expression="${scmVersionType}"
112 */
113 private String scmVersionType;
114
115 /**
116 * The version (revision number/branch name/tag name).
117 *
118 * @parameter expression="${scmVersion}"
119 */
120 private String scmVersion;
121
122 /**
123 * {@inheritDoc}
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 * Converts the localized date string pattern to date object.
178 *
179 * @return A date
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 }