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.lifecycle.internal;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashSet;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import org.apache.maven.AbstractCoreMavenComponentTestCase;
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.project.MavenProject;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  import static org.junit.jupiter.api.Assertions.assertNull;
38  
39  class LifecycleDependencyResolverTest extends AbstractCoreMavenComponentTestCase {
40      @Inject
41      private LifecycleDependencyResolver resolver;
42  
43      @Override
44      protected String getProjectsDirectory() {
45          return null;
46      }
47  
48      @Test
49      void testCachedReactorProjectDependencies() throws Exception {
50          MavenSession session = createMavenSession(
51                  new File("src/test/projects/lifecycle-dependency-resolver/pom.xml"), new Properties(), true);
52          Collection<String> scopesToCollect = null;
53          Collection<String> scopesToResolve = Collections.singletonList("compile");
54          boolean aggregating = false;
55  
56          Set<Artifact> reactorArtifacts = new HashSet<>(3);
57          for (MavenProject reactorProject : session.getProjects()) {
58              reactorProject.setArtifactFilter(artifact -> true);
59              resolver.resolveProjectDependencies(
60                      reactorProject, scopesToCollect, scopesToResolve, session, aggregating, reactorArtifacts);
61              reactorArtifacts.add(reactorProject.getArtifact());
62          }
63  
64          MavenProject lib = session.getProjects().get(1);
65          MavenProject war = session.getProjects().get(2);
66  
67          assertNull(war.getArtifactMap()
68                  .get("org.apache.maven.its.mng6300:mng6300-lib")
69                  .getFile());
70  
71          lib.getArtifact().setFile(new File("lib.jar"));
72  
73          resolver.resolveProjectDependencies(
74                  war, scopesToCollect, scopesToResolve, session, aggregating, reactorArtifacts);
75  
76          assertEquals(
77                  new File("lib.jar"),
78                  war.getArtifactMap()
79                          .get("org.apache.maven.its.mng6300:mng6300-lib")
80                          .getFile());
81      }
82  }