1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Properties;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.InvalidRepositoryException;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
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.MavenExecutionRequest;
36 import org.apache.maven.execution.MavenSession;
37 import org.apache.maven.internal.impl.DefaultRepositoryFactory;
38 import org.apache.maven.internal.impl.DefaultSession;
39 import org.apache.maven.internal.impl.InternalSession;
40 import org.apache.maven.model.Build;
41 import org.apache.maven.model.Dependency;
42 import org.apache.maven.model.Exclusion;
43 import org.apache.maven.model.Model;
44 import org.apache.maven.model.Plugin;
45 import org.apache.maven.model.Repository;
46 import org.apache.maven.model.RepositoryPolicy;
47 import org.apache.maven.project.DefaultProjectBuildingRequest;
48 import org.apache.maven.project.MavenProject;
49 import org.apache.maven.project.ProjectBuildingRequest;
50 import org.apache.maven.repository.RepositorySystem;
51 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
52 import org.codehaus.plexus.PlexusContainer;
53 import org.codehaus.plexus.testing.PlexusTest;
54 import org.codehaus.plexus.util.FileUtils;
55 import org.eclipse.aether.DefaultRepositorySystemSession;
56 import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
57 import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
58 import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
59 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
60 import org.eclipse.aether.repository.LocalRepository;
61
62 import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
63 import static org.mockito.Mockito.mock;
64
65 @PlexusTest
66 @Deprecated
67 public abstract class AbstractCoreMavenComponentTestCase {
68
69 @Inject
70 protected PlexusContainer container;
71
72 @Inject
73 protected RepositorySystem repositorySystem;
74
75 @Inject
76 protected org.apache.maven.project.ProjectBuilder projectBuilder;
77
78 protected abstract String getProjectsDirectory();
79
80 protected PlexusContainer getContainer() {
81 return container;
82 }
83
84 protected File getProject(String name) throws Exception {
85 File source = new File(new File(getBasedir(), getProjectsDirectory()), name);
86 File target = new File(new File(getBasedir(), "target"), name);
87 FileUtils.copyDirectoryStructureIfModified(source, target);
88 return new File(target, "pom.xml");
89 }
90
91 protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception {
92 MavenExecutionRequest request = new DefaultMavenExecutionRequest()
93 .setPom(pom)
94 .setProjectPresent(true)
95 .setShowErrors(true)
96 .setPluginGroups(Arrays.asList("org.apache.maven.plugins"))
97 .setLocalRepository(getLocalRepository())
98 .setRemoteRepositories(getRemoteRepositories())
99 .setPluginArtifactRepositories(getPluginArtifactRepositories())
100 .setGoals(Arrays.asList("package"));
101
102 if (pom != null) {
103 request.setMultiModuleProjectDirectory(pom.getParentFile());
104 }
105
106 return request;
107 }
108
109
110
111
112 protected MavenSession createMavenSession(File pom) throws Exception {
113 return createMavenSession(pom, new Properties());
114 }
115
116 protected MavenSession createMavenSession(File pom, Properties executionProperties) throws Exception {
117 return createMavenSession(pom, executionProperties, false);
118 }
119
120 protected MavenSession createMavenSession(File pom, Properties executionProperties, boolean includeModules)
121 throws Exception {
122 MavenExecutionRequest request = createMavenExecutionRequest(pom);
123
124 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest()
125 .setLocalRepository(request.getLocalRepository())
126 .setRemoteRepositories(request.getRemoteRepositories())
127 .setPluginArtifactRepositories(request.getPluginArtifactRepositories())
128 .setSystemProperties(executionProperties)
129 .setUserProperties(new Properties());
130
131 initRepoSession(configuration);
132
133 MavenSession session = new MavenSession(
134 getContainer(), configuration.getRepositorySession(), request, new DefaultMavenExecutionResult());
135 DefaultSession iSession = new DefaultSession(
136 session,
137 mock(org.eclipse.aether.RepositorySystem.class),
138 null,
139 null,
140 new SimpleLookup(List.of(new DefaultRepositoryFactory(new DefaultRemoteRepositoryManager(
141 new DefaultUpdatePolicyAnalyzer(), new DefaultChecksumPolicyProvider())))),
142 null);
143 InternalSession.associate(session.getRepositorySession(), iSession);
144 session.setSession(iSession);
145
146 List<MavenProject> projects = new ArrayList<>();
147
148 if (pom != null) {
149 MavenProject project = projectBuilder.build(pom, configuration).getProject();
150
151 projects.add(project);
152 if (includeModules) {
153 for (String module : project.getModules()) {
154 File modulePom = new File(pom.getParentFile(), module);
155 if (modulePom.isDirectory()) {
156 modulePom = new File(modulePom, "pom.xml");
157 }
158 projects.add(projectBuilder.build(modulePom, configuration).getProject());
159 }
160 }
161 } else {
162 MavenProject project = createStubMavenProject();
163 project.setRemoteArtifactRepositories(request.getRemoteRepositories());
164 project.setPluginArtifactRepositories(request.getPluginArtifactRepositories());
165 projects.add(project);
166 }
167
168 session.setProjects(projects);
169 session.setAllProjects(session.getProjects());
170
171 return session;
172 }
173
174 protected void initRepoSession(ProjectBuildingRequest request) throws Exception {
175 File localRepoDir = new File(request.getLocalRepository().getBasedir());
176 LocalRepository localRepo = new LocalRepository(localRepoDir);
177 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
178 session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo));
179 request.setRepositorySession(session);
180 }
181
182 protected MavenProject createStubMavenProject() {
183 Model model = new Model();
184 model.setGroupId("org.apache.maven.test");
185 model.setArtifactId("maven-test");
186 model.setVersion("1.0");
187 return new MavenProject(model);
188 }
189
190 protected List<ArtifactRepository> getRemoteRepositories() throws InvalidRepositoryException {
191 File repoDir = new File(getBasedir(), "src/test/remote-repo").getAbsoluteFile();
192
193 RepositoryPolicy policy = new RepositoryPolicy();
194 policy.setEnabled(true);
195 policy.setChecksumPolicy("ignore");
196 policy.setUpdatePolicy("always");
197
198 Repository repository = new Repository();
199 repository.setId(MavenRepositorySystem.DEFAULT_REMOTE_REPO_ID);
200 repository.setUrl("file://" + repoDir.toURI().getPath());
201 repository.setReleases(policy);
202 repository.setSnapshots(policy);
203
204 return Arrays.asList(repositorySystem.buildArtifactRepository(repository));
205 }
206
207 protected List<ArtifactRepository> getPluginArtifactRepositories() throws InvalidRepositoryException {
208 return getRemoteRepositories();
209 }
210
211 protected ArtifactRepository getLocalRepository() throws InvalidRepositoryException {
212 File repoDir = new File(getBasedir(), "target/local-repo").getAbsoluteFile();
213
214 return repositorySystem.createLocalRepository(repoDir);
215 }
216
217 protected class ProjectBuilder {
218 private MavenProject project;
219
220 public ProjectBuilder(MavenProject project) {
221 this.project = project;
222 }
223
224 public ProjectBuilder(String groupId, String artifactId, String version) {
225 Model model = new Model();
226 model.setModelVersion("4.0.0");
227 model.setGroupId(groupId);
228 model.setArtifactId(artifactId);
229 model.setVersion(version);
230 model.setBuild(new Build());
231 project = new MavenProject(model);
232 }
233
234 public ProjectBuilder setGroupId(String groupId) {
235 project.setGroupId(groupId);
236 return this;
237 }
238
239 public ProjectBuilder setArtifactId(String artifactId) {
240 project.setArtifactId(artifactId);
241 return this;
242 }
243
244 public ProjectBuilder setVersion(String version) {
245 project.setVersion(version);
246 return this;
247 }
248
249
250
251 public ProjectBuilder addDependency(String groupId, String artifactId, String version, String scope) {
252 return addDependency(groupId, artifactId, version, scope, (Exclusion) null);
253 }
254
255 public ProjectBuilder addDependency(
256 String groupId, String artifactId, String version, String scope, Exclusion exclusion) {
257 return addDependency(groupId, artifactId, version, scope, null, exclusion);
258 }
259
260 public ProjectBuilder addDependency(
261 String groupId, String artifactId, String version, String scope, String systemPath) {
262 return addDependency(groupId, artifactId, version, scope, systemPath, null);
263 }
264
265 public ProjectBuilder addDependency(
266 String groupId,
267 String artifactId,
268 String version,
269 String scope,
270 String systemPath,
271 Exclusion exclusion) {
272 Dependency d = new Dependency();
273 d.setGroupId(groupId);
274 d.setArtifactId(artifactId);
275 d.setVersion(version);
276 d.setScope(scope);
277
278 if (systemPath != null && scope.equals(Artifact.SCOPE_SYSTEM)) {
279 d.setSystemPath(systemPath);
280 }
281
282 if (exclusion != null) {
283 d.addExclusion(exclusion);
284 }
285
286 project.getDependencies().add(d);
287
288 return this;
289 }
290
291
292
293 public ProjectBuilder addPlugin(Plugin plugin) {
294 project.getBuildPlugins().add(plugin);
295 return this;
296 }
297
298 public MavenProject get() {
299 return project;
300 }
301 }
302 }