1   package org.apache.maven;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.execution.MavenExecutionRequest;
26  import org.apache.maven.execution.MavenExecutionResult;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.model.Dependency;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.PlexusContainer;
31  import org.codehaus.plexus.component.repository.ComponentDescriptor;
32  
33  public class MavenLifecycleParticipantTest
34      extends AbstractCoreMavenComponentTestCase
35  {
36  
37      private static final String INJECTED_ARTIFACT_ID = "injected";
38  
39      public static class InjectDependencyLifecycleListener
40          extends AbstractMavenLifecycleParticipant
41      {
42  
43          @Override
44          public void afterProjectsRead( MavenSession session )
45          {
46              MavenProject project = session.getProjects().get( 0 );
47  
48              Dependency dependency = new Dependency();
49              dependency.setArtifactId( INJECTED_ARTIFACT_ID );
50              dependency.setGroupId( "foo" );
51              dependency.setVersion( "1.2.3" );
52              dependency.setScope( "system" );
53              try
54              {
55                  dependency.setSystemPath( new File(
56                                                      "src/test/projects/lifecycle-executor/project-with-additional-lifecycle-elements/pom.xml" ).getCanonicalPath() );
57              }
58              catch ( IOException e )
59              {
60                  throw new RuntimeException( e );
61              }
62  
63              project.getModel().addDependency( dependency );
64          }
65  
66          @Override
67          public void afterSessionStart( MavenSession session )
68          {
69              session.getUserProperties().setProperty( "injected", "bar" );
70          }
71  
72      }
73  
74      @Override
75      protected void setupContainer()
76      {
77          super.setupContainer();
78      }
79  
80      @Override
81      protected String getProjectsDirectory()
82      {
83          return "src/test/projects/lifecycle-listener";
84      }
85  
86      public void testDependencyInjection()
87          throws Exception
88      {
89          PlexusContainer container = getContainer();
90  
91          ComponentDescriptor cd =
92              new ComponentDescriptor( InjectDependencyLifecycleListener.class, container.getContainerRealm() );
93          cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
94          container.addComponentDescriptor( cd );
95  
96          Maven maven = container.lookup( Maven.class );
97          File pom = getProject( "lifecycle-listener-dependency-injection" );
98          MavenExecutionRequest request = createMavenExecutionRequest( pom );
99          request.setGoals( Arrays.asList( "validate" ) );
100         MavenExecutionResult result = maven.execute( request );
101 
102         assertFalse( result.getExceptions().toString(), result.hasExceptions() );
103 
104         MavenProject project = result.getProject();
105         
106         assertEquals( "bar", project.getProperties().getProperty( "foo" ) );
107 
108         ArrayList<Artifact> artifacts = new ArrayList<Artifact>( project.getArtifacts() );
109 
110         assertEquals( 1, artifacts.size() );
111         assertEquals( INJECTED_ARTIFACT_ID, artifacts.get( 0 ).getArtifactId() );
112     }
113 }