View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle.internal;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import org.apache.maven.artifact.ArtifactUtils;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.project.MavenProject;
32  
33  /**
34   * <p>
35   * A list of project segments, ordered so that all ProjectSegments from first TaskSegment come before any
36   * subsequent TaskSegments.
37   * </p>
38   * <strong>Note:</strong> This interface is part of work in progress and can be changed or removed without notice.
39   *
40   * @since 3.0
41   * @author Kristian Rosenvold
42   */
43  public class ProjectBuildList implements Iterable<ProjectSegment> {
44      private final List<ProjectSegment> items;
45  
46      public ProjectBuildList(List<ProjectSegment> items) {
47          this.items = Collections.unmodifiableList(items);
48      }
49  
50      // TODO Optimize; or maybe just rewrite the whole way aggregating mojos are being run.
51      /**
52       * Returns aProjectBuildList that contains only items for the specified taskSegment
53       * @param taskSegment the requested task segment
54       * @return a project build list for the supplied task segment
55       */
56      public ProjectBuildList getByTaskSegment(TaskSegment taskSegment) {
57          List<ProjectSegment> currentSegment = new ArrayList<>();
58          for (ProjectSegment projectBuild : items) {
59              if (taskSegment == projectBuild.getTaskSegment()) { // NOTE: There's no notion of taskSegment equality.
60                  currentSegment.add(projectBuild);
61              }
62          }
63          return new ProjectBuildList(currentSegment);
64      }
65  
66      public Map<MavenProject, ProjectSegment> selectSegment(TaskSegment taskSegment) {
67          Map<MavenProject, ProjectSegment> result = new HashMap<>();
68          for (ProjectSegment projectBuild : items) {
69              if (taskSegment == projectBuild.getTaskSegment()) { // NOTE: There's no notion of taskSegment equality.
70                  result.put(projectBuild.getProject(), projectBuild);
71              }
72          }
73          return result;
74      }
75  
76      /**
77       * Finds the first ProjectSegment matching the supplied project
78       * @param mavenProject the requested project
79       * @return The projectSegment or null.
80       */
81      public ProjectSegment findByMavenProject(MavenProject mavenProject) {
82          for (ProjectSegment projectBuild : items) {
83              if (mavenProject.equals(projectBuild.getProject())) {
84                  return projectBuild;
85              }
86          }
87          return null;
88      }
89  
90      public Iterator<ProjectSegment> iterator() {
91          return items.iterator();
92      }
93  
94      public void closeAll() {
95          for (ProjectSegment item : items) {
96              MavenSession sessionForThisModule = item.getSession();
97              sessionForThisModule.setCurrentProject(null);
98          }
99      }
100 
101     public int size() {
102         return items.size();
103     }
104 
105     public ProjectSegment get(int index) {
106         return items.get(index);
107     }
108 
109     public Set<String> getReactorProjectKeys() {
110         Set<String> projectKeys = new HashSet<>(items.size() * 2);
111         for (ProjectSegment projectBuild : items) {
112             MavenProject project = projectBuild.getProject();
113             String key = ArtifactUtils.key(project.getGroupId(), project.getArtifactId(), project.getVersion());
114             projectKeys.add(key);
115         }
116         return projectKeys;
117     }
118 
119     public boolean isEmpty() {
120         return items.isEmpty();
121     }
122 
123     /**
124      * @return a set of all the projects managed by the build
125      */
126     public Set<MavenProject> getProjects() {
127         Set<MavenProject> projects = new HashSet<>();
128 
129         for (ProjectSegment s : items) {
130             projects.add(s.getProject());
131         }
132         return projects;
133     }
134 }