1 package com.soebes.maven.extensions.incremental;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
72 intermediateResult.addAll( projectDependencyGraph.getDownstreamProjects( selectedProject, true ) );
73
74
75
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 }