001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
003     * agreements. See the NOTICE file distributed with this work for additional information regarding
004     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
005     * "License"); you may not use this file except in compliance with the License. You may obtain a
006     * copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software distributed under the License
011     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing permissions and limitations under
013     * the License.
014     */
015    
016    package org.apache.maven.lifecycle.internal.stub;
017    
018    import org.apache.maven.execution.MavenSession;
019    import org.apache.maven.lifecycle.LifecycleNotFoundException;
020    import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
021    import org.apache.maven.lifecycle.MavenExecutionPlan;
022    import org.apache.maven.lifecycle.internal.ExecutionPlanItem;
023    import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
024    import org.apache.maven.lifecycle.internal.ProjectBuildList;
025    import org.apache.maven.lifecycle.internal.ProjectSegment;
026    import org.apache.maven.model.Plugin;
027    import org.apache.maven.plugin.InvalidPluginDescriptorException;
028    import org.apache.maven.plugin.MojoExecution;
029    import org.apache.maven.plugin.MojoNotFoundException;
030    import org.apache.maven.plugin.PluginDescriptorParsingException;
031    import org.apache.maven.plugin.PluginNotFoundException;
032    import org.apache.maven.plugin.PluginResolutionException;
033    import org.apache.maven.plugin.descriptor.MojoDescriptor;
034    import org.apache.maven.plugin.descriptor.PluginDescriptor;
035    import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
036    import org.apache.maven.plugin.version.PluginVersionResolutionException;
037    import org.apache.maven.project.MavenProject;
038    import org.codehaus.plexus.util.xml.Xpp3Dom;
039    
040    import java.util.ArrayList;
041    import java.util.Arrays;
042    import java.util.HashSet;
043    import java.util.List;
044    import java.util.Set;
045    
046    /**
047     * @author Kristian Rosenvold
048     */
049    public class LifecycleExecutionPlanCalculatorStub
050        implements LifecycleExecutionPlanCalculator
051    {
052        // clean
053    
054        public final static MojoDescriptor PRE_CLEAN = createMojoDescriptor( "pre-clean" );
055    
056        public final static MojoDescriptor CLEAN = createMojoDescriptor( "clean" );
057    
058        public final static MojoDescriptor POST_CLEAN = createMojoDescriptor( "post-clean" );
059    
060        // default (or at least some of them)
061    
062        public final static MojoDescriptor VALIDATE = createMojoDescriptor( "validate" );
063    
064        public final static MojoDescriptor INITIALIZE = createMojoDescriptor( "initialize" );
065    
066        public final static MojoDescriptor TEST_COMPILE = createMojoDescriptor( "test-compile" );
067    
068        public final static MojoDescriptor PROCESS_TEST_RESOURCES = createMojoDescriptor( "process-test-resources" );
069    
070        public final static MojoDescriptor PROCESS_RESOURCES = createMojoDescriptor( "process-resources" );
071    
072        public final static MojoDescriptor COMPILE = createMojoDescriptor( "compile", true );
073    
074        public final static MojoDescriptor TEST = createMojoDescriptor( "test" );
075    
076        public final static MojoDescriptor PACKAGE = createMojoDescriptor( "package" );
077    
078        public final static MojoDescriptor INSTALL = createMojoDescriptor( "install" );
079    
080        // site
081    
082        public final static MojoDescriptor PRE_SITE = createMojoDescriptor( "pre-site" );
083    
084        public final static MojoDescriptor SITE = createMojoDescriptor( "site" );
085    
086        public final static MojoDescriptor POST_SITE = createMojoDescriptor( "post-site" );
087    
088        public final static MojoDescriptor SITE_DEPLOY = createMojoDescriptor( "site-deploy" );
089    
090    
091        public int getNumberOfExceutions( ProjectBuildList projectBuildList )
092            throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
093            NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
094            LifecyclePhaseNotFoundException, LifecycleNotFoundException
095        {
096            int result = 0;
097            for ( ProjectSegment projectBuild : projectBuildList )
098            {
099                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                                                          boolean setup )
116            throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
117            PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
118            NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
119        {
120            if ( project.equals( ProjectDependencyGraphStub.A ) )
121            {
122                return getProjectAExceutionPlan();
123            }
124            if ( project.equals( ProjectDependencyGraphStub.B ) )
125            {
126                return getProjectBExecutionPlan();
127            }
128            // The remaining are basically "for future expansion"
129            List<MojoExecution> me = new ArrayList<MojoExecution>();
130            me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
131            me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
132            return createExecutionPlan( project, me );
133        }
134    
135        public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks )
136            throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
137            PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
138            NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
139        {
140            return calculateExecutionPlan( session, project, tasks, true );
141        }
142    
143        public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution )
144            throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
145            MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
146            LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
147        {
148        }
149    
150        public static MavenExecutionPlan getProjectAExceutionPlan()
151            throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
152            PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
153            NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
154        {
155            List<MojoExecution> me = new ArrayList<MojoExecution>();
156            me.add( createMojoExecution( "initialize", "default-initialize", INITIALIZE ) );
157            me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
158            me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
159            me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
160            me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
161            me.add( createMojoExecution( "test", "default-test", TEST ) );
162            me.add( createMojoExecution( "war", "default-war", PACKAGE ) );
163            me.add( createMojoExecution( "install", "default-install", INSTALL ) );
164            return createExecutionPlan( ProjectDependencyGraphStub.A.getExecutionProject(), me );
165        }
166    
167        public static MavenExecutionPlan getProjectBExecutionPlan()
168            throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
169            PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
170            NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
171        {
172            List<MojoExecution> me = new ArrayList<MojoExecution>();
173            me.add( createMojoExecution( "enforce", "enforce-versions", VALIDATE ) );
174            me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
175            me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
176            me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
177            me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
178            me.add( createMojoExecution( "test", "default-test", TEST ) );
179            return createExecutionPlan( ProjectDependencyGraphStub.B.getExecutionProject(), me );
180        }
181    
182    
183        private static MavenExecutionPlan createExecutionPlan( MavenProject project, List<MojoExecution> mojoExecutions )
184            throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
185            NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
186            LifecyclePhaseNotFoundException, LifecycleNotFoundException
187        {
188            final List<ExecutionPlanItem> planItemList =
189                DefaultSchedulesStub.createDefaultSchedules().createExecutionPlanItem( project, mojoExecutions );
190            return new MavenExecutionPlan( planItemList, DefaultLifecyclesStub.createDefaultLifecycles() );
191        }
192    
193        private static MojoExecution createMojoExecution( String goal, String executionId, MojoDescriptor mojoDescriptor )
194        {
195            final Plugin plugin = mojoDescriptor.getPluginDescriptor().getPlugin();
196            MojoExecution result = new MojoExecution( plugin, goal, executionId );
197            result.setConfiguration( new Xpp3Dom( executionId + "-" + goal ) );
198            result.setMojoDescriptor( mojoDescriptor );
199            result.setLifecyclePhase( mojoDescriptor.getPhase() );
200    
201            return result;
202    
203        }
204    
205        public static MojoDescriptor createMojoDescriptor( String phaseName )
206        {
207            return createMojoDescriptor( phaseName, false );
208        }
209    
210        public static MojoDescriptor createMojoDescriptor( String phaseName, boolean threadSafe )
211        {
212            final MojoDescriptor mojoDescriptor = new MojoDescriptor();
213            mojoDescriptor.setPhase( phaseName );
214            final PluginDescriptor descriptor = new PluginDescriptor();
215            Plugin plugin = new Plugin();
216            plugin.setArtifactId( "org.apache.maven.test.MavenExecutionPlan" );
217            plugin.setGroupId( "stub-plugin-" + phaseName );
218            descriptor.setPlugin( plugin );
219            descriptor.setArtifactId( "artifact." + phaseName );
220            mojoDescriptor.setPluginDescriptor( descriptor );
221            mojoDescriptor.setThreadSafe( threadSafe );
222            return mojoDescriptor;
223        }
224    
225    
226        public static Set<String> getScopes()
227        {
228            return new HashSet<String>( Arrays.asList( "compile" ) );
229        }
230    
231    }