View Javadoc

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.scm.ChangeSet;
24  import org.apache.maven.scm.ScmBranch;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmVersion;
27  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
28  import org.apache.maven.scm.command.changelog.ChangeLogSet;
29  import org.apache.maven.scm.provider.ScmProvider;
30  import org.apache.maven.scm.repository.ScmRepository;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  import java.io.IOException;
34  import java.text.ParseException;
35  import java.text.SimpleDateFormat;
36  import java.util.Date;
37  
38  /**
39   * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
40   *
41   * @author <a href="dantran@gmail.com">Dan Tran</a>
42   * @author Olivier Lamy
43   *
44   * @goal changelog
45   * @aggregator
46   */
47  public class ChangeLogMojo
48      extends AbstractScmMojo
49  {
50      private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
51  
52      /**
53       * Start Date.
54       *
55       * @parameter expression="${startDate}"
56       */
57      private String startDate;
58  
59      /**
60       * End Date.
61       *
62       * @parameter expression="${endDate}"
63       */
64      private String endDate;
65  
66      /**
67       * Start Scm Version.
68       *
69       * @parameter expression="${startScmVersion}"
70       */
71      private String startScmVersion;
72  
73      /**
74       * End Scm Version.
75       *
76       * @parameter expression="${endScmVersion}"
77       */
78      private String endScmVersion;
79  
80      /**
81       * Start Scm Version Type.
82       *
83       * @parameter expression="${startScmVersionType}"
84       */
85      private String startScmVersionType;
86  
87      /**
88       * End Scm Version Type.
89       *
90       * @parameter expression="${endScmVersionType}"
91       */
92      private String endScmVersionType;
93  
94      /**
95       * Date Format in changelog output of scm tool.
96       *
97       * @parameter expression="${dateFormat}"
98       */
99      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 }