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() )
79 {
80 if ( !StringUtils.isEmpty( rootProject.getDefaultGoal() ) )
81 {
82 tasks = Arrays.asList( StringUtils.split( rootProject.getDefaultGoal() ) );
83 }
84 }
85
86 return calculateTaskSegments( session, tasks );
87 }
88
89 public List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
90 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
91 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
92 PluginVersionResolutionException
93 {
94 List<TaskSegment> taskSegments = new ArrayList<TaskSegment>( tasks.size() );
95
96 TaskSegment currentSegment = null;
97
98 for ( String task : tasks )
99 {
100 if ( isGoalSpecification( task ) )
101 {
102
103
104 lifecyclePluginResolver.resolveMissingPluginVersions( session.getTopLevelProject(), session );
105
106 MojoDescriptor mojoDescriptor =
107 mojoDescriptorCreator.getMojoDescriptor( task, session, session.getTopLevelProject() );
108
109 boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
110
111 if ( currentSegment == null || currentSegment.isAggregating() != aggregating )
112 {
113 currentSegment = new TaskSegment( aggregating );
114 taskSegments.add( currentSegment );
115 }
116
117 currentSegment.getTasks().add( new GoalTask( task ) );
118 }
119 else
120 {
121
122
123 if ( currentSegment == null || currentSegment.isAggregating() )
124 {
125 currentSegment = new TaskSegment( false );
126 taskSegments.add( currentSegment );
127 }
128
129 currentSegment.getTasks().add( new LifecycleTask( task ) );
130 }
131 }
132
133 return taskSegments;
134 }
135
136 public boolean requiresProject( MavenSession session )
137 {
138 List<String> goals = session.getGoals();
139 if ( goals != null )
140 {
141 for ( String goal : goals )
142 {
143 if ( !isGoalSpecification( goal ) )
144 {
145 return true;
146 }
147 }
148 }
149 return false;
150 }
151
152
153 private boolean isGoalSpecification( String task )
154 {
155 return task.indexOf( ':' ) >= 0;
156 }
157
158 }