1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
83 projectBuilder = getContainer().lookup(ProjectBuilder.class);
84 }
85 }
86
87 protected ProjectBuilder getProjectBuilder() {
88 return projectBuilder;
89 }
90
91
92
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
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 }