001 package org.apache.maven.lifecycle; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license 005 * agreements. See the NOTICE file distributed with this work for additional information regarding 006 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance with the License. You may obtain a 008 * copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed under the License 013 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 014 * or implied. See the License for the specific language governing permissions and limitations under 015 * the License. 016 */ 017 018 import java.io.File; 019 import java.util.ArrayList; 020 import java.util.Arrays; 021 import java.util.List; 022 023 import org.apache.maven.AbstractCoreMavenComponentTestCase; 024 import org.apache.maven.exception.ExceptionHandler; 025 import org.apache.maven.execution.MavenSession; 026 import org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator; 027 import org.apache.maven.lifecycle.internal.ExecutionPlanItem; 028 import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator; 029 import org.apache.maven.lifecycle.internal.LifecycleTask; 030 import org.apache.maven.lifecycle.internal.LifecycleTaskSegmentCalculator; 031 import org.apache.maven.lifecycle.internal.MojoDescriptorCreator; 032 import org.apache.maven.lifecycle.internal.TaskSegment; 033 import org.apache.maven.model.Plugin; 034 import org.apache.maven.plugin.MojoExecution; 035 import org.apache.maven.plugin.MojoNotFoundException; 036 import org.apache.maven.plugin.descriptor.MojoDescriptor; 037 import org.codehaus.plexus.component.annotations.Requirement; 038 import org.codehaus.plexus.util.xml.Xpp3Dom; 039 040 public class LifecycleExecutorTest 041 extends AbstractCoreMavenComponentTestCase 042 { 043 @Requirement 044 private DefaultLifecycleExecutor lifecycleExecutor; 045 046 @Requirement 047 private DefaultLifecycleTaskSegmentCalculator lifeCycleTaskSegmentCalculator; 048 049 @Requirement 050 private LifecycleExecutionPlanCalculator lifeCycleExecutionPlanCalculator; 051 052 @Requirement 053 private MojoDescriptorCreator mojoDescriptorCreator; 054 055 056 protected void setUp() 057 throws Exception 058 { 059 super.setUp(); 060 lifecycleExecutor = (DefaultLifecycleExecutor) lookup( LifecycleExecutor.class ); 061 lifeCycleTaskSegmentCalculator = 062 (DefaultLifecycleTaskSegmentCalculator) lookup( LifecycleTaskSegmentCalculator.class ); 063 lifeCycleExecutionPlanCalculator = lookup( LifecycleExecutionPlanCalculator.class ); 064 mojoDescriptorCreator = lookup( MojoDescriptorCreator.class ); 065 lookup( ExceptionHandler.class ); 066 } 067 068 @Override 069 protected void tearDown() 070 throws Exception 071 { 072 lifecycleExecutor = null; 073 super.tearDown(); 074 } 075 076 protected String getProjectsDirectory() 077 { 078 return "src/test/projects/lifecycle-executor"; 079 } 080 081 // ----------------------------------------------------------------------------------------------- 082 // Tests which exercise the lifecycle executor when it is dealing with default lifecycle phases. 083 // ----------------------------------------------------------------------------------------------- 084 085 public void testCalculationOfBuildPlanWithIndividualTaskWherePluginIsSpecifiedInThePom() 086 throws Exception 087 { 088 // We are doing something like "mvn resources:resources" where no version is specified but this 089 // project we are working on has the version specified in the POM so the version should come from there. 090 File pom = getProject( "project-basic" ); 091 MavenSession session = createMavenSession( pom ); 092 assertEquals( "project-basic", session.getCurrentProject().getArtifactId() ); 093 assertEquals( "1.0", session.getCurrentProject().getVersion() ); 094 List<MojoExecution> executionPlan = getExecutions( calculateExecutionPlan( session, "resources:resources" ) ); 095 assertEquals( 1, executionPlan.size() ); 096 MojoExecution mojoExecution = executionPlan.get( 0 ); 097 assertNotNull( mojoExecution ); 098 assertEquals( "org.apache.maven.plugins", 099 mojoExecution.getMojoDescriptor().getPluginDescriptor().getGroupId() ); 100 assertEquals( "maven-resources-plugin", 101 mojoExecution.getMojoDescriptor().getPluginDescriptor().getArtifactId() ); 102 assertEquals( "0.1", mojoExecution.getMojoDescriptor().getPluginDescriptor().getVersion() ); 103 } 104 105 public void testCalculationOfBuildPlanWithIndividualTaskOfTheCleanLifecycle() 106 throws Exception 107 { 108 // We are doing something like "mvn clean:clean" where no version is specified but this 109 // project we are working on has the version specified in the POM so the version should come from there. 110 File pom = getProject( "project-basic" ); 111 MavenSession session = createMavenSession( pom ); 112 assertEquals( "project-basic", session.getCurrentProject().getArtifactId() ); 113 assertEquals( "1.0", session.getCurrentProject().getVersion() ); 114 List<MojoExecution> executionPlan = getExecutions( calculateExecutionPlan( session, "clean" ) ); 115 assertEquals( 1, executionPlan.size() ); 116 MojoExecution mojoExecution = executionPlan.get( 0 ); 117 assertNotNull( mojoExecution ); 118 assertEquals( "org.apache.maven.plugins", 119 mojoExecution.getMojoDescriptor().getPluginDescriptor().getGroupId() ); 120 assertEquals( "maven-clean-plugin", mojoExecution.getMojoDescriptor().getPluginDescriptor().getArtifactId() ); 121 assertEquals( "0.1", mojoExecution.getMojoDescriptor().getPluginDescriptor().getVersion() ); 122 } 123 124 public void testCalculationOfBuildPlanWithIndividualTaskOfTheCleanCleanGoal() 125 throws Exception 126 { 127 // We are doing something like "mvn clean:clean" where no version is specified but this 128 // project we are working on has the version specified in the POM so the version should come from there. 129 File pom = getProject( "project-basic" ); 130 MavenSession session = createMavenSession( pom ); 131 assertEquals( "project-basic", session.getCurrentProject().getArtifactId() ); 132 assertEquals( "1.0", session.getCurrentProject().getVersion() ); 133 List<MojoExecution> executionPlan = getExecutions( calculateExecutionPlan( session, "clean:clean" ) ); 134 assertEquals( 1, executionPlan.size() ); 135 MojoExecution mojoExecution = executionPlan.get( 0 ); 136 assertNotNull( mojoExecution ); 137 assertEquals( "org.apache.maven.plugins", 138 mojoExecution.getMojoDescriptor().getPluginDescriptor().getGroupId() ); 139 assertEquals( "maven-clean-plugin", mojoExecution.getMojoDescriptor().getPluginDescriptor().getArtifactId() ); 140 assertEquals( "0.1", mojoExecution.getMojoDescriptor().getPluginDescriptor().getVersion() ); 141 } 142 143 List<MojoExecution> getExecutions( MavenExecutionPlan mavenExecutionPlan ) 144 { 145 List<MojoExecution> result = new ArrayList<MojoExecution>(); 146 for ( ExecutionPlanItem executionPlanItem : mavenExecutionPlan ) 147 { 148 result.add( executionPlanItem.getMojoExecution() ); 149 } 150 return result; 151 } 152 153 // We need to take in multiple lifecycles 154 public void testCalculationOfBuildPlanTasksOfTheCleanLifecycleAndTheInstallLifecycle() 155 throws Exception 156 { 157 File pom = getProject( "project-with-additional-lifecycle-elements" ); 158 MavenSession session = createMavenSession( pom ); 159 assertEquals( "project-with-additional-lifecycle-elements", session.getCurrentProject().getArtifactId() ); 160 assertEquals( "1.0", session.getCurrentProject().getVersion() ); 161 List<MojoExecution> executionPlan = getExecutions( calculateExecutionPlan( session, "clean", "install" ) ); 162 163 //[01] clean:clean 164 //[02] resources:resources 165 //[03] compiler:compile 166 //[04] it:generate-metadata 167 //[05] resources:testResources 168 //[06] compiler:testCompile 169 //[07] it:generate-test-metadata 170 //[08] surefire:test 171 //[09] jar:jar 172 //[10] install:install 173 // 174 assertEquals( 10, executionPlan.size() ); 175 176 assertEquals( "clean:clean", executionPlan.get( 0 ).getMojoDescriptor().getFullGoalName() ); 177 assertEquals( "resources:resources", executionPlan.get( 1 ).getMojoDescriptor().getFullGoalName() ); 178 assertEquals( "compiler:compile", executionPlan.get( 2 ).getMojoDescriptor().getFullGoalName() ); 179 assertEquals( "it:generate-metadata", executionPlan.get( 3 ).getMojoDescriptor().getFullGoalName() ); 180 assertEquals( "resources:testResources", executionPlan.get( 4 ).getMojoDescriptor().getFullGoalName() ); 181 assertEquals( "compiler:testCompile", executionPlan.get( 5 ).getMojoDescriptor().getFullGoalName() ); 182 assertEquals( "it:generate-test-metadata", executionPlan.get( 6 ).getMojoDescriptor().getFullGoalName() ); 183 assertEquals( "surefire:test", executionPlan.get( 7 ).getMojoDescriptor().getFullGoalName() ); 184 assertEquals( "jar:jar", executionPlan.get( 8 ).getMojoDescriptor().getFullGoalName() ); 185 assertEquals( "install:install", executionPlan.get( 9 ).getMojoDescriptor().getFullGoalName() ); 186 } 187 188 // We need to take in multiple lifecycles 189 public void testCalculationOfBuildPlanWithMultipleExecutionsOfModello() 190 throws Exception 191 { 192 File pom = getProject( "project-with-multiple-executions" ); 193 MavenSession session = createMavenSession( pom ); 194 assertEquals( "project-with-multiple-executions", session.getCurrentProject().getArtifactId() ); 195 assertEquals( "1.0.1", session.getCurrentProject().getVersion() ); 196 197 MavenExecutionPlan plan = calculateExecutionPlan( session, "clean", "install" ); 198 199 List<MojoExecution> executions = getExecutions( plan ); 200 201 //[01] clean:clean 202 //[02] modello:xpp3-writer 203 //[03] modello:java 204 //[04] modello:xpp3-reader 205 //[05] modello:xpp3-writer 206 //[06] modello:java 207 //[07] modello:xpp3-reader 208 //[08] plugin:descriptor 209 //[09] resources:resources 210 //[10] compiler:compile 211 //[11] resources:testResources 212 //[12] compiler:testCompile 213 //[13] surefire:test 214 //[14] jar:jar 215 //[15] plugin:addPluginArtifactMetadata 216 //[16] install:install 217 // 218 219 assertEquals( 16, executions.size() ); 220 221 assertEquals( "clean:clean", executions.get( 0 ).getMojoDescriptor().getFullGoalName() ); 222 assertEquals( "it:xpp3-writer", executions.get( 1 ).getMojoDescriptor().getFullGoalName() ); 223 assertEquals( "it:java", executions.get( 2 ).getMojoDescriptor().getFullGoalName() ); 224 assertEquals( "it:xpp3-reader", executions.get( 3 ).getMojoDescriptor().getFullGoalName() ); 225 assertEquals( "it:xpp3-writer", executions.get( 4 ).getMojoDescriptor().getFullGoalName() ); 226 assertEquals( "it:java", executions.get( 5 ).getMojoDescriptor().getFullGoalName() ); 227 assertEquals( "it:xpp3-reader", executions.get( 6 ).getMojoDescriptor().getFullGoalName() ); 228 assertEquals( "plugin:descriptor", executions.get( 7 ).getMojoDescriptor().getFullGoalName() ); 229 assertEquals( "resources:resources", executions.get( 8 ).getMojoDescriptor().getFullGoalName() ); 230 assertEquals( "compiler:compile", executions.get( 9 ).getMojoDescriptor().getFullGoalName() ); 231 assertEquals( "resources:testResources", executions.get( 10 ).getMojoDescriptor().getFullGoalName() ); 232 assertEquals( "compiler:testCompile", executions.get( 11 ).getMojoDescriptor().getFullGoalName() ); 233 assertEquals( "surefire:test", executions.get( 12 ).getMojoDescriptor().getFullGoalName() ); 234 assertEquals( "jar:jar", executions.get( 13 ).getMojoDescriptor().getFullGoalName() ); 235 assertEquals( "plugin:addPluginArtifactMetadata", executions.get( 14 ).getMojoDescriptor().getFullGoalName() ); 236 assertEquals( "install:install", executions.get( 15 ).getMojoDescriptor().getFullGoalName() ); 237 238 assertEquals( "src/main/mdo/remote-resources.mdo", 239 new MojoExecutionXPathContainer( executions.get( 1 ) ).getValue( 240 "configuration/models[1]/model" ) ); 241 assertEquals( "src/main/mdo/supplemental-model.mdo", 242 new MojoExecutionXPathContainer( executions.get( 4 ) ).getValue( 243 "configuration/models[1]/model" ) ); 244 } 245 246 public void testLifecycleQueryingUsingADefaultLifecyclePhase() 247 throws Exception 248 { 249 File pom = getProject( "project-with-additional-lifecycle-elements" ); 250 MavenSession session = createMavenSession( pom ); 251 assertEquals( "project-with-additional-lifecycle-elements", session.getCurrentProject().getArtifactId() ); 252 assertEquals( "1.0", session.getCurrentProject().getVersion() ); 253 List<MojoExecution> executionPlan = getExecutions( calculateExecutionPlan( session, "package" ) ); 254 255 //[01] resources:resources 256 //[02] compiler:compile 257 //[03] it:generate-metadata 258 //[04] resources:testResources 259 //[05] compiler:testCompile 260 //[06] plexus-component-metadata:generate-test-metadata 261 //[07] surefire:test 262 //[08] jar:jar 263 // 264 assertEquals( 8, executionPlan.size() ); 265 266 assertEquals( "resources:resources", executionPlan.get( 0 ).getMojoDescriptor().getFullGoalName() ); 267 assertEquals( "compiler:compile", executionPlan.get( 1 ).getMojoDescriptor().getFullGoalName() ); 268 assertEquals( "it:generate-metadata", executionPlan.get( 2 ).getMojoDescriptor().getFullGoalName() ); 269 assertEquals( "resources:testResources", executionPlan.get( 3 ).getMojoDescriptor().getFullGoalName() ); 270 assertEquals( "compiler:testCompile", executionPlan.get( 4 ).getMojoDescriptor().getFullGoalName() ); 271 assertEquals( "it:generate-test-metadata", executionPlan.get( 5 ).getMojoDescriptor().getFullGoalName() ); 272 assertEquals( "surefire:test", executionPlan.get( 6 ).getMojoDescriptor().getFullGoalName() ); 273 assertEquals( "jar:jar", executionPlan.get( 7 ).getMojoDescriptor().getFullGoalName() ); 274 } 275 276 public void testLifecyclePluginsRetrievalForDefaultLifecycle() 277 throws Exception 278 { 279 List<Plugin> plugins = 280 new ArrayList<Plugin>( lifecycleExecutor.getPluginsBoundByDefaultToAllLifecycles( "jar" ) ); 281 282 assertEquals( 8, plugins.size() ); 283 } 284 285 public void testPluginConfigurationCreation() 286 throws Exception 287 { 288 File pom = getProject( "project-with-additional-lifecycle-elements" ); 289 MavenSession session = createMavenSession( pom ); 290 MojoDescriptor mojoDescriptor = 291 mojoDescriptorCreator.getMojoDescriptor( "org.apache.maven.its.plugins:maven-it-plugin:0.1:java", session, 292 session.getCurrentProject() ); 293 Xpp3Dom dom = MojoDescriptorCreator.convert( mojoDescriptor ); 294 System.out.println( dom ); 295 } 296 297 MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks ) 298 throws Exception 299 { 300 List<TaskSegment> taskSegments = 301 lifeCycleTaskSegmentCalculator.calculateTaskSegments( session, Arrays.asList( tasks ) ); 302 303 TaskSegment mergedSegment = new TaskSegment( false ); 304 305 for ( TaskSegment taskSegment : taskSegments ) 306 { 307 mergedSegment.getTasks().addAll( taskSegment.getTasks() ); 308 } 309 310 return lifeCycleExecutionPlanCalculator.calculateExecutionPlan( session, session.getCurrentProject(), 311 mergedSegment.getTasks() ); 312 } 313 314 public void testInvalidGoalName() 315 throws Exception 316 { 317 File pom = getProject( "project-basic" ); 318 MavenSession session = createMavenSession( pom ); 319 try 320 { 321 getExecutions( calculateExecutionPlan( session, "resources:" ) ); 322 fail( "expected a MojoNotFoundException" ); 323 } 324 catch ( MojoNotFoundException e ) 325 { 326 assertEquals( "", e.getGoal() ); 327 } 328 329 try 330 { 331 getExecutions( calculateExecutionPlan( session, "org.apache.maven.plugins:maven-resources-plugin:0.1:resources:toomany" ) ); 332 fail( "expected a MojoNotFoundException" ); 333 } 334 catch ( MojoNotFoundException e ) 335 { 336 assertEquals( "resources:toomany", e.getGoal() ); 337 } 338 } 339 340 341 public void testPluginPrefixRetrieval() 342 throws Exception 343 { 344 File pom = getProject( "project-basic" ); 345 MavenSession session = createMavenSession( pom ); 346 Plugin plugin = mojoDescriptorCreator.findPluginForPrefix( "resources", session ); 347 assertEquals( "org.apache.maven.plugins", plugin.getGroupId() ); 348 assertEquals( "maven-resources-plugin", plugin.getArtifactId() ); 349 } 350 351 // Prefixes 352 353 public void testFindingPluginPrefixforCleanClean() 354 throws Exception 355 { 356 File pom = getProject( "project-basic" ); 357 MavenSession session = createMavenSession( pom ); 358 Plugin plugin = mojoDescriptorCreator.findPluginForPrefix( "clean", session ); 359 assertNotNull( plugin ); 360 } 361 362 public void testSetupMojoExecution() 363 throws Exception 364 { 365 File pom = getProject( "mojo-configuration" ); 366 367 MavenSession session = createMavenSession( pom ); 368 369 LifecycleTask task = new LifecycleTask( "generate-sources" ); 370 MavenExecutionPlan executionPlan = 371 lifeCycleExecutionPlanCalculator.calculateExecutionPlan( session, session.getCurrentProject(), 372 Arrays.asList( (Object) task ), false ); 373 374 MojoExecution execution = executionPlan.getMojoExecutions().get(0); 375 assertEquals(execution.toString(), "maven-it-plugin", execution.getArtifactId()); 376 assertNull(execution.getConfiguration()); 377 378 lifeCycleExecutionPlanCalculator.setupMojoExecution( session, session.getCurrentProject(), execution ); 379 assertNotNull(execution.getConfiguration()); 380 assertEquals("1.0", execution.getConfiguration().getChild( "version" ).getAttribute( "default-value" )); 381 } 382 383 }