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.command.changelog.ChangeLogScmResult;
27  import org.apache.maven.scm.command.changelog.ChangeLogSet;
28  import org.apache.maven.scm.provider.ScmProvider;
29  import org.apache.maven.scm.repository.ScmRepository;
30  
31  import java.io.IOException;
32  import java.text.ParseException;
33  import java.text.SimpleDateFormat;
34  import java.util.Date;
35  import java.util.Iterator;
36  
37  /**
38   * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
39   *
40   * @author <a href="dantran@gmail.com">Dan Tran</a>
41   * @version $Id: ChangeLogMojo.java 538854 2007-05-17 09:52:11Z evenisse $
42   * @goal changelog
43   * @aggregator
44   */
45  public class ChangeLogMojo
46      extends AbstractScmMojo
47  {
48      private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
49  
50      /**
51       * Start Date.
52       *
53       * @parameter expression="${startDate}"
54       */
55      private String startDate;
56  
57      /**
58       * End Date.
59       *
60       * @parameter expression="${endDate}"
61       */
62      private String endDate;
63  
64      /**
65       * Date Format in changelog output of scm tool.
66       *
67       * @parameter expression="${dateFormat}"
68       */
69      private String dateFormat;
70  
71      /**
72       * Date format to use for the specified startDate and/or endDate.
73       *
74       * @parameter expression="${userDateFormat}" default-value="yyyy-MM-dd"
75       */
76      private String userDateFormat = DEFAULT_DATE_FORMAT;
77  
78      /**
79       * The version type (branch/tag) of scmVersion.
80       *
81       * @parameter expression="${scmVersionType}"
82       */
83      private String scmVersionType;
84  
85      /**
86       * The version (revision number/branch name/tag name).
87       *
88       * @parameter expression="${scmVersion}"
89       */
90      private String scmVersion;
91  
92      public void execute()
93          throws MojoExecutionException
94      {
95          super.execute();
96  
97          SimpleDateFormat localFormat = new SimpleDateFormat( userDateFormat );
98  
99          try
100         {
101             ScmRepository repository = getScmRepository();
102 
103             ScmProvider provider = getScmManager().getProviderByRepository( repository );
104 
105             ChangeLogScmResult result = provider.changeLog( repository, getFileSet(),
106                                                             this.parseDate( localFormat, this.startDate ),
107                                                             this.parseDate( localFormat, this.endDate ), 0,
108                                                             (ScmBranch) getScmVersion( scmVersionType, scmVersion ),
109                                                             dateFormat );
110             checkResult( result );
111 
112             ChangeLogSet changeLogSet = result.getChangeLog();
113 
114             for ( Iterator i = changeLogSet.getChangeSets().iterator(); i.hasNext(); )
115             {
116                 ChangeSet changeSet = (ChangeSet) i.next();
117 
118                 System.out.println( changeSet.toString() );
119             }
120 
121         }
122         catch ( IOException e )
123         {
124             throw new MojoExecutionException( "Cannot run changelog command : ", e );
125         }
126         catch ( ScmException e )
127         {
128             throw new MojoExecutionException( "Cannot run changelog command : ", e );
129         }
130     }
131 
132     /**
133      * Converts the localized date string pattern to date object.
134      *
135      * @return A date
136      */
137     private Date parseDate( SimpleDateFormat format, String date )
138         throws MojoExecutionException
139     {
140         if ( date == null || date.trim().length() == 0 )
141         {
142             return null;
143         }
144 
145         try
146         {
147             return format.parse( date.toString() );
148         }
149         catch ( ParseException e )
150         {
151             throw new MojoExecutionException( "Please use this date pattern: " + format.toLocalizedPattern().toString(),
152                                               e );
153         }
154     }
155 }