1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package org.apache.maven.lifecycle.internal.stub;
17  
18  import org.apache.maven.execution.MavenSession;
19  import org.apache.maven.lifecycle.LifecycleNotFoundException;
20  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
21  import org.apache.maven.lifecycle.MavenExecutionPlan;
22  import org.apache.maven.lifecycle.internal.ExecutionPlanItem;
23  import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
24  import org.apache.maven.lifecycle.internal.ProjectBuildList;
25  import org.apache.maven.lifecycle.internal.ProjectSegment;
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.plugin.InvalidPluginDescriptorException;
28  import org.apache.maven.plugin.MojoExecution;
29  import org.apache.maven.plugin.MojoNotFoundException;
30  import org.apache.maven.plugin.PluginDescriptorParsingException;
31  import org.apache.maven.plugin.PluginNotFoundException;
32  import org.apache.maven.plugin.PluginResolutionException;
33  import org.apache.maven.plugin.descriptor.MojoDescriptor;
34  import org.apache.maven.plugin.descriptor.PluginDescriptor;
35  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
36  import org.apache.maven.plugin.version.PluginVersionResolutionException;
37  import org.apache.maven.project.MavenProject;
38  import org.codehaus.plexus.util.xml.Xpp3Dom;
39  
40  import java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.HashSet;
43  import java.util.List;
44  import java.util.Set;
45  
46  /**
47   * @author Kristian Rosenvold
48   */
49  public class LifecycleExecutionPlanCalculatorStub
50      implements LifecycleExecutionPlanCalculator
51  {
52      // clean
53  
54      public final static MojoDescriptor PRE_CLEAN = createMojoDescriptor( "pre-clean" );
55  
56      public final static MojoDescriptor CLEAN = createMojoDescriptor( "clean" );
57  
58      public final static MojoDescriptor POST_CLEAN = createMojoDescriptor( "post-clean" );
59  
60      // default (or at least some of them)
61  
62      public final static MojoDescriptor VALIDATE = createMojoDescriptor( "validate" );
63  
64      public final static MojoDescriptor INITIALIZE = createMojoDescriptor( "initialize" );
65  
66      public final static MojoDescriptor TEST_COMPILE = createMojoDescriptor( "test-compile" );
67  
68      public final static MojoDescriptor PROCESS_TEST_RESOURCES = createMojoDescriptor( "process-test-resources" );
69  
70      public final static MojoDescriptor PROCESS_RESOURCES = createMojoDescriptor( "process-resources" );
71  
72      public final static MojoDescriptor COMPILE = createMojoDescriptor( "compile", true );
73  
74      public final static MojoDescriptor TEST = createMojoDescriptor( "test" );
75  
76      public final static MojoDescriptor PACKAGE = createMojoDescriptor( "package" );
77  
78      public final static MojoDescriptor INSTALL = createMojoDescriptor( "install" );
79  
80      // site
81  
82      public final static MojoDescriptor PRE_SITE = createMojoDescriptor( "pre-site" );
83  
84      public final static MojoDescriptor SITE = createMojoDescriptor( "site" );
85  
86      public final static MojoDescriptor POST_SITE = createMojoDescriptor( "post-site" );
87  
88      public final static MojoDescriptor SITE_DEPLOY = createMojoDescriptor( "site-deploy" );
89  
90  
91      public int getNumberOfExceutions( ProjectBuildList projectBuildList )
92          throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
93          NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
94          LifecyclePhaseNotFoundException, LifecycleNotFoundException
95      {
96          int result = 0;
97          for ( ProjectSegment projectBuild : projectBuildList )
98          {
99              MavenExecutionPlan plan = calculateExecutionPlan( projectBuild.getSession(), projectBuild.getProject(),
100                                                               projectBuild.getTaskSegment().getTasks() );
101             result += plan.size();
102         }
103         return result;
104     }
105 
106     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
107         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
108         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
109         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
110     {
111         // Maybe do something ?
112     }
113 
114     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks )
115         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
116         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
117         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
118     {
119         if ( project.equals( ProjectDependencyGraphStub.A ) )
120         {
121             return getProjectAExceutionPlan();
122         }
123         if ( project.equals( ProjectDependencyGraphStub.B ) )
124         {
125             return getProjectBExecutionPlan();
126         }
127         // The remaining are basically "for future expansion"
128         List<MojoExecution> me = new ArrayList<MojoExecution>();
129         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
130         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
131         return createExecutionPlan( project, me );
132     }
133 
134     public static MavenExecutionPlan getProjectAExceutionPlan()
135         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
136         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
137         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
138     {
139         List<MojoExecution> me = new ArrayList<MojoExecution>();
140         me.add( createMojoExecution( "initialize", "default-initialize", INITIALIZE ) );
141         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
142         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
143         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
144         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
145         me.add( createMojoExecution( "test", "default-test", TEST ) );
146         me.add( createMojoExecution( "war", "default-war", PACKAGE ) );
147         me.add( createMojoExecution( "install", "default-install", INSTALL ) );
148         return createExecutionPlan( ProjectDependencyGraphStub.A.getExecutionProject(), me );
149     }
150 
151     public static MavenExecutionPlan getProjectBExecutionPlan()
152         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
153         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
154         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
155     {
156         List<MojoExecution> me = new ArrayList<MojoExecution>();
157         me.add( createMojoExecution( "enforce", "enforce-versions", VALIDATE ) );
158         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
159         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
160         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
161         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
162         me.add( createMojoExecution( "test", "default-test", TEST ) );
163         return createExecutionPlan( ProjectDependencyGraphStub.B.getExecutionProject(), me );
164     }
165 
166 
167     private static MavenExecutionPlan createExecutionPlan( MavenProject project, List<MojoExecution> mojoExecutions )
168         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
169         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
170         LifecyclePhaseNotFoundException, LifecycleNotFoundException
171     {
172         final List<ExecutionPlanItem> planItemList =
173             DefaultSchedulesStub.createDefaultSchedules().createExecutionPlanItem( project, mojoExecutions );
174         return new MavenExecutionPlan( planItemList, DefaultLifecyclesStub.createDefaultLifecycles() );
175     }
176 
177     private static MojoExecution createMojoExecution( String goal, String executionId, MojoDescriptor mojoDescriptor )
178     {
179         final Plugin plugin = mojoDescriptor.getPluginDescriptor().getPlugin();
180         MojoExecution result = new MojoExecution( plugin, goal, executionId );
181         result.setConfiguration( new Xpp3Dom( executionId + "-" + goal ) );
182         result.setMojoDescriptor( mojoDescriptor );
183         result.setLifecyclePhase( mojoDescriptor.getPhase() );
184 
185         return result;
186 
187     }
188 
189     public static MojoDescriptor createMojoDescriptor( String phaseName )
190     {
191         return createMojoDescriptor( phaseName, false );
192     }
193 
194     public static MojoDescriptor createMojoDescriptor( String phaseName, boolean threadSafe )
195     {
196         final MojoDescriptor mojoDescriptor = new MojoDescriptor();
197         mojoDescriptor.setPhase( phaseName );
198         final PluginDescriptor descriptor = new PluginDescriptor();
199         Plugin plugin = new Plugin();
200         plugin.setArtifactId( "org.apache.maven.test.MavenExecutionPlan" );
201         plugin.setGroupId( "stub-plugin-" + phaseName );
202         descriptor.setPlugin( plugin );
203         descriptor.setArtifactId( "artifact." + phaseName );
204         mojoDescriptor.setPluginDescriptor( descriptor );
205         mojoDescriptor.setThreadSafe( threadSafe );
206         return mojoDescriptor;
207     }
208 
209 
210     public static Set<String> getScopes()
211     {
212         return new HashSet<String>( Arrays.asList( "compile" ) );
213     }
214 
215 }