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