1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.execution.MavenSession;
23 import org.apache.maven.lifecycle.LifecycleNotFoundException;
24 import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
25 import org.apache.maven.plugin.InvalidPluginDescriptorException;
26 import org.apache.maven.plugin.MojoNotFoundException;
27 import org.apache.maven.plugin.PluginDescriptorParsingException;
28 import org.apache.maven.plugin.PluginNotFoundException;
29 import org.apache.maven.plugin.PluginResolutionException;
30 import org.apache.maven.plugin.descriptor.MojoDescriptor;
31 import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
32 import org.apache.maven.plugin.version.PluginVersionResolutionException;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.component.annotations.Component;
35 import org.codehaus.plexus.component.annotations.Requirement;
36 import org.codehaus.plexus.util.StringUtils;
37
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @Component( role = LifecycleTaskSegmentCalculator.class )
55 public class DefaultLifecycleTaskSegmentCalculator
56 implements LifecycleTaskSegmentCalculator
57 {
58 @Requirement
59 private MojoDescriptorCreator mojoDescriptorCreator;
60
61 @Requirement
62 private LifecyclePluginResolver lifecyclePluginResolver;
63
64 public DefaultLifecycleTaskSegmentCalculator()
65 {
66 }
67
68 public List<TaskSegment> calculateTaskSegments( MavenSession session )
69 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
70 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
71 PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException
72 {
73
74 MavenProject rootProject = session.getTopLevelProject();
75
76 List<String> tasks = session.getGoals();
77
78 if ( ( tasks == null || tasks.isEmpty() ) && !StringUtils.isEmpty( rootProject.getDefaultGoal() ) )
79 {
80 tasks = Arrays.asList( StringUtils.split( rootProject.getDefaultGoal() ) );
81 }
82
83 return calculateTaskSegments( session, tasks );
84 }
85
86 public List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
87 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
88 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
89 PluginVersionResolutionException
90 {
91 List<TaskSegment> taskSegments = new ArrayList<>( tasks.size() );
92
93 TaskSegment currentSegment = null;
94
95 for ( String task : tasks )
96 {
97 if ( isGoalSpecification( task ) )
98 {
99
100
101 lifecyclePluginResolver.resolveMissingPluginVersions( session.getTopLevelProject(), session );
102
103 MojoDescriptor mojoDescriptor =
104 mojoDescriptorCreator.getMojoDescriptor( task, session, session.getTopLevelProject() );
105
106 boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
107
108 if ( currentSegment == null || currentSegment.isAggregating() != aggregating )
109 {
110 currentSegment = new TaskSegment( aggregating );
111 taskSegments.add( currentSegment );
112 }
113
114 currentSegment.getTasks().add( new GoalTask( task ) );
115 }
116 else
117 {
118
119
120 if ( currentSegment == null || currentSegment.isAggregating() )
121 {
122 currentSegment = new TaskSegment( false );
123 taskSegments.add( currentSegment );
124 }
125
126 currentSegment.getTasks().add( new LifecycleTask( task ) );
127 }
128 }
129
130 return taskSegments;
131 }
132
133 public boolean requiresProject( MavenSession session )
134 {
135 List<String> goals = session.getGoals();
136 if ( goals != null )
137 {
138 for ( String goal : goals )
139 {
140 if ( !isGoalSpecification( goal ) )
141 {
142 return true;
143 }
144 }
145 }
146 return false;
147 }
148
149
150 private boolean isGoalSpecification( String task )
151 {
152 return task.indexOf( ':' ) >= 0;
153 }
154
155 }