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.Collections;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.maven.AbstractCoreMavenComponentTestCase;
28  import org.apache.maven.artifact.InvalidArtifactRTException;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.model.building.FileModelSource;
31  import org.apache.maven.model.building.ModelBuildingRequest;
32  import org.apache.maven.model.building.ModelSource;
33  import org.apache.maven.shared.utils.io.FileUtils;
34  
35  import com.google.common.io.Files;
36  
37  public class ProjectBuilderTest
38      extends AbstractCoreMavenComponentTestCase
39  {
40      @Override
41      protected String getProjectsDirectory()
42      {
43          return "src/test/projects/project-builder";
44      }
45  
46      public void testSystemScopeDependencyIsPresentInTheCompileClasspathElements()
47          throws Exception
48      {
49          File pom = getProject( "it0063" );
50  
51          Properties eps = new Properties();
52          eps.setProperty( "jre.home", new File( pom.getParentFile(), "jdk/jre" ).getPath() );
53  
54          MavenSession session = createMavenSession( pom, eps );
55          MavenProject project = session.getCurrentProject();
56  
57          // Here we will actually not have any artifacts because the ProjectDependenciesResolver is not involved here. So
58          // right now it's not valid to ask for artifacts unless plugins require the artifacts.
59  
60          project.getCompileClasspathElements();
61      }
62  
63      public void testBuildFromModelSource()
64          throws Exception
65      {
66          File pomFile = new File( "src/test/resources/projects/modelsource/module01/pom.xml" );
67          MavenSession mavenSession = createMavenSession( pomFile );
68          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
69          configuration.setRepositorySession( mavenSession.getRepositorySession() );
70          ModelSource modelSource = new FileModelSource( pomFile );
71          ProjectBuildingResult result =
72              lookup( org.apache.maven.project.ProjectBuilder.class ).build( modelSource, configuration );
73  
74          assertNotNull( result.getProject().getParentFile() );
75      }
76  
77      public void testVersionlessManagedDependency()
78          throws Exception
79      {
80          File pomFile = new File( "src/test/resources/projects/versionless-managed-dependency.xml" );
81          MavenSession mavenSession = createMavenSession( null );
82          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
83          configuration.setRepositorySession( mavenSession.getRepositorySession() );
84  
85          try
86          {
87              lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
88              fail();
89          }
90          catch ( ProjectBuildingException e )
91          {
92              // this is expected
93          }
94      }
95  
96      public void testResolveDependencies()
97          throws Exception
98      {
99          File pomFile = new File( "src/test/resources/projects/basic-resolveDependencies.xml" );
100         MavenSession mavenSession = createMavenSession( null );
101         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
102         configuration.setRepositorySession( mavenSession.getRepositorySession() );
103         configuration.setResolveDependencies( true );
104 
105         // single project build entry point
106         ProjectBuildingResult result = lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
107         assertEquals( 1, result.getProject().getArtifacts().size() );
108         // multi projects build entry point
109         List<ProjectBuildingResult> results = lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false, configuration );
110         assertEquals( 1, results.size() );
111         MavenProject mavenProject = results.get( 0 ).getProject();
112         assertEquals( 1, mavenProject.getArtifacts().size() );
113     }
114 
115     public void testDontResolveDependencies()
116         throws Exception
117     {
118         File pomFile = new File( "src/test/resources/projects/basic-resolveDependencies.xml" );
119         MavenSession mavenSession = createMavenSession( null );
120         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
121         configuration.setRepositorySession( mavenSession.getRepositorySession() );
122         configuration.setResolveDependencies( false );
123 
124         // single project build entry point
125         ProjectBuildingResult result = lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
126         assertEquals( 0, result.getProject().getArtifacts().size() );
127         // multi projects build entry point
128         List<ProjectBuildingResult> results = lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false, configuration );
129         assertEquals( 1, results.size() );
130         MavenProject mavenProject = results.get( 0 ).getProject();
131         assertEquals( 0, mavenProject.getArtifacts().size() );
132     }
133 
134     public void testReadModifiedPoms() throws Exception {
135         String initialValue = System.setProperty( DefaultProjectBuilder.DISABLE_GLOBAL_MODEL_CACHE_SYSTEM_PROPERTY, Boolean.toString( true ) );
136         // TODO a similar test should be created to test the dependency management (basically all usages
137         // of DefaultModelBuilder.getCache() are affected by MNG-6530
138         File tempDir = Files.createTempDir();
139         FileUtils.copyDirectoryStructure (new File( "src/test/resources/projects/grandchild-check"), tempDir );
140         try
141         {
142             MavenSession mavenSession = createMavenSession( null );
143             ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
144             configuration.setRepositorySession( mavenSession.getRepositorySession() );
145             org.apache.maven.project.ProjectBuilder projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class );
146             File child = new File( tempDir, "child/pom.xml" );
147             // build project once
148             projectBuilder.build( child, configuration );
149             // modify parent
150             File parent = new File( tempDir, "pom.xml" );
151             String parentContent = FileUtils.fileRead( parent );
152             parentContent = parentContent.replaceAll( "<packaging>pom</packaging>",
153             		"<packaging>pom</packaging><properties><addedProperty>addedValue</addedProperty></properties>" );
154             FileUtils.fileWrite( parent, "UTF-8", parentContent );
155             // re-build pom with modified parent
156             ProjectBuildingResult result = projectBuilder.build( child, configuration );
157             assertTrue( result.getProject().getProperties().containsKey( "addedProperty" ) );
158         }
159         finally
160         {
161             if ( initialValue == null )
162             {
163                 System.clearProperty( DefaultProjectBuilder.DISABLE_GLOBAL_MODEL_CACHE_SYSTEM_PROPERTY );
164             }
165             else
166             {
167                 System.setProperty( DefaultProjectBuilder.DISABLE_GLOBAL_MODEL_CACHE_SYSTEM_PROPERTY, initialValue );
168             }
169             FileUtils.deleteDirectory( tempDir );
170         }
171     }
172 
173     public void testReadErroneousMavenProjectContainsReference()
174         throws Exception
175     {
176         File pomFile = new File( "src/test/resources/projects/artifactMissingVersion.xml" ).getAbsoluteFile();
177         MavenSession mavenSession = createMavenSession( null );
178         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
179         configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
180         configuration.setRepositorySession( mavenSession.getRepositorySession() );
181         org.apache.maven.project.ProjectBuilder projectBuilder =
182             lookup( org.apache.maven.project.ProjectBuilder.class );
183 
184         // single project build entry point
185         try
186         {
187             projectBuilder.build( pomFile, configuration );
188         }
189         catch ( ProjectBuildingException ex )
190         {
191             assertEquals( 1, ex.getResults().size() );
192             MavenProject project = ex.getResults().get( 0 ).getProject();
193             assertNotNull( project );
194             assertEquals( "testArtifactMissingVersion", project.getArtifactId() );
195             assertEquals( pomFile, project.getFile() );
196         }
197 
198         // multi projects build entry point
199         try
200         {
201             projectBuilder.build( Collections.singletonList( pomFile ), false, configuration );
202         }
203         catch ( ProjectBuildingException ex )
204         {
205             assertEquals( 1, ex.getResults().size() );
206             MavenProject project = ex.getResults().get( 0 ).getProject();
207             assertNotNull( project );
208             assertEquals( "testArtifactMissingVersion", project.getArtifactId() );
209             assertEquals( pomFile, project.getFile() );
210         }
211     }
212 
213     public void testReadInvalidPom()
214         throws Exception
215     {
216         File pomFile = new File( "src/test/resources/projects/badPom.xml" ).getAbsoluteFile();
217         MavenSession mavenSession = createMavenSession( null );
218         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
219         configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
220         configuration.setRepositorySession( mavenSession.getRepositorySession() );
221         org.apache.maven.project.ProjectBuilder projectBuilder =
222             lookup( org.apache.maven.project.ProjectBuilder.class );
223 
224         // single project build entry point
225         try
226         {
227             projectBuilder.build( pomFile, configuration );
228         }
229         catch ( InvalidArtifactRTException iarte )
230         {
231             assertTrue( iarte.getMessage().contains( "The groupId cannot be empty." ) );
232         }
233 
234         // multi projects build entry point
235         try
236         {
237             projectBuilder.build( Collections.singletonList( pomFile ), false, configuration );
238         }
239         catch ( ProjectBuildingException ex )
240         {
241             assertEquals( 1, ex.getResults().size() );
242             MavenProject project = ex.getResults().get( 0 ).getProject();
243             assertNotNull( project );
244             assertNotSame( 0, ex.getResults().get( 0 ).getProblems().size() );
245         }
246     }
247 
248 }