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