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 
148         List<MavenProject> projects = new ArrayList<>();
149 
150         if ( pom != null )
151         {
152             MavenProject project = projectBuilder.build( pom, configuration ).getProject();
153             
154             projects.add( project );
155             if ( includeModules )
156             {
157                 for( String module : project.getModules() )
158                 {
159                     File modulePom = new File( pom.getParentFile(), module );
160                     if( modulePom.isDirectory() )
161                     {
162                         modulePom = new File( modulePom, "pom.xml" );
163                     }
164                     projects.add( projectBuilder.build( modulePom, configuration ).getProject() );
165                 }
166             }
167         }
168         else
169         {
170             MavenProject project = createStubMavenProject();
171             project.setRemoteArtifactRepositories( request.getRemoteRepositories() );
172             project.setPluginArtifactRepositories( request.getPluginArtifactRepositories() );
173             projects.add( project );
174         }
175 
176         initRepoSession( configuration );
177 
178         MavenSession session =
179             new MavenSession( getContainer(), configuration.getRepositorySession(), request,
180                               new DefaultMavenExecutionResult() );
181         session.setProjects( projects );
182         session.setAllProjects( session.getProjects() );
183 
184         return session;
185     }
186 
187     protected void initRepoSession( ProjectBuildingRequest request )
188         throws Exception
189     {
190         File localRepoDir = new File( request.getLocalRepository().getBasedir() );
191         LocalRepository localRepo = new LocalRepository( localRepoDir );
192         DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
193         session.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( session, localRepo ) );
194         request.setRepositorySession( session );
195     }
196 
197     protected MavenProject createStubMavenProject()
198     {
199         Model model = new Model();
200         model.setGroupId( "org.apache.maven.test" );
201         model.setArtifactId( "maven-test" );
202         model.setVersion( "1.0" );
203         return new MavenProject( model );
204     }
205 
206     protected List<ArtifactRepository> getRemoteRepositories()
207         throws InvalidRepositoryException
208     {
209         File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile();
210 
211         RepositoryPolicy policy = new RepositoryPolicy();
212         policy.setEnabled( true );
213         policy.setChecksumPolicy( "ignore" );
214         policy.setUpdatePolicy( "always" );
215 
216         Repository repository = new Repository();
217         repository.setId( RepositorySystem.DEFAULT_REMOTE_REPO_ID );
218         repository.setUrl( "file://" + repoDir.toURI().getPath() );
219         repository.setReleases( policy );
220         repository.setSnapshots( policy );
221 
222         return Arrays.asList( repositorySystem.buildArtifactRepository( repository ) );
223     }
224 
225     protected List<ArtifactRepository> getPluginArtifactRepositories()
226         throws InvalidRepositoryException
227     {
228         return getRemoteRepositories();
229     }
230 
231     protected ArtifactRepository getLocalRepository()
232         throws InvalidRepositoryException
233     {
234         File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile();
235 
236         return repositorySystem.createLocalRepository( repoDir );
237     }
238 
239     protected class ProjectBuilder
240     {
241         private MavenProject project;
242 
243         public ProjectBuilder( MavenProject project )
244         {
245             this.project = project;
246         }
247 
248         public ProjectBuilder( String groupId, String artifactId, String version )
249         {
250             Model model = new Model();
251             model.setModelVersion( "4.0.0" );
252             model.setGroupId( groupId );
253             model.setArtifactId( artifactId );
254             model.setVersion( version );
255             model.setBuild(  new Build() );
256             project = new MavenProject( model );
257         }
258 
259         public ProjectBuilder setGroupId( String groupId )
260         {
261             project.setGroupId( groupId );
262             return this;
263         }
264 
265         public ProjectBuilder setArtifactId( String artifactId )
266         {
267             project.setArtifactId( artifactId );
268             return this;
269         }
270 
271         public ProjectBuilder setVersion( String version )
272         {
273             project.setVersion( version );
274             return this;
275         }
276 
277         // Dependencies
278         //
279         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope )
280         {
281             return addDependency( groupId, artifactId, version, scope, (Exclusion)null );
282         }
283 
284         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion )
285         {
286             return addDependency( groupId, artifactId, version, scope, null, exclusion );
287         }
288 
289         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath )
290         {
291             return addDependency( groupId, artifactId, version, scope, systemPath, null );
292         }
293 
294         public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion )
295         {
296             Dependency d = new Dependency();
297             d.setGroupId( groupId );
298             d.setArtifactId( artifactId );
299             d.setVersion( version );
300             d.setScope( scope );
301 
302             if ( systemPath != null && scope.equals(  Artifact.SCOPE_SYSTEM ) )
303             {
304                 d.setSystemPath( systemPath );
305             }
306 
307             if ( exclusion != null )
308             {
309                 d.addExclusion( exclusion );
310             }
311 
312             project.getDependencies().add( d );
313 
314             return this;
315         }
316 
317         // Plugins
318         //
319         public ProjectBuilder addPlugin( Plugin plugin )
320         {
321             project.getBuildPlugins().add( plugin );
322             return this;
323         }
324 
325         public MavenProject get()
326         {
327             return project;
328         }
329     }
330 }