View Javadoc
1   package com.soebes.maven.extensions.incremental;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Objects;
25  import java.util.concurrent.ExecutionException;
26  
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.execution.ProjectDependencyGraph;
29  import org.apache.maven.lifecycle.internal.LifecycleModuleBuilder;
30  import org.apache.maven.lifecycle.internal.ReactorContext;
31  import org.apache.maven.lifecycle.internal.TaskSegment;
32  import org.apache.maven.project.MavenProject;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * @author Karl Heinz Marbaise <khmabaise@apache.org>
38   */
39  class IncrementalModuleBuilderImpl
40  {
41  
42      private final Logger logger = LoggerFactory.getLogger( getClass() );
43  
44      private MavenSession mavenSession;
45  
46      private List<MavenProject> projects;
47  
48      private List<TaskSegment> taskSegments;
49  
50      private ReactorContext reactorContext;
51  
52      private final LifecycleModuleBuilder lifecycleModuleBuilder;
53  
54      IncrementalModuleBuilderImpl( List<MavenProject> selectedProjects, LifecycleModuleBuilder lifecycleModuleBuilder,
55                                    MavenSession session, ReactorContext reactorContext, List<TaskSegment> taskSegments )
56      {
57  
58          this.lifecycleModuleBuilder =
59              Objects.requireNonNull( lifecycleModuleBuilder, "lifecycleModuleBuilder is not allowed to be null." );
60          this.mavenSession = Objects.requireNonNull( session, "session is not allowed to be null." );
61          this.taskSegments = Objects.requireNonNull( taskSegments, "taskSegements is not allowed to be null" );
62          this.reactorContext = Objects.requireNonNull( reactorContext, "reactorContext is not allowed to be null." );
63  
64          ProjectDependencyGraph projectDependencyGraph = session.getProjectDependencyGraph();
65  
66          List<MavenProject> intermediateResult = new LinkedList<>();
67  
68          for ( MavenProject selectedProject : selectedProjects )
69          {
70              intermediateResult.add( selectedProject );
71              // Up or downstream ? (-amd)
72              intermediateResult.addAll( projectDependencyGraph.getDownstreamProjects( selectedProject, true ) );
73              // TODO: Need to think about this? -am ?
74              // intermediateResult.addAll(projectDependencyGraph.getUpstreamProjects(selectedProject,
75              // true));
76          }
77  
78          List<MavenProject> result = new LinkedList<>();
79  
80          for ( MavenProject project : intermediateResult )
81          {
82              if ( !result.contains( project ) )
83              {
84                  result.add( project );
85              }
86          }
87  
88          this.projects = result;
89  
90      }
91  
92      public void build()
93          throws ExecutionException, InterruptedException
94      {
95          this.mavenSession.setProjects( this.projects );
96  
97          logger.info( "New Calculated Reactor:" );
98          for ( MavenProject mavenProject : this.mavenSession.getProjects() )
99          {
100             logger.info( " {}", mavenProject.getName() );
101         }
102 
103         for ( TaskSegment taskSegment : this.taskSegments )
104         {
105             logger.debug( "Segment" );
106             List<Object> tasks = taskSegment.getTasks();
107             for ( Object task : tasks )
108             {
109                 logger.debug( " Task:" + task );
110             }
111             for ( MavenProject mavenProject : mavenSession.getProjects() )
112             {
113                 logger.info( "Building project: {}", mavenProject.getId() );
114                 lifecycleModuleBuilder.buildProject( mavenSession, reactorContext, mavenProject, taskSegment );
115             }
116         }
117     }
118 
119 }