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