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.project;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.FileNotFoundException;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  import java.util.Arrays;
28  
29  import org.apache.maven.api.Session;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
32  import org.apache.maven.bridge.MavenRepositorySystem;
33  import org.apache.maven.execution.DefaultMavenExecutionRequest;
34  import org.apache.maven.execution.DefaultMavenExecutionResult;
35  import org.apache.maven.execution.MavenSession;
36  import org.apache.maven.impl.InternalSession;
37  import org.apache.maven.impl.resolver.scopes.Maven4ScopeManagerConfiguration;
38  import org.apache.maven.internal.impl.DefaultLookup;
39  import org.apache.maven.internal.impl.DefaultSession;
40  import org.apache.maven.internal.impl.InternalMavenSession;
41  import org.apache.maven.model.building.ModelBuildingException;
42  import org.apache.maven.model.building.ModelProblem;
43  import org.apache.maven.repository.RepositorySystem;
44  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
45  import org.apache.maven.session.scope.internal.SessionScope;
46  import org.codehaus.plexus.PlexusContainer;
47  import org.codehaus.plexus.testing.PlexusTest;
48  import org.eclipse.aether.DefaultRepositorySystemSession;
49  import org.eclipse.aether.internal.impl.scope.ScopeManagerImpl;
50  import org.junit.jupiter.api.BeforeEach;
51  
52  import static org.junit.jupiter.api.Assertions.fail;
53  
54  /**
55   */
56  @PlexusTest
57  @Deprecated
58  public abstract class AbstractMavenProjectTestCase {
59      protected ProjectBuilder projectBuilder;
60  
61      @Inject
62      protected RepositorySystem repositorySystem;
63  
64      @Inject
65      protected org.eclipse.aether.RepositorySystem resolverRepositorySystem;
66  
67      @Inject
68      protected MavenRepositorySystem mavenRepositorySystem;
69  
70      @Inject
71      protected PlexusContainer container;
72  
73      public PlexusContainer getContainer() {
74          return container;
75      }
76  
77      @BeforeEach
78      public void setUp() throws Exception {
79          if (getContainer().hasComponent(ProjectBuilder.class, "test")) {
80              projectBuilder = getContainer().lookup(ProjectBuilder.class, "test");
81          } else {
82              // default over to the main project builder...
83              projectBuilder = getContainer().lookup(ProjectBuilder.class);
84          }
85      }
86  
87      protected ProjectBuilder getProjectBuilder() {
88          return projectBuilder;
89      }
90  
91      // ----------------------------------------------------------------------
92      // Local repository
93      // ----------------------------------------------------------------------
94  
95      protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
96          File markerFile = getFileForClasspathResource("local-repo/marker.txt");
97  
98          return markerFile.getAbsoluteFile().getParentFile();
99      }
100 
101     protected static File getFileForClasspathResource(String resource)
102             throws FileNotFoundException, URISyntaxException {
103         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
104 
105         URL resourceUrl = cloader.getResource(resource);
106 
107         if (resourceUrl == null) {
108             throw new FileNotFoundException("Unable to find: " + resource);
109         }
110 
111         return new File(resourceUrl.toURI());
112     }
113 
114     protected ArtifactRepository getLocalRepository() throws Exception {
115         ArtifactRepositoryLayout repoLayout = getContainer().lookup(ArtifactRepositoryLayout.class);
116 
117         ArtifactRepository r = repositorySystem.createArtifactRepository(
118                 "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null);
119 
120         return r;
121     }
122 
123     // ----------------------------------------------------------------------
124     // Project building
125     // ----------------------------------------------------------------------
126 
127     protected MavenProject getProjectWithDependencies(File pom) throws Exception {
128         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
129         configuration.setLocalRepository(getLocalRepository());
130         configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
131         configuration.setProcessPlugins(true);
132         configuration.setResolveDependencies(true);
133         initRepoSession(configuration);
134 
135         try {
136             return projectBuilder.build(pom, configuration).getProject();
137         } catch (Exception e) {
138             Throwable cause = e.getCause();
139             if (cause instanceof ModelBuildingException modelBuildingException) {
140                 StringBuilder message = new StringBuilder("In: " + pom + "\n\n");
141                 for (ModelProblem problem : modelBuildingException.getProblems()) {
142                     message.append(problem).append("\n");
143                 }
144                 System.out.println(message);
145                 fail(message.toString());
146             }
147 
148             throw e;
149         }
150     }
151 
152     protected MavenProject getProject(File pom) throws Exception {
153         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
154         configuration.setLocalRepository(getLocalRepository());
155         initRepoSession(configuration);
156 
157         return projectBuilder.build(pom, configuration).getProject();
158     }
159 
160     protected void initRepoSession(ProjectBuildingRequest request) throws Exception {
161         File localRepo = new File(request.getLocalRepository().getBasedir());
162         DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
163         session.setScopeManager(new ScopeManagerImpl(Maven4ScopeManagerConfiguration.INSTANCE));
164         session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
165         request.setRepositorySession(session);
166 
167         DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
168         MavenSession msession =
169                 new MavenSession(getContainer(), session, mavenExecutionRequest, new DefaultMavenExecutionResult());
170         DefaultSession iSession = new DefaultSession(
171                 msession, resolverRepositorySystem, null, mavenRepositorySystem, new DefaultLookup(container), null);
172         InternalSession.associate(session, iSession);
173 
174         SessionScope sessionScope = container.lookup(SessionScope.class);
175         sessionScope.enter();
176         sessionScope.seed(MavenSession.class, msession);
177         sessionScope.seed(InternalMavenSession.class, iSession);
178         sessionScope.seed(Session.class, iSession);
179     }
180 }