View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.execution.MavenExecutionRequest;
29  import org.apache.maven.execution.MavenExecutionResult;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.model.Dependency;
32  import org.apache.maven.project.MavenProject;
33  import org.codehaus.plexus.PlexusContainer;
34  import org.codehaus.plexus.component.repository.ComponentDescriptor;
35  
36  public class MavenLifecycleParticipantTest extends AbstractCoreMavenComponentTestCase {
37  
38      private static final String INJECTED_ARTIFACT_ID = "injected";
39  
40      public static class InjectDependencyLifecycleListener extends AbstractMavenLifecycleParticipant {
41  
42          @Override
43          public void afterProjectsRead(MavenSession session) {
44              MavenProject project = session.getProjects().get(0);
45  
46              Dependency dependency = new Dependency();
47              dependency.setArtifactId(INJECTED_ARTIFACT_ID);
48              dependency.setGroupId("foo");
49              dependency.setVersion("1.2.3");
50              dependency.setScope("system");
51              try {
52                  dependency.setSystemPath(new File(
53                                  "src/test/projects/lifecycle-executor/project-with-additional-lifecycle-elements/pom.xml")
54                          .getCanonicalPath());
55              } catch (IOException e) {
56                  throw new RuntimeException(e);
57              }
58  
59              project.getModel().addDependency(dependency);
60          }
61  
62          @Override
63          public void afterSessionStart(MavenSession session) {
64              session.getUserProperties().setProperty("injected", "bar");
65          }
66      }
67  
68      public static class InjectReactorDependency extends AbstractMavenLifecycleParticipant {
69          @Override
70          public void afterProjectsRead(MavenSession session) {
71              injectReactorDependency(session.getProjects(), "module-a", "module-b");
72          }
73  
74          private void injectReactorDependency(List<MavenProject> projects, String moduleFrom, String moduleTo) {
75              for (MavenProject project : projects) {
76                  if (moduleFrom.equals(project.getArtifactId())) {
77                      Dependency dependency = new Dependency();
78                      dependency.setArtifactId(moduleTo);
79                      dependency.setGroupId(project.getGroupId());
80                      dependency.setVersion(project.getVersion());
81  
82                      project.getModel().addDependency(dependency);
83                  }
84              }
85          }
86      }
87  
88      @Override
89      protected void setupContainer() {
90          super.setupContainer();
91      }
92  
93      @Override
94      protected String getProjectsDirectory() {
95          return "src/test/projects/lifecycle-listener";
96      }
97  
98      public void testDependencyInjection() throws Exception {
99          PlexusContainer container = getContainer();
100 
101         ComponentDescriptor<? extends AbstractMavenLifecycleParticipant> cd =
102                 new ComponentDescriptor<>(InjectDependencyLifecycleListener.class, container.getContainerRealm());
103         cd.setRoleClass(AbstractMavenLifecycleParticipant.class);
104         container.addComponentDescriptor(cd);
105 
106         Maven maven = container.lookup(Maven.class);
107         File pom = getProject("lifecycle-listener-dependency-injection");
108         MavenExecutionRequest request = createMavenExecutionRequest(pom);
109         request.setGoals(Arrays.asList("validate"));
110         MavenExecutionResult result = maven.execute(request);
111 
112         assertFalse(result.getExceptions().toString(), result.hasExceptions());
113 
114         MavenProject project = result.getProject();
115 
116         assertEquals("bar", project.getProperties().getProperty("foo"));
117 
118         ArrayList<Artifact> artifacts = new ArrayList<>(project.getArtifacts());
119 
120         assertEquals(1, artifacts.size());
121         assertEquals(INJECTED_ARTIFACT_ID, artifacts.get(0).getArtifactId());
122     }
123 
124     public void testReactorDependencyInjection() throws Exception {
125         List<String> reactorOrder =
126                 getReactorOrder("lifecycle-participant-reactor-dependency-injection", InjectReactorDependency.class);
127         assertEquals(Arrays.asList("parent", "module-b", "module-a"), reactorOrder);
128     }
129 
130     private <T> List<String> getReactorOrder(String testProject, Class<T> participant) throws Exception {
131         PlexusContainer container = getContainer();
132 
133         ComponentDescriptor<T> cd = new ComponentDescriptor<>(participant, container.getContainerRealm());
134         cd.setRoleClass(AbstractMavenLifecycleParticipant.class);
135         container.addComponentDescriptor(cd);
136 
137         Maven maven = container.lookup(Maven.class);
138         File pom = getProject(testProject);
139         MavenExecutionRequest request = createMavenExecutionRequest(pom);
140         request.setGoals(Arrays.asList("validate"));
141         MavenExecutionResult result = maven.execute(request);
142 
143         assertFalse(result.getExceptions().toString(), result.hasExceptions());
144 
145         List<String> order = new ArrayList<>();
146         for (MavenProject project : result.getTopologicallySortedProjects()) {
147             order.add(project.getArtifactId());
148         }
149         return order;
150     }
151 }