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.execution.DefaultMavenExecutionRequest;
31  import org.apache.maven.execution.DefaultMavenExecutionResult;
32  import org.apache.maven.execution.MavenExecutionRequest;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.model.Build;
35  import org.apache.maven.model.Dependency;
36  import org.apache.maven.model.Exclusion;
37  import org.apache.maven.model.Model;
38  import org.apache.maven.model.Plugin;
39  import org.apache.maven.model.Repository;
40  import org.apache.maven.model.RepositoryPolicy;
41  import org.apache.maven.project.DefaultProjectBuildingRequest;
42  import org.apache.maven.project.MavenProject;
43  import org.apache.maven.project.ProjectBuildingRequest;
44  import org.apache.maven.repository.RepositorySystem;
45  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
46  import org.codehaus.plexus.ContainerConfiguration;
47  import org.codehaus.plexus.PlexusConstants;
48  import org.codehaus.plexus.PlexusTestCase;
49  import org.codehaus.plexus.component.annotations.Requirement;
50  import org.codehaus.plexus.util.FileUtils;
51  import org.eclipse.aether.DefaultRepositorySystemSession;
52  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
53  import org.eclipse.aether.repository.LocalRepository;
54  
55  public abstract class AbstractCoreMavenComponentTestCase extends PlexusTestCase {
56      @Requirement
57      protected RepositorySystem repositorySystem;
58  
59      @Requirement
60      protected org.apache.maven.project.ProjectBuilder projectBuilder;
61  
62      protected void setUp() throws Exception {
63          repositorySystem = lookup(RepositorySystem.class);
64          projectBuilder = lookup(org.apache.maven.project.ProjectBuilder.class);
65      }
66  
67      @Override
68      protected void tearDown() throws Exception {
69          repositorySystem = null;
70          projectBuilder = null;
71          super.tearDown();
72      }
73  
74      protected abstract String getProjectsDirectory();
75  
76      protected File getProject(String name) throws Exception {
77          File source = new File(new File(getBasedir(), getProjectsDirectory()), name);
78          File target = new File(new File(getBasedir(), "target"), name);
79          FileUtils.copyDirectoryStructureIfModified(source, target);
80          return new File(target, "pom.xml");
81      }
82  
83      /**
84       * We need to customize the standard Plexus container with the plugin discovery listener which
85       * is what looks for the META-INF/maven/plugin.xml resources that enter the system when a Maven
86       * plugin is loaded.
87       *
88       * We also need to customize the Plexus container with a standard plugin discovery listener
89       * which is the MavenPluginCollector. When a Maven plugin is discovered the MavenPluginCollector
90       * collects the plugin descriptors which are found.
91       */
92      protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
93          containerConfiguration.setAutoWiring(true).setClassPathScanning(PlexusConstants.SCANNING_INDEX);
94      }
95  
96      protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception {
97          MavenExecutionRequest request = new DefaultMavenExecutionRequest()
98                  .setPom(pom)
99                  .setProjectPresent(true)
100                 .setShowErrors(true)
101                 .setPluginGroups(Arrays.asList("org.apache.maven.plugins"))
102                 .setLocalRepository(getLocalRepository())
103                 .setRemoteRepositories(getRemoteRepositories())
104                 .setPluginArtifactRepositories(getPluginArtifactRepositories())
105                 .setGoals(Arrays.asList("package"));
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(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         MavenSession session = new MavenSession(
157                 getContainer(), configuration.getRepositorySession(), request, new DefaultMavenExecutionResult());
158         session.setProjects(projects);
159         session.setAllProjects(session.getProjects());
160 
161         return session;
162     }
163 
164     protected void initRepoSession(ProjectBuildingRequest request) throws Exception {
165         File localRepoDir = new File(request.getLocalRepository().getBasedir());
166         LocalRepository localRepo = new LocalRepository(localRepoDir);
167         DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
168         session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo));
169         request.setRepositorySession(session);
170     }
171 
172     protected MavenProject createStubMavenProject() {
173         Model model = new Model();
174         model.setGroupId("org.apache.maven.test");
175         model.setArtifactId("maven-test");
176         model.setVersion("1.0");
177         return new MavenProject(model);
178     }
179 
180     protected List<ArtifactRepository> getRemoteRepositories() throws InvalidRepositoryException {
181         File repoDir = new File(getBasedir(), "src/test/remote-repo").getAbsoluteFile();
182 
183         RepositoryPolicy policy = new RepositoryPolicy();
184         policy.setEnabled(true);
185         policy.setChecksumPolicy("ignore");
186         policy.setUpdatePolicy("always");
187 
188         Repository repository = new Repository();
189         repository.setId(RepositorySystem.DEFAULT_REMOTE_REPO_ID);
190         repository.setUrl("file://" + repoDir.toURI().getPath());
191         repository.setReleases(policy);
192         repository.setSnapshots(policy);
193 
194         return Arrays.asList(repositorySystem.buildArtifactRepository(repository));
195     }
196 
197     protected List<ArtifactRepository> getPluginArtifactRepositories() throws InvalidRepositoryException {
198         return getRemoteRepositories();
199     }
200 
201     protected ArtifactRepository getLocalRepository() throws InvalidRepositoryException {
202         File repoDir = new File(getBasedir(), "target/local-repo").getAbsoluteFile();
203 
204         return repositorySystem.createLocalRepository(repoDir);
205     }
206 
207     protected class ProjectBuilder {
208         private MavenProject project;
209 
210         public ProjectBuilder(MavenProject project) {
211             this.project = project;
212         }
213 
214         public ProjectBuilder(String groupId, String artifactId, String version) {
215             Model model = new Model();
216             model.setModelVersion("4.0.0");
217             model.setGroupId(groupId);
218             model.setArtifactId(artifactId);
219             model.setVersion(version);
220             model.setBuild(new Build());
221             project = new MavenProject(model);
222         }
223 
224         public ProjectBuilder setGroupId(String groupId) {
225             project.setGroupId(groupId);
226             return this;
227         }
228 
229         public ProjectBuilder setArtifactId(String artifactId) {
230             project.setArtifactId(artifactId);
231             return this;
232         }
233 
234         public ProjectBuilder setVersion(String version) {
235             project.setVersion(version);
236             return this;
237         }
238 
239         // Dependencies
240         //
241         public ProjectBuilder addDependency(String groupId, String artifactId, String version, String scope) {
242             return addDependency(groupId, artifactId, version, scope, (Exclusion) null);
243         }
244 
245         public ProjectBuilder addDependency(
246                 String groupId, String artifactId, String version, String scope, Exclusion exclusion) {
247             return addDependency(groupId, artifactId, version, scope, null, exclusion);
248         }
249 
250         public ProjectBuilder addDependency(
251                 String groupId, String artifactId, String version, String scope, String systemPath) {
252             return addDependency(groupId, artifactId, version, scope, systemPath, null);
253         }
254 
255         public ProjectBuilder addDependency(
256                 String groupId,
257                 String artifactId,
258                 String version,
259                 String scope,
260                 String systemPath,
261                 Exclusion exclusion) {
262             Dependency d = new Dependency();
263             d.setGroupId(groupId);
264             d.setArtifactId(artifactId);
265             d.setVersion(version);
266             d.setScope(scope);
267 
268             if (systemPath != null && scope.equals(Artifact.SCOPE_SYSTEM)) {
269                 d.setSystemPath(systemPath);
270             }
271 
272             if (exclusion != null) {
273                 d.addExclusion(exclusion);
274             }
275 
276             project.getDependencies().add(d);
277 
278             return this;
279         }
280 
281         // Plugins
282         //
283         public ProjectBuilder addPlugin(Plugin plugin) {
284             project.getBuildPlugins().add(plugin);
285             return this;
286         }
287 
288         public MavenProject get() {
289             return project;
290         }
291     }
292 }