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