View Javadoc
1   package org.apache.maven.project;
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.Collections;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.maven.AbstractCoreMavenComponentTestCase;
29  import org.apache.maven.artifact.InvalidArtifactRTException;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.model.building.FileModelSource;
32  import org.apache.maven.model.building.ModelBuildingRequest;
33  import org.apache.maven.model.building.ModelSource;
34  import org.apache.maven.shared.utils.io.FileUtils;
35  
36  import com.google.common.io.Files;
37  
38  public class ProjectBuilderTest
39      extends AbstractCoreMavenComponentTestCase
40  {
41      @Override
42      protected String getProjectsDirectory()
43      {
44          return "src/test/projects/project-builder";
45      }
46  
47      public void testSystemScopeDependencyIsPresentInTheCompileClasspathElements()
48          throws Exception
49      {
50          File pom = getProject( "it0063" );
51  
52          Properties eps = new Properties();
53          eps.setProperty( "jre.home", new File( pom.getParentFile(), "jdk/jre" ).getPath() );
54  
55          MavenSession session = createMavenSession( pom, eps );
56          MavenProject project = session.getCurrentProject();
57  
58          // Here we will actually not have any artifacts because the ProjectDependenciesResolver is not involved here. So
59          // right now it's not valid to ask for artifacts unless plugins require the artifacts.
60  
61          project.getCompileClasspathElements();
62      }
63  
64      public void testBuildFromModelSource()
65          throws Exception
66      {
67          File pomFile = new File( "src/test/resources/projects/modelsource/module01/pom.xml" );
68          MavenSession mavenSession = createMavenSession( pomFile );
69          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
70          configuration.setRepositorySession( mavenSession.getRepositorySession() );
71          ModelSource modelSource = new FileModelSource( pomFile );
72          ProjectBuildingResult result =
73              lookup( org.apache.maven.project.ProjectBuilder.class ).build( modelSource, configuration );
74  
75          assertNotNull( result.getProject().getParentFile() );
76      }
77  
78      public void testVersionlessManagedDependency()
79          throws Exception
80      {
81          File pomFile = new File( "src/test/resources/projects/versionless-managed-dependency.xml" );
82          MavenSession mavenSession = createMavenSession( null );
83          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
84          configuration.setRepositorySession( mavenSession.getRepositorySession() );
85  
86          try
87          {
88              lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
89              fail();
90          }
91          catch ( ProjectBuildingException e )
92          {
93              // this is expected
94          }
95      }
96  
97      public void testResolveDependencies()
98          throws Exception
99      {
100         File pomFile = new File( "src/test/resources/projects/basic-resolveDependencies.xml" );
101         MavenSession mavenSession = createMavenSession( null );
102         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
103         configuration.setRepositorySession( mavenSession.getRepositorySession() );
104         configuration.setResolveDependencies( true );
105 
106         // single project build entry point
107         ProjectBuildingResult result = lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
108         assertEquals( 1, result.getProject().getArtifacts().size() );
109         // multi projects build entry point
110         List<ProjectBuildingResult> results = lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false, configuration );
111         assertEquals( 1, results.size() );
112         MavenProject mavenProject = results.get( 0 ).getProject();
113         assertEquals( 1, mavenProject.getArtifacts().size() );
114     }
115 
116     public void testDontResolveDependencies()
117         throws Exception
118     {
119         File pomFile = new File( "src/test/resources/projects/basic-resolveDependencies.xml" );
120         MavenSession mavenSession = createMavenSession( null );
121         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
122         configuration.setRepositorySession( mavenSession.getRepositorySession() );
123         configuration.setResolveDependencies( false );
124 
125         // single project build entry point
126         ProjectBuildingResult result = lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
127         assertEquals( 0, result.getProject().getArtifacts().size() );
128         // multi projects build entry point
129         List<ProjectBuildingResult> results = lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false, configuration );
130         assertEquals( 1, results.size() );
131         MavenProject mavenProject = results.get( 0 ).getProject();
132         assertEquals( 0, mavenProject.getArtifacts().size() );
133     }
134 
135     public void testReadModifiedPoms() throws Exception {
136         String initialValue = System.setProperty( DefaultProjectBuilder.DISABLE_GLOBAL_MODEL_CACHE_SYSTEM_PROPERTY, Boolean.toString( true ) );
137         // TODO a similar test should be created to test the dependency management (basically all usages
138         // of DefaultModelBuilder.getCache() are affected by MNG-6530
139         File tempDir = Files.createTempDir();
140         FileUtils.copyDirectoryStructure (new File( "src/test/resources/projects/grandchild-check"), tempDir );
141         try
142         {
143             MavenSession mavenSession = createMavenSession( null );
144             ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
145             configuration.setRepositorySession( mavenSession.getRepositorySession() );
146             org.apache.maven.project.ProjectBuilder projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class );
147             File child = new File( tempDir, "child/pom.xml" );
148             // build project once
149             projectBuilder.build( child, configuration );
150             // modify parent
151             File parent = new File( tempDir, "pom.xml" );
152             String parentContent = FileUtils.fileRead( parent );
153             parentContent = parentContent.replaceAll( "<packaging>pom</packaging>",
154             		"<packaging>pom</packaging><properties><addedProperty>addedValue</addedProperty></properties>" );
155             FileUtils.fileWrite( parent, "UTF-8", parentContent );
156             // re-build pom with modified parent
157             ProjectBuildingResult result = projectBuilder.build( child, configuration );
158             assertTrue( result.getProject().getProperties().containsKey( "addedProperty" ) );
159         }
160         finally
161         {
162             if ( initialValue == null )
163             {
164                 System.clearProperty( DefaultProjectBuilder.DISABLE_GLOBAL_MODEL_CACHE_SYSTEM_PROPERTY );
165             }
166             else
167             {
168                 System.setProperty( DefaultProjectBuilder.DISABLE_GLOBAL_MODEL_CACHE_SYSTEM_PROPERTY, initialValue );
169             }
170             FileUtils.deleteDirectory( tempDir );
171         }
172     }
173 
174     public void testReadErroneousMavenProjectContainsReference()
175         throws Exception
176     {
177         File pomFile = new File( "src/test/resources/projects/artifactMissingVersion.xml" ).getAbsoluteFile();
178         MavenSession mavenSession = createMavenSession( null );
179         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
180         configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
181         configuration.setRepositorySession( mavenSession.getRepositorySession() );
182         org.apache.maven.project.ProjectBuilder projectBuilder =
183             lookup( org.apache.maven.project.ProjectBuilder.class );
184 
185         // single project build entry point
186         try
187         {
188             projectBuilder.build( pomFile, configuration );
189         }
190         catch ( ProjectBuildingException ex )
191         {
192             assertEquals( 1, ex.getResults().size() );
193             MavenProject project = ex.getResults().get( 0 ).getProject();
194             assertNotNull( project );
195             assertEquals( "testArtifactMissingVersion", project.getArtifactId() );
196             assertEquals( pomFile, project.getFile() );
197         }
198 
199         // multi projects build entry point
200         try
201         {
202             projectBuilder.build( Collections.singletonList( pomFile ), false, configuration );
203         }
204         catch ( ProjectBuildingException ex )
205         {
206             assertEquals( 1, ex.getResults().size() );
207             MavenProject project = ex.getResults().get( 0 ).getProject();
208             assertNotNull( project );
209             assertEquals( "testArtifactMissingVersion", project.getArtifactId() );
210             assertEquals( pomFile, project.getFile() );
211         }
212     }
213 
214     public void testReadInvalidPom()
215         throws Exception
216     {
217         File pomFile = new File( "src/test/resources/projects/badPom.xml" ).getAbsoluteFile();
218         MavenSession mavenSession = createMavenSession( null );
219         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
220         configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
221         configuration.setRepositorySession( mavenSession.getRepositorySession() );
222         org.apache.maven.project.ProjectBuilder projectBuilder =
223             lookup( org.apache.maven.project.ProjectBuilder.class );
224 
225         // single project build entry point
226         try
227         {
228             projectBuilder.build( pomFile, configuration );
229         }
230         catch ( InvalidArtifactRTException iarte )
231         {
232             assertTrue( iarte.getMessage().contains( "The groupId cannot be empty." ) );
233         }
234 
235         // multi projects build entry point
236         try
237         {
238             projectBuilder.build( Collections.singletonList( pomFile ), false, configuration );
239         }
240         catch ( ProjectBuildingException ex )
241         {
242             assertEquals( 1, ex.getResults().size() );
243             MavenProject project = ex.getResults().get( 0 ).getProject();
244             assertNotNull( project );
245             assertNotSame( 0, ex.getResults().get( 0 ).getProblems().size() );
246         }
247     }
248 
249     public void testReadParentAndChildWithRegularVersionSetParentFile()
250         throws Exception
251     {
252         List<File> toRead = new ArrayList<>( 2 );
253         File parentPom = getProject( "MNG-6723" );
254         toRead.add( parentPom );
255         toRead.add( new File( parentPom.getParentFile(), "child/pom.xml" ) );
256         MavenSession mavenSession = createMavenSession( null );
257         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
258         configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
259         configuration.setRepositorySession( mavenSession.getRepositorySession() );
260         org.apache.maven.project.ProjectBuilder projectBuilder =
261             lookup( org.apache.maven.project.ProjectBuilder.class );
262 
263         // read poms separately
264         boolean parentFileWasFoundOnChild = false;
265         for ( File file : toRead )
266         {
267             List<ProjectBuildingResult> results = projectBuilder.build( Collections.singletonList( file ), false, configuration );
268             assertResultShowNoError( results );
269             MavenProject project = findChildProject( results );
270             if ( project != null )
271             {
272                 assertEquals( parentPom, project.getParentFile() );
273                 parentFileWasFoundOnChild = true;
274             }
275         }
276         assertTrue( parentFileWasFoundOnChild );
277 
278         // read projects together
279         List<ProjectBuildingResult> results = projectBuilder.build( toRead, false, configuration );
280         assertResultShowNoError( results );
281         assertEquals( parentPom, findChildProject( results ).getParentFile() );
282         Collections.reverse( toRead );
283         results = projectBuilder.build( toRead, false, configuration );
284         assertResultShowNoError( results );
285         assertEquals( parentPom, findChildProject( results ).getParentFile() );
286     }
287 
288     private MavenProject findChildProject( List<ProjectBuildingResult> results )
289     {
290         for ( ProjectBuildingResult result : results )
291         {
292             if ( result.getPomFile().getParentFile().getName().equals( "child" ) )
293             {
294                 return result.getProject();
295             }
296         }
297         return null;
298     }
299 
300     private void assertResultShowNoError(List<ProjectBuildingResult> results)
301     {
302         for ( ProjectBuildingResult result : results )
303         {
304             assertTrue( result.getProblems().isEmpty() );
305             assertNotNull( result.getProject() );
306         }
307     }
308 
309     public void testBuildProperties()
310             throws Exception
311     {
312         File file = new File( getProject( "MNG-6716" ).getParentFile(), "project/pom.xml" );
313         MavenSession mavenSession = createMavenSession( null );
314         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
315         configuration.setRepositorySession( mavenSession.getRepositorySession() );
316         configuration.setResolveDependencies( true );
317         List<ProjectBuildingResult> result = projectBuilder.build( Collections.singletonList(file), true, configuration );
318         MavenProject project = result.get(0).getProject();
319         // verify a few typical parameters are not duplicated
320         assertEquals( 1, project.getTestCompileSourceRoots().size() );
321         assertEquals( 1, project.getCompileSourceRoots().size() );
322         assertEquals( 1, project.getMailingLists().size() );
323         assertEquals( 1, project.getResources().size() );
324     }
325 }