View Javadoc

1   package org.apache.maven;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
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.PlexusTestCase;
48  import org.codehaus.plexus.component.annotations.Requirement;
49  import org.codehaus.plexus.util.FileUtils;
50  import org.eclipse.aether.DefaultRepositorySystemSession;
51  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
52  import org.eclipse.aether.repository.LocalRepository;
53  
54  public abstract class AbstractCoreMavenComponentTestCase
55      extends PlexusTestCase
56  {
57      @Requirement
58      protected RepositorySystem repositorySystem;
59  
60      @Requirement
61      protected org.apache.maven.project.ProjectBuilder projectBuilder;
62  
63      protected void setUp()
64          throws Exception
65      {
66          repositorySystem = lookup( RepositorySystem.class );
67          projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class );
68      }
69  
70      @Override
71      protected void tearDown()
72          throws Exception
73      {
74          repositorySystem = null;
75          projectBuilder = null;
76          super.tearDown();
77      }
78  
79      abstract protected String getProjectsDirectory();
80  
81      protected File getProject( String name )
82          throws Exception
83      {
84          File source = new File( new File( getBasedir(), getProjectsDirectory() ), name );
85          File target = new File( new File( getBasedir(), "target" ), name );
86          FileUtils.copyDirectoryStructureIfModified( source, target );
87          return new File( target, "pom.xml" );
88      }
89  
90      /**
91       * We need to customize the standard Plexus container with the plugin discovery listener which
92       * is what looks for the META-INF/maven/plugin.xml resources that enter the system when a Maven
93       * plugin is loaded.
94       * 
95       * We also need to customize the Plexus container with a standard plugin discovery listener
96       * which is the MavenPluginCollector. When a Maven plugin is discovered the MavenPluginCollector
97       * collects the plugin descriptors which are found.
98       */
99      protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
100     {
101         containerConfiguration.setAutoWiring( true );
102     }
103 
104     protected MavenExecutionRequest createMavenExecutionRequest( File pom )
105         throws Exception
106     {        
107         MavenExecutionRequest request = new DefaultMavenExecutionRequest()
108             .setPom( pom )
109             .setProjectPresent( true )
110             .setShowErrors( true )
111             .setPluginGroups( Arrays.asList( "org.apache.maven.plugins" ) )
112             .setLocalRepository( getLocalRepository() )
113             .setRemoteRepositories( getRemoteRepositories() )
114             .setPluginArtifactRepositories( getPluginArtifactRepositories() )
115             .setGoals( Arrays.asList( "package" ) );
116 
117         return request;
118     }
119 
120     // layer the creation of a project builder configuration with a request, but this will need to be
121     // a Maven subclass because we don't want to couple maven to the project builder which we need to
122     // separate.
123     protected MavenSession createMavenSession( File pom )
124         throws Exception        
125     {
126         return createMavenSession( pom, new Properties() );
127     }
128     
129     protected MavenSession createMavenSession( File pom, Properties executionProperties )
130         throws Exception
131     {
132         MavenExecutionRequest request = createMavenExecutionRequest( pom );
133         
134         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest()
135             .setLocalRepository( request.getLocalRepository() )
136             .setRemoteRepositories( request.getRemoteRepositories() )
137             .setPluginArtifactRepositories( request.getPluginArtifactRepositories() )
138             .setSystemProperties( executionProperties );
139 
140         MavenProject project = null;
141 
142         if ( pom != null )
143         {
144             project = projectBuilder.build( pom, configuration ).getProject();
145         }
146         else
147         {
148             project = createStubMavenProject();
149             project.setRemoteArtifactRepositories( request.getRemoteRepositories() );
150             project.setPluginArtifactRepositories( request.getPluginArtifactRepositories() );
151         }
152 
153         initRepoSession( configuration );
154 
155         MavenSession session =
156             new MavenSession( getContainer(), configuration.getRepositorySession(), request,
157                               new DefaultMavenExecutionResult() );
158         session.setProjects( Arrays.asList( project ) );
159 
160         return session;
161     }
162 
163     protected void initRepoSession( ProjectBuildingRequest request )
164         throws Exception
165     {
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     {
175         Model model = new Model();
176         model.setGroupId( "org.apache.maven.test" );
177         model.setArtifactId( "maven-test" );
178         model.setVersion( "1.0" );
179         return new MavenProject( model );
180     }
181     
182     protected List<ArtifactRepository> getRemoteRepositories()
183         throws InvalidRepositoryException
184     {
185         File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile();
186 
187         RepositoryPolicy policy = new RepositoryPolicy();
188         policy.setEnabled( true );
189         policy.setChecksumPolicy( "ignore" );
190         policy.setUpdatePolicy( "always" );
191 
192         Repository repository = new Repository();
193         repository.setId( RepositorySystem.DEFAULT_REMOTE_REPO_ID );
194         repository.setUrl( "file://" + repoDir.toURI().getPath() );
195         repository.setReleases( policy );
196         repository.setSnapshots( policy );
197 
198         return Arrays.asList( repositorySystem.buildArtifactRepository( repository ) );
199     }
200 
201     protected List<ArtifactRepository> getPluginArtifactRepositories()
202         throws InvalidRepositoryException
203     {
204         return getRemoteRepositories();
205     }
206 
207     protected ArtifactRepository getLocalRepository()
208         throws InvalidRepositoryException
209     {
210         File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile();
211 
212         return repositorySystem.createLocalRepository( repoDir );
213     }
214     
215     protected class ProjectBuilder
216     {
217         private MavenProject project;
218 
219         public ProjectBuilder( MavenProject project )
220         {
221             this.project = project;
222         }
223 
224         public ProjectBuilder( String groupId, String artifactId, String version )
225         {
226             Model model = new Model();
227             model.setModelVersion( "4.0.0" );
228             model.setGroupId( groupId );
229             model.setArtifactId( artifactId );
230             model.setVersion( version );  
231             model.setBuild(  new Build() );
232             project = new MavenProject( model );            
233         }
234         
235         public ProjectBuilder setGroupId( String groupId )
236         {
237             project.setGroupId( groupId );
238             return this;
239         }
240 
241         public ProjectBuilder setArtifactId( String artifactId )
242         {
243             project.setArtifactId( artifactId );
244             return this;
245         }
246         
247         public ProjectBuilder setVersion( String version )
248         {
249             project.setVersion( version );
250             return this;
251         }
252         
253         // Dependencies
254         //
255         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope )
256         {
257             return addDependency( groupId, artifactId, version, scope, (Exclusion)null );
258         }
259         
260         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
261         {
262             return addDependency( groupId, artifactId, version, scope, null, exclusion );            
263         }
264 
265         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
266         {
267             return addDependency( groupId, artifactId, version, scope, systemPath, null );         
268         }
269         
270         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
271         {
272             Dependency d = new Dependency();
273             d.setGroupId( groupId );
274             d.setArtifactId( artifactId );
275             d.setVersion( version );
276             d.setScope( scope );
277             
278             if ( systemPath != null && scope.equals(  Artifact.SCOPE_SYSTEM ) )
279             {
280                 d.setSystemPath( systemPath );
281             }
282             
283             if ( exclusion != null )
284             {
285                 d.addExclusion( exclusion );
286             }
287             
288             project.getDependencies().add( d );
289             
290             return this;
291         }
292         
293         // Plugins
294         //
295         public ProjectBuilder addPlugin( Plugin plugin )
296         {
297             project.getBuildPlugins().add( plugin );            
298             return this;
299         }
300         
301         public MavenProject get()
302         {
303             return project;
304         }        
305     }    
306     
307     protected class PluginBuilder
308     {
309         private Plugin plugin;
310         
311         public PluginBuilder( String groupId, String artifactId, String version )
312         {
313             plugin = new Plugin();
314             plugin.setGroupId( groupId );
315             plugin.setArtifactId( artifactId );
316             plugin.setVersion( version );                         
317         }
318                 
319         // Dependencies
320         //
321         public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
322         {
323             return addDependency( groupId, artifactId, version, scope, exclusion );            
324         }
325 
326         public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
327         {
328             return addDependency( groupId, artifactId, version, scope, systemPath, null );         
329         }
330         
331         public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
332         {
333             Dependency d = new Dependency();
334             d.setGroupId( groupId );
335             d.setArtifactId( artifactId );
336             d.setVersion( version );
337             d.setScope( scope );
338             
339             if ( systemPath != null && scope.equals(  Artifact.SCOPE_SYSTEM ) )
340             {
341                 d.setSystemPath( systemPath );
342             }
343             
344             if ( exclusion != null )
345             {
346                 d.addExclusion( exclusion );
347             }
348             
349             plugin.getDependencies().add( d );
350             
351             return this;
352         }
353                 
354         public Plugin get()
355         {
356             return plugin;
357         }        
358     }        
359 }