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