View Javadoc

1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  import java.net.URL;
23  import java.util.Arrays;
24  
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27  import org.apache.maven.model.building.ModelBuildingException;
28  import org.apache.maven.model.building.ModelProblem;
29  import org.apache.maven.repository.RepositorySystem;
30  import org.apache.maven.repository.internal.MavenRepositorySystemSession;
31  import org.codehaus.plexus.PlexusTestCase;
32  
33  /**
34   * @author Jason van Zyl
35   */
36  public abstract class AbstractMavenProjectTestCase
37      extends PlexusTestCase
38  {
39      protected ProjectBuilder projectBuilder;
40  
41      protected RepositorySystem repositorySystem;
42      
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47          
48          if ( getContainer().hasComponent( ProjectBuilder.class, "test" ) )
49          {
50              projectBuilder = lookup( ProjectBuilder.class, "test" );
51          }
52          else
53          {
54              // default over to the main project builder...
55              projectBuilder = lookup( ProjectBuilder.class );
56          }
57          
58          repositorySystem = lookup( RepositorySystem.class );        
59      }    
60  
61      @Override
62      protected void tearDown()
63          throws Exception
64      {
65          projectBuilder = null;
66  
67          super.tearDown();
68      }
69  
70      protected ProjectBuilder getProjectBuilder()
71      {
72          return projectBuilder;
73      }
74  
75      @Override
76      protected String getCustomConfigurationName()
77      {
78          String name = AbstractMavenProjectTestCase.class.getName().replace( '.', '/' ) + ".xml";
79          System.out.println( name );
80          return name;
81      }
82  
83      // ----------------------------------------------------------------------
84      // Local repository
85      // ----------------------------------------------------------------------
86  
87      protected File getLocalRepositoryPath()
88          throws FileNotFoundException, URISyntaxException
89      {
90          File markerFile = getFileForClasspathResource( "local-repo/marker.txt" );
91  
92          return markerFile.getAbsoluteFile().getParentFile();
93      }
94  
95      protected static File getFileForClasspathResource( String resource )
96          throws FileNotFoundException
97      {
98          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
99  
100         URL resourceUrl = cloader.getResource( resource );
101 
102         if ( resourceUrl == null )
103         {
104             throw new FileNotFoundException( "Unable to find: " + resource );
105         }
106 
107         return new File( URI.create( resourceUrl.toString().replaceAll( " ", "%20" ) ) );
108     }
109 
110     protected ArtifactRepository getLocalRepository()
111         throws Exception
112     {
113         ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "legacy" );
114 
115         ArtifactRepository r = repositorySystem.createArtifactRepository( "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null );
116 
117         return r;
118     }
119 
120     // ----------------------------------------------------------------------
121     // Project building
122     // ----------------------------------------------------------------------
123 
124     protected MavenProject getProjectWithDependencies( File pom )
125         throws Exception
126     {
127         ProjectBuildingRequest configuration = newBuildingRequest();
128         configuration.setRemoteRepositories( Arrays.asList( new ArtifactRepository[] {} ) );
129         configuration.setProcessPlugins( false );
130         configuration.setResolveDependencies( true );
131 
132         try
133         {
134             return projectBuilder.build( pom, configuration ).getProject();
135         }
136         catch ( Exception e )
137         {
138             Throwable cause = e.getCause();
139             if ( cause instanceof ModelBuildingException )
140             {
141                 String message = "In: " + pom + "\n\n";
142                 for ( ModelProblem problem : ( (ModelBuildingException) cause ).getProblems() )
143                 {
144                     message += problem + "\n";
145                 }
146                 System.out.println( message );
147             }
148 
149             throw e;
150         }
151     }
152 
153     protected MavenProject getProject( File pom )
154         throws Exception
155     {
156         ProjectBuildingRequest configuration = newBuildingRequest();
157 
158         return projectBuilder.build( pom, configuration ).getProject();
159     }
160 
161     protected ProjectBuildingRequest newBuildingRequest()
162         throws Exception
163     {
164         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
165         configuration.setLocalRepository( getLocalRepository() );
166         initRepoSession( configuration );
167         return configuration;
168     }
169 
170     protected void initRepoSession( ProjectBuildingRequest request )
171     {
172         File localRepo = new File( request.getLocalRepository().getBasedir() );
173         MavenRepositorySystemSession repoSession = new MavenRepositorySystemSession();
174         repoSession.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
175         request.setRepositorySession( repoSession );
176     }
177 
178 }