View Javadoc
1   package org.apache.maven.lifecycle.internal;
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.HashMap;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.maven.artifact.ArtifactUtils;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.project.MavenProject;
34  
35  /**
36   * A list of project segments, ordered so that all ProjectSegments from first TaskSegment come before any
37   * subsequent TaskSegments.
38   * 
39   * @since 3.0
40   * @author Kristian Rosenvold
41   *         <p/>
42   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
43   */
44  public class ProjectBuildList
45      implements Iterable<ProjectSegment>
46  {
47      private final List<ProjectSegment> items;
48  
49      public ProjectBuildList( List<ProjectSegment> items )
50      {
51          this.items = Collections.unmodifiableList( items );
52      }
53  
54      // TODO: Optimize; or maybe just rewrite the whole way aggregating mojos are being run.
55      /**
56       * Returns aProjectBuildList that contains only items for the specified taskSegment
57       * @param taskSegment the requested tasksegment
58       * @return a project build list for the supplied task segment
59       */
60      public ProjectBuildList getByTaskSegment( TaskSegment taskSegment )
61      {
62          List<ProjectSegment> currentSegment = new ArrayList<ProjectSegment>();
63          for ( ProjectSegment projectBuild : items )
64          {
65              if ( taskSegment == projectBuild.getTaskSegment() )
66              { // NOTE: There's no notion of taskSegment equality.
67                  currentSegment.add( projectBuild );
68              }
69          }
70          return new ProjectBuildList( currentSegment );
71      }
72  
73      public Map<MavenProject, ProjectSegment> selectSegment( TaskSegment taskSegment )
74      {
75          Map<MavenProject, ProjectSegment> result = new HashMap<MavenProject, ProjectSegment>();
76          for ( ProjectSegment projectBuild : items )
77          {
78              if ( taskSegment == projectBuild.getTaskSegment() )
79              { // NOTE: There's no notion of taskSegment equality.
80                  result.put( projectBuild.getProject(), projectBuild );
81              }
82          }
83          return result;
84      }
85  
86      /**
87       * Finds the first ProjectSegment matching the supplied project
88       * @param mavenProject the requested project
89       * @return The projectSegment or null.
90       */
91      public ProjectSegment findByMavenProject( MavenProject mavenProject )
92      {
93          for ( ProjectSegment projectBuild : items )
94          {
95              if ( mavenProject.equals( projectBuild.getProject() ) )
96              {
97                  return projectBuild;
98              }
99          }
100         return null;
101     }
102 
103     public Iterator<ProjectSegment> iterator()
104     {
105         return items.iterator();
106     }
107 
108     public void closeAll()
109     {
110         for ( ProjectSegment item : items )
111         {
112             MavenSession sessionForThisModule = item.getSession();
113             sessionForThisModule.setCurrentProject( null );
114         }
115     }
116 
117     public int size()
118     {
119         return items.size();
120     }
121 
122     public ProjectSegment get( int index )
123     {
124         return items.get( index );
125     }
126 
127     public Set<String> getReactorProjectKeys()
128     {
129         Set<String> projectKeys = new HashSet<String>( items.size() * 2 );
130         for ( ProjectSegment projectBuild : items )
131         {
132             MavenProject project = projectBuild.getProject();
133             String key = ArtifactUtils.key( project.getGroupId(), project.getArtifactId(), project.getVersion() );
134             projectKeys.add( key );
135         }
136         return projectKeys;
137     }
138 
139 
140     public boolean isEmpty()
141     {
142         return items.isEmpty();
143     }
144 
145     /**
146      * @return a set of all the projects managed by the build
147      */
148     public Set<MavenProject> getProjects()
149     {
150         Set<MavenProject> projects = new HashSet<MavenProject>();
151 
152         for ( ProjectSegment s : items )
153         {
154             projects.add( s.getProject() );
155         }
156         return projects;
157     }
158 }