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.ChangeLogScmRequest;
030import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
031import org.apache.maven.scm.command.changelog.ChangeLogSet;
032import org.apache.maven.scm.provider.ScmProvider;
033import org.apache.maven.scm.repository.ScmRepository;
034import org.codehaus.plexus.util.StringUtils;
035
036import java.io.IOException;
037import java.text.ParseException;
038import java.text.SimpleDateFormat;
039import java.util.Date;
040
041/**
042 * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
043 *
044 * @author <a href="dantran@gmail.com">Dan Tran</a>
045 * @author Olivier Lamy
046 */
047@Mojo( name = "changelog", aggregator = true )
048public class ChangeLogMojo
049    extends AbstractScmMojo
050{
051
052    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
053
054    /**
055     * Start Date.
056     */
057    @Parameter( property = "startDate" )
058    private String startDate;
059
060    /**
061     * End Date.
062     */
063    @Parameter( property = "endDate" )
064    private String endDate;
065
066    /**
067     * Start Scm Version.
068     */
069    @Parameter( property = "startScmVersion" )
070    private String startScmVersion;
071
072    /**
073     * End Scm Version.
074     */
075    @Parameter( property = "endScmVersion" )
076    private String endScmVersion;
077
078    /**
079     * Start Scm Version Type.
080     */
081    @Parameter( property = "startScmVersionType" )
082    private String startScmVersionType;
083
084    /**
085     * End Scm Version Type.
086     */
087    @Parameter( property = "endScmVersionType" )
088    private String endScmVersionType;
089
090    /**
091     * Date Format in changelog output of scm tool.
092     */
093    @Parameter( property = "dateFormat" )
094    private String dateFormat;
095
096    /**
097     * Date format to use for the specified startDate and/or endDate.
098     */
099    @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}