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.MavenRepositorySystemSession;
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.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
51  
52  public abstract class AbstractCoreMavenComponentTestCase
53      extends PlexusTestCase
54  {
55      @Requirement
56      protected RepositorySystem repositorySystem;
57  
58      @Requirement
59      protected org.apache.maven.project.ProjectBuilder projectBuilder;
60  
61      protected void setUp()
62          throws Exception
63      {
64          repositorySystem = lookup( RepositorySystem.class );
65          projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class );
66      }
67  
68      @Override
69      protected void tearDown()
70          throws Exception
71      {
72          repositorySystem = null;
73          projectBuilder = null;
74          super.tearDown();
75      }
76  
77      abstract protected String getProjectsDirectory();
78  
79      protected File getProject( String name )
80          throws Exception
81      {
82          File source = new File( new File( getBasedir(), getProjectsDirectory() ), name );
83          File target = new File( new File( getBasedir(), "target" ), name );
84          FileUtils.copyDirectoryStructureIfModified( source, target );
85          return new File( target, "pom.xml" );
86      }
87  
88      /**
89       * We need to customize the standard Plexus container with the plugin discovery listener which
90       * is what looks for the META-INF/maven/plugin.xml resources that enter the system when a Maven
91       * plugin is loaded.
92       * 
93       * We also need to customize the Plexus container with a standard plugin discovery listener
94       * which is the MavenPluginCollector. When a Maven plugin is discovered the MavenPluginCollector
95       * collects the plugin descriptors which are found.
96       */
97      protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
98      {
99  //        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 }