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