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