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