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