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