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