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