001package 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 022import org.apache.maven.plugin.MojoExecutionException; 023import org.apache.maven.plugins.annotations.Mojo; 024import org.apache.maven.plugins.annotations.Parameter; 025import org.apache.maven.scm.ChangeSet; 026import org.apache.maven.scm.ScmBranch; 027import org.apache.maven.scm.ScmException; 028import org.apache.maven.scm.ScmVersion; 029import org.apache.maven.scm.command.changelog.ChangeLogScmResult; 030import org.apache.maven.scm.command.changelog.ChangeLogSet; 031import org.apache.maven.scm.provider.ScmProvider; 032import org.apache.maven.scm.repository.ScmRepository; 033import org.codehaus.plexus.util.StringUtils; 034 035import java.io.IOException; 036import java.text.ParseException; 037import java.text.SimpleDateFormat; 038import java.util.Date; 039 040/** 041 * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command. 042 * 043 * @author <a href="dantran@gmail.com">Dan Tran</a> 044 * @author Olivier Lamy 045 */ 046@Mojo( name = "changelog", aggregator = true ) 047public 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( property = "startDate" ) 056 private String startDate; 057 058 /** 059 * End Date. 060 */ 061 @Parameter( property = "endDate" ) 062 private String endDate; 063 064 /** 065 * Start Scm Version. 066 */ 067 @Parameter( property = "startScmVersion" ) 068 private String startScmVersion; 069 070 /** 071 * End Scm Version. 072 */ 073 @Parameter( property = "endScmVersion" ) 074 private String endScmVersion; 075 076 /** 077 * Start Scm Version Type. 078 */ 079 @Parameter( property = "startScmVersionType" ) 080 private String startScmVersionType; 081 082 /** 083 * End Scm Version Type. 084 */ 085 @Parameter( property = "endScmVersionType" ) 086 private String endScmVersionType; 087 088 /** 089 * Date Format in changelog output of scm tool. 090 */ 091 @Parameter( property = "dateFormat" ) 092 private String dateFormat; 093 094 /** 095 * Date format to use for the specified startDate and/or endDate. 096 */ 097 @Parameter( property = "userDateFormat", defaultValue = "yyyy-MM-dd" ) 098 private String userDateFormat = DEFAULT_DATE_FORMAT; 099 100 /** 101 * The version type (branch/tag) of scmVersion. 102 */ 103 @Parameter( property = "scmVersionType" ) 104 private String scmVersionType; 105 106 /** 107 * The version (revision number/branch name/tag name). 108 */ 109 @Parameter( property = "scmVersion" ) 110 private String scmVersion; 111 112 /** 113 * {@inheritDoc} 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 * Converts the localized date string pattern to date object. 168 * 169 * @return A date 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}