View Javadoc
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  import java.io.File;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
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      public static class InjectReactorDependency
75          extends AbstractMavenLifecycleParticipant
76      {
77          @Override
78          public void afterProjectsRead( MavenSession session )
79          {
80              injectReactorDependency( session.getProjects(), "module-a", "module-b" );
81          }
82  
83          private void injectReactorDependency( List<MavenProject> projects, String moduleFrom, String moduleTo )
84          {
85              for ( MavenProject project : projects )
86              {
87                  if ( moduleFrom.equals( project.getArtifactId() ) )
88                  {
89                      Dependency dependency = new Dependency();
90                      dependency.setArtifactId( moduleTo );
91                      dependency.setGroupId( project.getGroupId() );
92                      dependency.setVersion( project.getVersion() );
93  
94                      project.getModel().addDependency( dependency );
95                  }
96              }
97          }
98      }
99  
100     @Override
101     protected void setupContainer()
102     {
103         super.setupContainer();
104     }
105 
106     @Override
107     protected String getProjectsDirectory()
108     {
109         return "src/test/projects/lifecycle-listener";
110     }
111 
112     public void testDependencyInjection()
113         throws Exception
114     {
115         PlexusContainer container = getContainer();
116 
117         ComponentDescriptor<? extends AbstractMavenLifecycleParticipant> cd =
118             new ComponentDescriptor<>( InjectDependencyLifecycleListener.class,
119                                                                         container.getContainerRealm() );
120         cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
121         container.addComponentDescriptor( cd );
122 
123         Maven maven = container.lookup( Maven.class );
124         File pom = getProject( "lifecycle-listener-dependency-injection" );
125         MavenExecutionRequest request = createMavenExecutionRequest( pom );
126         request.setGoals( Arrays.asList( "validate" ) );
127         MavenExecutionResult result = maven.execute( request );
128 
129         assertFalse( result.getExceptions().toString(), result.hasExceptions() );
130 
131         MavenProject project = result.getProject();
132 
133         assertEquals( "bar", project.getProperties().getProperty( "foo" ) );
134 
135         ArrayList<Artifact> artifacts = new ArrayList<>( project.getArtifacts() );
136 
137         assertEquals( 1, artifacts.size() );
138         assertEquals( INJECTED_ARTIFACT_ID, artifacts.get( 0 ).getArtifactId() );
139     }
140 
141     public void testReactorDependencyInjection()
142         throws Exception
143     {
144         List<String> reactorOrder =
145             getReactorOrder( "lifecycle-participant-reactor-dependency-injection", InjectReactorDependency.class );
146         assertEquals( Arrays.asList( "parent", "module-b", "module-a" ), reactorOrder );
147     }
148 
149     private <T> List<String> getReactorOrder( String testProject, Class<T> participant )
150         throws Exception
151     {
152         PlexusContainer container = getContainer();
153 
154         ComponentDescriptor<T> cd = new ComponentDescriptor<>( participant, container.getContainerRealm() );
155         cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
156         container.addComponentDescriptor( cd );
157 
158         Maven maven = container.lookup( Maven.class );
159         File pom = getProject( testProject );
160         MavenExecutionRequest request = createMavenExecutionRequest( pom );
161         request.setGoals( Arrays.asList( "validate" ) );
162         MavenExecutionResult result = maven.execute( request );
163 
164         assertFalse( result.getExceptions().toString(), result.hasExceptions() );
165 
166         List<String> order = new ArrayList<>();
167         for ( MavenProject project : result.getTopologicallySortedProjects() )
168         {
169             order.add( project.getArtifactId() );
170         }
171         return order;
172     }
173 }