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