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.List;
22  
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.execution.ProjectDependencyGraph;
25  import org.apache.maven.project.MavenProject;
26  
27  /**
28   * A build context that matches a Maven project to a given task segment, and the session to be used.
29   * <p>
30   * A note to the reader;
31   * </p>
32   * <p>
33   * There are several issues/discussions regarding how "aggregator" plugins should be handled.
34   * Read for instance http://docs.codehaus.org/display/MAVEN/Deterministic+Lifecycle+Planning
35   * </p>
36   * <p>
37   * In their current implementation they are "bolted" onto the lifecycle by separating them
38   * into TaskSegments. This class represents the execution context of one such task segment.
39   * </p>
40   * <p>
41   * Wise voices have suggested that maybe aggregators shouldn't be bound to the ordinary
42   * lifecycle at all, in which case we wouldn't be needing this class at all ( and
43   * ProjectBuildList.getByTaskSegments). Or maybe they should be introduced in the calculation
44   * of the execution plan instead, which seems much nicer.
45   * </p>
46   * <p>
47   * Additionally this class contains a clone of the MavenSession, which is *only* needed
48   * because it has as notion of a "current" project.
49   * </p>
50   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
51   *
52   * @since 3.0
53   */
54  public final class ProjectSegment {
55      private final MavenProject project;
56  
57      private final TaskSegment taskSegment;
58  
59      private final MavenSession session;
60  
61      private final List<MavenProject> nonTransitiveUpstreamProjects;
62  
63      private final List<MavenProject> transitiveUpstreamProjects;
64  
65      public ProjectSegment(MavenProject project, TaskSegment taskSegment, MavenSession copiedSession) {
66          this.project = project;
67          this.taskSegment = taskSegment;
68          this.session = copiedSession;
69          final ProjectDependencyGraph dependencyGraph = getSession().getProjectDependencyGraph();
70          nonTransitiveUpstreamProjects = dependencyGraph.getUpstreamProjects(getProject(), false);
71          transitiveUpstreamProjects = dependencyGraph.getUpstreamProjects(getProject(), true);
72      }
73  
74      public MavenSession getSession() {
75          return session;
76      }
77  
78      public MavenProject getProject() {
79          return project;
80      }
81  
82      public TaskSegment getTaskSegment() {
83          return taskSegment;
84      }
85  
86      public List<MavenProject> getImmediateUpstreamProjects() {
87          return nonTransitiveUpstreamProjects;
88      }
89  
90      public List<MavenProject> getTransitiveUpstreamProjects() {
91          return transitiveUpstreamProjects;
92      }
93  
94      @Override
95      public String toString() {
96          return getProject().getId() + " -> " + getTaskSegment();
97      }
98  }