001 package org.apache.maven;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.io.File;
023 import java.util.Arrays;
024 import java.util.List;
025 import java.util.Properties;
026
027 import org.apache.maven.artifact.Artifact;
028 import org.apache.maven.artifact.InvalidRepositoryException;
029 import org.apache.maven.artifact.repository.ArtifactRepository;
030 import org.apache.maven.execution.DefaultMavenExecutionRequest;
031 import org.apache.maven.execution.DefaultMavenExecutionResult;
032 import org.apache.maven.execution.MavenExecutionRequest;
033 import org.apache.maven.execution.MavenSession;
034 import org.apache.maven.model.Build;
035 import org.apache.maven.model.Dependency;
036 import org.apache.maven.model.Exclusion;
037 import org.apache.maven.model.Model;
038 import org.apache.maven.model.Plugin;
039 import org.apache.maven.model.Repository;
040 import org.apache.maven.model.RepositoryPolicy;
041 import org.apache.maven.project.DefaultProjectBuildingRequest;
042 import org.apache.maven.project.MavenProject;
043 import org.apache.maven.project.ProjectBuildingRequest;
044 import org.apache.maven.repository.RepositorySystem;
045 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
046 import org.codehaus.plexus.ContainerConfiguration;
047 import org.codehaus.plexus.PlexusTestCase;
048 import org.codehaus.plexus.component.annotations.Requirement;
049 import org.codehaus.plexus.util.FileUtils;
050 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
051
052 public abstract class AbstractCoreMavenComponentTestCase
053 extends PlexusTestCase
054 {
055 @Requirement
056 protected RepositorySystem repositorySystem;
057
058 @Requirement
059 protected org.apache.maven.project.ProjectBuilder projectBuilder;
060
061 protected void setUp()
062 throws Exception
063 {
064 repositorySystem = lookup( RepositorySystem.class );
065 projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class );
066 }
067
068 @Override
069 protected void tearDown()
070 throws Exception
071 {
072 repositorySystem = null;
073 projectBuilder = null;
074 super.tearDown();
075 }
076
077 abstract protected String getProjectsDirectory();
078
079 protected File getProject( String name )
080 throws Exception
081 {
082 File source = new File( new File( getBasedir(), getProjectsDirectory() ), name );
083 File target = new File( new File( getBasedir(), "target" ), name );
084 FileUtils.copyDirectoryStructureIfModified( source, target );
085 return new File( target, "pom.xml" );
086 }
087
088 /**
089 * We need to customize the standard Plexus container with the plugin discovery listener which
090 * is what looks for the META-INF/maven/plugin.xml resources that enter the system when a Maven
091 * plugin is loaded.
092 *
093 * We also need to customize the Plexus container with a standard plugin discovery listener
094 * which is the MavenPluginCollector. When a Maven plugin is discovered the MavenPluginCollector
095 * collects the plugin descriptors which are found.
096 */
097 protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
098 {
099 // containerConfiguration.addComponentDiscoverer( PluginManager.class );
100 // containerConfiguration.addComponentDiscoveryListener( PluginManager.class );
101 }
102
103 protected MavenExecutionRequest createMavenExecutionRequest( File pom )
104 throws Exception
105 {
106 MavenExecutionRequest request = new DefaultMavenExecutionRequest()
107 .setPom( pom )
108 .setProjectPresent( true )
109 .setShowErrors( true )
110 .setPluginGroups( Arrays.asList( new String[] { "org.apache.maven.plugins" } ) )
111 .setLocalRepository( getLocalRepository() )
112 .setRemoteRepositories( getRemoteRepositories() )
113 .setPluginArtifactRepositories( getPluginArtifactRepositories() )
114 .setGoals( Arrays.asList( new String[] { "package" } ) );
115
116 return request;
117 }
118
119 // layer the creation of a project builder configuration with a request, but this will need to be
120 // a Maven subclass because we don't want to couple maven to the project builder which we need to
121 // separate.
122 protected MavenSession createMavenSession( File pom )
123 throws Exception
124 {
125 return createMavenSession( pom, new Properties() );
126 }
127
128 protected MavenSession createMavenSession( File pom, Properties executionProperties )
129 throws Exception
130 {
131 MavenExecutionRequest request = createMavenExecutionRequest( pom );
132
133 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest()
134 .setLocalRepository( request.getLocalRepository() )
135 .setRemoteRepositories( request.getRemoteRepositories() )
136 .setPluginArtifactRepositories( request.getPluginArtifactRepositories() )
137 .setSystemProperties( executionProperties );
138
139 MavenProject project = null;
140
141 if ( pom != null )
142 {
143 project = projectBuilder.build( pom, configuration ).getProject();
144 }
145 else
146 {
147 project = createStubMavenProject();
148 project.setRemoteArtifactRepositories( request.getRemoteRepositories() );
149 project.setPluginArtifactRepositories( request.getPluginArtifactRepositories() );
150 }
151
152 initRepoSession( configuration );
153
154 MavenSession session =
155 new MavenSession( getContainer(), configuration.getRepositorySession(), request,
156 new DefaultMavenExecutionResult() );
157 session.setProjects( Arrays.asList( project ) );
158
159 return session;
160 }
161
162 protected void initRepoSession( ProjectBuildingRequest request )
163 {
164 File localRepo = new File( request.getLocalRepository().getBasedir() );
165 MavenRepositorySystemSession session = new MavenRepositorySystemSession();
166 session.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
167 request.setRepositorySession( session );
168 }
169
170 protected MavenProject createStubMavenProject()
171 {
172 Model model = new Model();
173 model.setGroupId( "org.apache.maven.test" );
174 model.setArtifactId( "maven-test" );
175 model.setVersion( "1.0" );
176 return new MavenProject( model );
177 }
178
179 protected List<ArtifactRepository> getRemoteRepositories()
180 throws InvalidRepositoryException
181 {
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.buildArtifactRepository( repository ) );
196 }
197
198 protected List<ArtifactRepository> getPluginArtifactRepositories()
199 throws InvalidRepositoryException
200 {
201 return getRemoteRepositories();
202 }
203
204 protected ArtifactRepository getLocalRepository()
205 throws InvalidRepositoryException
206 {
207 File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile();
208
209 return repositorySystem.createLocalRepository( repoDir );
210 }
211
212 protected class ProjectBuilder
213 {
214 private MavenProject project;
215
216 public ProjectBuilder( MavenProject project )
217 {
218 this.project = project;
219 }
220
221 public ProjectBuilder( String groupId, String artifactId, String version )
222 {
223 Model model = new Model();
224 model.setModelVersion( "4.0.0" );
225 model.setGroupId( groupId );
226 model.setArtifactId( artifactId );
227 model.setVersion( version );
228 model.setBuild( new Build() );
229 project = new MavenProject( model );
230 }
231
232 public ProjectBuilder setGroupId( String groupId )
233 {
234 project.setGroupId( groupId );
235 return this;
236 }
237
238 public ProjectBuilder setArtifactId( String artifactId )
239 {
240 project.setArtifactId( artifactId );
241 return this;
242 }
243
244 public ProjectBuilder setVersion( String version )
245 {
246 project.setVersion( version );
247 return this;
248 }
249
250 // Dependencies
251 //
252 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope )
253 {
254 return addDependency( groupId, artifactId, version, scope, (Exclusion)null );
255 }
256
257 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
258 {
259 return addDependency( groupId, artifactId, version, scope, null, exclusion );
260 }
261
262 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
263 {
264 return addDependency( groupId, artifactId, version, scope, systemPath, null );
265 }
266
267 public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
268 {
269 Dependency d = new Dependency();
270 d.setGroupId( groupId );
271 d.setArtifactId( artifactId );
272 d.setVersion( version );
273 d.setScope( scope );
274
275 if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) )
276 {
277 d.setSystemPath( systemPath );
278 }
279
280 if ( exclusion != null )
281 {
282 d.addExclusion( exclusion );
283 }
284
285 project.getDependencies().add( d );
286
287 return this;
288 }
289
290 // Plugins
291 //
292 public ProjectBuilder addPlugin( Plugin plugin )
293 {
294 project.getBuildPlugins().add( plugin );
295 return this;
296 }
297
298 public MavenProject get()
299 {
300 return project;
301 }
302 }
303
304 protected class PluginBuilder
305 {
306 private Plugin plugin;
307
308 public PluginBuilder( String groupId, String artifactId, String version )
309 {
310 plugin = new Plugin();
311 plugin.setGroupId( groupId );
312 plugin.setArtifactId( artifactId );
313 plugin.setVersion( version );
314 }
315
316 // Dependencies
317 //
318 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
319 {
320 return addDependency( groupId, artifactId, version, scope, exclusion );
321 }
322
323 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
324 {
325 return addDependency( groupId, artifactId, version, scope, systemPath, null );
326 }
327
328 public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
329 {
330 Dependency d = new Dependency();
331 d.setGroupId( groupId );
332 d.setArtifactId( artifactId );
333 d.setVersion( version );
334 d.setScope( scope );
335
336 if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) )
337 {
338 d.setSystemPath( systemPath );
339 }
340
341 if ( exclusion != null )
342 {
343 d.addExclusion( exclusion );
344 }
345
346 plugin.getDependencies().add( d );
347
348 return this;
349 }
350
351 public Plugin get()
352 {
353 return plugin;
354 }
355 }
356 }