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  import org.junit.jupiter.api.Test;
36  
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  import static org.junit.jupiter.api.Assertions.assertFalse;
39  import static org.junit.jupiter.api.Assertions.assertSame;
40  
41  class MavenLifecycleParticipantTest extends AbstractCoreMavenComponentTestCase {
42  
43      private static final String INJECTED_ARTIFACT_ID = "injected";
44  
45      public static class InjectDependencyLifecycleListener extends AbstractMavenLifecycleParticipant {
46  
47          @Override
48          public void afterProjectsRead(MavenSession session) {
49              MavenProject project = session.getProjects().get(0);
50  
51              Dependency dependency = new Dependency();
52              dependency.setArtifactId(INJECTED_ARTIFACT_ID);
53              dependency.setGroupId("foo");
54              dependency.setVersion("1.2.3");
55              dependency.setScope("system");
56              try {
57                  dependency.setSystemPath(new File(
58                                  "src/test/projects/lifecycle-executor/project-with-additional-lifecycle-elements/pom.xml")
59                          .getCanonicalPath());
60              } catch (IOException e) {
61                  throw new RuntimeException(e);
62              }
63  
64              project.getModel().addDependency(dependency);
65          }
66  
67          @Override
68          public void afterSessionStart(MavenSession session) {
69              session.getUserProperties().setProperty("injected", "bar");
70          }
71      }
72  
73      public static class InjectReactorDependency extends AbstractMavenLifecycleParticipant {
74          @Override
75          public void afterProjectsRead(MavenSession session) {
76              injectReactorDependency(session.getProjects(), "module-a", "module-b");
77          }
78  
79          private void injectReactorDependency(List<MavenProject> projects, String moduleFrom, String moduleTo) {
80              for (MavenProject project : projects) {
81                  if (moduleFrom.equals(project.getArtifactId())) {
82                      Dependency dependency = new Dependency();
83                      dependency.setArtifactId(moduleTo);
84                      dependency.setGroupId(project.getGroupId());
85                      dependency.setVersion(project.getVersion());
86  
87                      project.getModel().addDependency(dependency);
88                  }
89              }
90          }
91      }
92  
93      public static class SelectCurrentProject extends AbstractMavenLifecycleParticipant {
94          @Override
95          public void afterProjectsRead(MavenSession session) {
96              assertEquals("dependency", session.getProjects().get(0).getArtifactId());
97              assertEquals("root", session.getTopLevelProject().getArtifactId());
98              assertSame(session.getTopLevelProject(), session.getCurrentProject());
99              session.setProjects(List.of(session.getCurrentProject()));
100         }
101     }
102 
103     @Override
104     protected String getProjectsDirectory() {
105         return "src/test/projects/lifecycle-listener";
106     }
107 
108     @Test
109     void testDependencyInjection() throws Exception {
110         PlexusContainer container = getContainer();
111 
112         ComponentDescriptor<? extends AbstractMavenLifecycleParticipant> cd =
113                 new ComponentDescriptor<>(InjectDependencyLifecycleListener.class, container.getContainerRealm());
114         cd.setRoleClass(AbstractMavenLifecycleParticipant.class);
115         container.addComponentDescriptor(cd);
116 
117         Maven maven = container.lookup(Maven.class);
118         File pom = getProject("lifecycle-listener-dependency-injection");
119         MavenExecutionRequest request = createMavenExecutionRequest(pom);
120         request.setGoals(Arrays.asList("validate"));
121         MavenExecutionResult result = maven.execute(request);
122 
123         assertFalse(result.hasExceptions(), result.getExceptions().toString());
124 
125         MavenProject project = result.getProject();
126 
127         assertEquals("bar", project.getProperties().getProperty("foo"));
128 
129         ArrayList<Artifact> artifacts = new ArrayList<>(project.getArtifacts());
130 
131         assertEquals(1, artifacts.size());
132         assertEquals(INJECTED_ARTIFACT_ID, artifacts.get(0).getArtifactId());
133     }
134 
135     @Test
136     void testReactorDependencyInjection() throws Exception {
137         List<String> reactorOrder =
138                 getReactorOrder("lifecycle-participant-reactor-dependency-injection", InjectReactorDependency.class);
139         assertEquals(Arrays.asList("parent", "module-b", "module-a"), reactorOrder);
140     }
141 
142     @Test
143     void testCurrentProjectSelection() throws Exception {
144         List<String> reactorOrder = getReactorOrder("current-project-selection", SelectCurrentProject.class);
145         assertEquals(List.of("root"), reactorOrder);
146     }
147 
148     private <T> List<String> getReactorOrder(String testProject, Class<T> participant) throws Exception {
149         PlexusContainer container = getContainer();
150 
151         ComponentDescriptor<T> cd = new ComponentDescriptor<>(participant, container.getContainerRealm());
152         cd.setRoleClass(AbstractMavenLifecycleParticipant.class);
153         container.addComponentDescriptor(cd);
154 
155         Maven maven = container.lookup(Maven.class);
156         File pom = getProject(testProject);
157         MavenExecutionRequest request = createMavenExecutionRequest(pom);
158         request.setGoals(Arrays.asList("validate"));
159         MavenExecutionResult result = maven.execute(request);
160 
161         assertFalse(result.hasExceptions(), result.getExceptions().toString());
162 
163         List<String> order = new ArrayList<>();
164         for (MavenProject project : result.getTopologicallySortedProjects()) {
165             order.add(project.getArtifactId());
166         }
167         return order;
168     }
169 }