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.Collections;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.function.Function;
28  import java.util.stream.Collectors;
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   */
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          return new ProjectBuildList(
58                  items.stream().filter(pb -> taskSegment == pb.getTaskSegment()).collect(Collectors.toList()));
59      }
60  
61      public Map<MavenProject, ProjectSegment> selectSegment(TaskSegment taskSegment) {
62          return items.stream()
63                  .filter(pb -> taskSegment == pb.getTaskSegment())
64                  .collect(Collectors.toMap(ProjectSegment::getProject, Function.identity()));
65      }
66  
67      /**
68       * Finds the first ProjectSegment matching the supplied project
69       * @param mavenProject the requested project
70       * @return The projectSegment or null.
71       */
72      public ProjectSegment findByMavenProject(MavenProject mavenProject) {
73          return items.stream()
74                  .filter(pb -> mavenProject.equals(pb.getProject()))
75                  .findFirst()
76                  .orElse(null);
77      }
78  
79      public Iterator<ProjectSegment> iterator() {
80          return items.iterator();
81      }
82  
83      public void closeAll() {
84          for (ProjectSegment item : items) {
85              MavenSession sessionForThisModule = item.getSession();
86              sessionForThisModule.setCurrentProject(null);
87          }
88      }
89  
90      public int size() {
91          return items.size();
92      }
93  
94      public ProjectSegment get(int index) {
95          return items.get(index);
96      }
97  
98      public Set<String> getReactorProjectKeys() {
99          Set<String> projectKeys = new HashSet<>(items.size() * 2);
100         for (ProjectSegment projectBuild : items) {
101             MavenProject project = projectBuild.getProject();
102             String key = ArtifactUtils.key(project.getGroupId(), project.getArtifactId(), project.getVersion());
103             projectKeys.add(key);
104         }
105         return projectKeys;
106     }
107 
108     public boolean isEmpty() {
109         return items.isEmpty();
110     }
111 
112     /**
113      * @return a set of all the projects managed by the build
114      */
115     public Set<MavenProject> getProjects() {
116         return items.stream().map(ProjectSegment::getProject).collect(Collectors.toSet());
117     }
118 }