View Javadoc

1   package org.apache.maven.plugin.changes;
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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.logging.Log;
29  import org.apache.maven.plugins.changes.model.Action;
30  import org.apache.maven.plugins.changes.model.Release;
31  
32  /**
33   * A utility class for working with Release objects.
34   *
35   * @author Dennis Lundberg
36   * @version $Id: ReleaseUtils.java 1328886 2012-04-22 14:09:45Z bimargulies $
37   * @since 2.4
38   */
39  public class ReleaseUtils
40  {
41      private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
42  
43      private Log log;
44  
45      public ReleaseUtils( Log log )
46      {
47          this.log = log;
48      }
49  
50      /**
51       * Get the latest release by matching the supplied releases
52       * with the version from the pom.
53       *
54       * @param releases list of releases
55       * @param pomVersion Version of the artifact
56       * @return A <code>Release</code> that matches the next release of the current project
57       * @throws org.apache.maven.plugin.MojoExecutionException If a release can't be found
58       */
59      public Release getLatestRelease( List<Release> releases, String pomVersion )
60          throws MojoExecutionException
61      {
62          // Remove "-SNAPSHOT" from the end, if it's there
63          if ( pomVersion != null && pomVersion.endsWith( SNAPSHOT_SUFFIX ) )
64          {
65              pomVersion = pomVersion.substring( 0, pomVersion.length() - SNAPSHOT_SUFFIX.length() );
66          }
67          getLog().debug( "Found " + releases.size() + " releases." );
68  
69          Release release = getRelease( releases, pomVersion );
70  
71          if ( release == null )
72          {
73              throw new MojoExecutionException( "Couldn't find the release '" + pomVersion
74                  + "' among the supplied releases." );
75          }
76  
77          return release;
78      }
79  
80      private Log getLog()
81      {
82          return log;
83      }
84  
85      /**
86       * Get a release with the specified version from the list of releases.
87       *
88       * @param releases A list of releases
89       * @param version The version we want
90       * @return A Release, or null if no release with the specified version can be found
91       */
92      protected Release getRelease( List<Release> releases, String version )
93      {
94          for ( Release release : releases )
95          {
96              if ( getLog().isDebugEnabled() )
97              {
98                  getLog().debug( "The release: " + release.getVersion()
99                      + " has " + release.getActions().size() + " actions." );
100             }
101 
102             if ( release.getVersion() != null && release.getVersion().equals( version ) )
103             {
104                 if ( getLog().isDebugEnabled() )
105                 {
106                     getLog().debug( "Found the correct release: " + release.getVersion() );
107                     logRelease( release );
108                 }
109                 return release;
110             }
111         }
112         return null;
113     }
114 
115     protected void logRelease( Release release )
116     {
117         Action action;
118         for ( Iterator iterator = release.getActions().iterator(); iterator.hasNext(); )
119         {
120             action = (Action) iterator.next();
121             getLog().debug( "o " + action.getType() );
122             getLog().debug( "issue : " + action.getIssue() );
123             getLog().debug( "action : " + action.getAction() );
124             getLog().debug( "dueTo : " + action.getDueTo() );
125         }
126     }
127 
128     /**
129      * Merge releases from one issue tracker with releases from another issue
130      * tracker. If a release is found in both issue trackers, i.e. they have
131      * the same version, their issues are merged into one release.
132      *
133      * @param firstReleases Releases from the first issue tracker
134      * @param secondReleases Releases from the second issue tracker
135      * @return A list containing the merged releases
136      */
137     public List<Release> mergeReleases( final List<Release> firstReleases, final List<Release> secondReleases )
138     {
139         if ( firstReleases == null && secondReleases == null )
140         {
141             return Collections.emptyList();
142         }
143         if ( firstReleases == null )
144         {
145             return secondReleases;
146         }
147         if ( secondReleases == null )
148         {
149             return firstReleases;
150         }
151 
152         List<Release> mergedReleases = new ArrayList<Release>();
153 
154         // Loop through the releases from the first issue tracker, merging in
155         // actions from releases with the same version from the second issue
156         // tracker
157         for ( Release firstRelease : firstReleases )
158         {
159             Release secondRelease = getRelease( secondReleases, firstRelease.getVersion() );
160             if ( secondRelease != null )
161             {
162                 if ( secondRelease.getActions() != null )
163                 {
164                     firstRelease.getActions().addAll( secondRelease.getActions() );
165                 }
166             }
167             mergedReleases.add( firstRelease );
168         }
169 
170         // Handle releases that are only in the second issue tracker
171         for ( Release secondRelease : secondReleases )
172         {
173             Release mergedRelease = getRelease( mergedReleases, secondRelease.getVersion() );
174             if ( mergedRelease == null )
175             {
176                 mergedReleases.add( secondRelease );
177             }
178         }
179         return mergedReleases;
180     }
181 
182     /**
183      * Convert an untyped List of Release objects that comes from changes.xml
184      * into a typed List of Release objects.
185      *
186      * @param changesReleases An untyped List of Release objects
187      * @return A type List of Release objects
188      * @todo When Modello can generate typed collections this method is no longer needed
189      */
190     public List<Release> convertReleaseList( List changesReleases ) {
191         List<Release> releases = new ArrayList<Release>();
192 
193         // Loop through the List of releases from changes.xml and casting each
194         // release to a Release
195         for ( Iterator iterator = changesReleases.iterator(); iterator.hasNext(); )
196         {
197             Release release = (Release) iterator.next();
198             releases.add( release );
199         }
200         return releases;
201     }
202 
203     /**
204      * Merge releases from parent component with releases from child component.
205      * If a release is found in both components, i.e. they have the same version,
206      * their issues are merged into one (parent) release with component marker
207      * for component issues.
208      *
209      * @param releases Releases from the parent component
210      * @param componentName child component name (retrieved from project name)
211      * @param componentReleases Releases from the child component
212      * @return A list containing the merged releases
213      */
214     public List mergeReleases( final List releases, final String componentName, final List componentReleases ) {
215         if ( releases == null && componentReleases == null )
216         {
217             return Collections.EMPTY_LIST;
218         }
219         if ( componentReleases == null )
220         {
221             return releases;
222         }
223 
224         final List mergedReleases = new ArrayList();
225 
226         if ( releases != null )
227         {
228             for ( Iterator iterator = releases.iterator(); iterator.hasNext(); )
229             {
230                 final Release release = (Release) iterator.next();
231                 final Release componentRelease = getRelease( componentReleases, release.getVersion() );
232                 if ( componentRelease != null ) {
233                     release.addComponent( componentName, componentRelease );
234                 }
235                 mergedReleases.add( release );
236             }
237         }
238 
239         for ( Iterator iterator = componentReleases.iterator(); iterator.hasNext(); )
240         {
241             final Release release = (Release) iterator.next();
242             final Release mergedRelease = getRelease( mergedReleases, release.getVersion() );
243             if ( mergedRelease == null )
244             {
245                 final Release componentRelease = new Release();
246                 componentRelease.setVersion( release.getVersion() );
247                 componentRelease.setDateRelease( release.getDateRelease() );
248                 componentRelease.addComponent( componentName, release );
249                 mergedReleases.add( componentRelease );
250             }
251         }
252 
253         return mergedReleases;
254     }
255 }