View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.io.File;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Properties;
29  import java.util.concurrent.atomic.AtomicInteger;
30  
31  import org.apache.commons.io.FileUtils;
32  import org.apache.maven.AbstractCoreMavenComponentTestCase;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.model.Dependency;
35  import org.apache.maven.model.InputLocation;
36  import org.apache.maven.model.Plugin;
37  import org.apache.maven.model.building.FileModelSource;
38  import org.apache.maven.model.building.ModelBuildingRequest;
39  import org.apache.maven.model.building.ModelProblem;
40  import org.apache.maven.model.building.ModelSource;
41  import org.junit.jupiter.api.Test;
42  import org.junit.jupiter.api.io.TempDir;
43  
44  import static org.apache.maven.project.ProjectBuildingResultWithLocationMatcher.projectBuildingResultWithLocation;
45  import static org.apache.maven.project.ProjectBuildingResultWithProblemMessageMatcher.projectBuildingResultWithProblemMessage;
46  import static org.hamcrest.MatcherAssert.assertThat;
47  import static org.hamcrest.Matchers.contains;
48  import static org.hamcrest.Matchers.containsString;
49  import static org.hamcrest.Matchers.empty;
50  import static org.hamcrest.Matchers.greaterThan;
51  import static org.hamcrest.Matchers.hasKey;
52  import static org.hamcrest.Matchers.is;
53  import static org.junit.jupiter.api.Assertions.assertEquals;
54  import static org.junit.jupiter.api.Assertions.assertNotNull;
55  import static org.junit.jupiter.api.Assertions.assertThrows;
56  import static org.junit.jupiter.api.Assertions.assertTrue;
57  
58  class ProjectBuilderTest extends AbstractCoreMavenComponentTestCase {
59      @Override
60      protected String getProjectsDirectory() {
61          return "src/test/projects/project-builder";
62      }
63  
64      @Test
65      void testSystemScopeDependencyIsPresentInTheCompileClasspathElements() throws Exception {
66          File pom = getProject("it0063");
67  
68          Properties eps = new Properties();
69          eps.setProperty("jre.home", new File(pom.getParentFile(), "jdk/jre").getPath());
70  
71          MavenSession session = createMavenSession(pom, eps);
72          MavenProject project = session.getCurrentProject();
73  
74          // Here we will actually not have any artifacts because the ProjectDependenciesResolver is not involved here. So
75          // right now it's not valid to ask for artifacts unless plugins require the artifacts.
76  
77          project.getCompileClasspathElements();
78      }
79  
80      @Test
81      void testBuildFromModelSource() throws Exception {
82          File pomFile = new File("src/test/resources/projects/modelsource/module01/pom.xml");
83          MavenSession mavenSession = createMavenSession(pomFile);
84          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
85          configuration.setRepositorySession(mavenSession.getRepositorySession());
86          ModelSource modelSource = new FileModelSource(pomFile);
87          ProjectBuildingResult result = getContainer()
88                  .lookup(org.apache.maven.project.ProjectBuilder.class)
89                  .build(modelSource, configuration);
90  
91          assertNotNull(result.getProject().getParentFile());
92      }
93  
94      @Test
95      void testVersionlessManagedDependency() throws Exception {
96          File pomFile = new File("src/test/resources/projects/versionless-managed-dependency.xml");
97          MavenSession mavenSession = createMavenSession(null);
98          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
99          configuration.setRepositorySession(mavenSession.getRepositorySession());
100 
101         ProjectBuildingException e = assertThrows(ProjectBuildingException.class, () -> getContainer()
102                 .lookup(org.apache.maven.project.ProjectBuilder.class)
103                 .build(pomFile, configuration));
104         assertThat(
105                 e.getResults(),
106                 contains(projectBuildingResultWithProblemMessage(
107                         "'dependencies.dependency.version' for org.apache.maven.its:a:jar is missing")));
108         assertThat(e.getResults(), contains(projectBuildingResultWithLocation(17, 9)));
109     }
110 
111     @Test
112     void testResolveDependencies() throws Exception {
113         File pomFile = new File("src/test/resources/projects/basic-resolveDependencies.xml");
114         MavenSession mavenSession = createMavenSession(null);
115         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
116         configuration.setRepositorySession(mavenSession.getRepositorySession());
117         configuration.setResolveDependencies(true);
118 
119         // single project build entry point
120         ProjectBuildingResult result = getContainer()
121                 .lookup(org.apache.maven.project.ProjectBuilder.class)
122                 .build(pomFile, configuration);
123         assertEquals(1, result.getProject().getArtifacts().size());
124         // multi projects build entry point
125         List<ProjectBuildingResult> results = getContainer()
126                 .lookup(org.apache.maven.project.ProjectBuilder.class)
127                 .build(Collections.singletonList(pomFile), false, configuration);
128         assertEquals(1, results.size());
129         MavenProject mavenProject = results.get(0).getProject();
130         assertEquals(1, mavenProject.getArtifacts().size());
131 
132         final MavenProject project = mavenProject;
133         final AtomicInteger artifactsResultInAnotherThread = new AtomicInteger();
134         Thread t = new Thread(new Runnable() {
135             @Override
136             public void run() {
137                 artifactsResultInAnotherThread.set(project.getArtifacts().size());
138             }
139         });
140         t.start();
141         t.join();
142         assertEquals(project.getArtifacts().size(), artifactsResultInAnotherThread.get());
143     }
144 
145     @Test
146     void testDontResolveDependencies() throws Exception {
147         File pomFile = new File("src/test/resources/projects/basic-resolveDependencies.xml");
148         MavenSession mavenSession = createMavenSession(null);
149         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
150         configuration.setRepositorySession(mavenSession.getRepositorySession());
151         configuration.setResolveDependencies(false);
152 
153         // single project build entry point
154         ProjectBuildingResult result = getContainer()
155                 .lookup(org.apache.maven.project.ProjectBuilder.class)
156                 .build(pomFile, configuration);
157         assertEquals(0, result.getProject().getArtifacts().size());
158         // multi projects build entry point
159         List<ProjectBuildingResult> results = getContainer()
160                 .lookup(org.apache.maven.project.ProjectBuilder.class)
161                 .build(Collections.singletonList(pomFile), false, configuration);
162         assertEquals(1, results.size());
163         MavenProject mavenProject = results.get(0).getProject();
164         assertEquals(0, mavenProject.getArtifacts().size());
165     }
166 
167     @Test
168     void testReadModifiedPoms(@TempDir Path tempDir) throws Exception {
169         // TODO a similar test should be created to test the dependency management (basically all usages
170         // of DefaultModelBuilder.getCache() are affected by MNG-6530
171 
172         FileUtils.copyDirectory(new File("src/test/resources/projects/grandchild-check"), tempDir.toFile());
173         MavenSession mavenSession = createMavenSession(null);
174         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
175         configuration.setRepositorySession(mavenSession.getRepositorySession());
176         org.apache.maven.project.ProjectBuilder projectBuilder =
177                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
178         File child = new File(tempDir.toFile(), "child/pom.xml");
179         // build project once
180         projectBuilder.build(child, configuration);
181         // modify parent
182         File parent = new File(tempDir.toFile(), "pom.xml");
183         String parentContent = new String(Files.readAllBytes(parent.toPath()), StandardCharsets.UTF_8);
184         parentContent = parentContent.replace(
185                 "<packaging>pom</packaging>",
186                 "<packaging>pom</packaging><properties><addedProperty>addedValue</addedProperty></properties>");
187         Files.write(parent.toPath(), parentContent.getBytes(StandardCharsets.UTF_8));
188         // re-build pom with modified parent
189         ProjectBuildingResult result = projectBuilder.build(child, configuration);
190         assertThat(result.getProject().getProperties(), hasKey((Object) "addedProperty"));
191     }
192 
193     @Test
194     void testReadErroneousMavenProjectContainsReference() throws Exception {
195         File pomFile = new File("src/test/resources/projects/artifactMissingVersion.xml").getAbsoluteFile();
196         MavenSession mavenSession = createMavenSession(null);
197         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
198         configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
199         configuration.setRepositorySession(mavenSession.getRepositorySession());
200         org.apache.maven.project.ProjectBuilder projectBuilder =
201                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
202 
203         // single project build entry point
204         ProjectBuildingException ex1 =
205                 assertThrows(ProjectBuildingException.class, () -> projectBuilder.build(pomFile, configuration));
206 
207         assertEquals(1, ex1.getResults().size());
208         MavenProject project1 = ex1.getResults().get(0).getProject();
209         assertNotNull(project1);
210         assertEquals("testArtifactMissingVersion", project1.getArtifactId());
211         assertEquals(pomFile, project1.getFile());
212 
213         // multi projects build entry point
214         ProjectBuildingException ex2 = assertThrows(
215                 ProjectBuildingException.class,
216                 () -> projectBuilder.build(Collections.singletonList(pomFile), false, configuration));
217 
218         assertEquals(1, ex2.getResults().size());
219         MavenProject project2 = ex2.getResults().get(0).getProject();
220         assertNotNull(project2);
221         assertEquals("testArtifactMissingVersion", project2.getArtifactId());
222         assertEquals(pomFile, project2.getFile());
223     }
224 
225     @Test
226     void testReadInvalidPom() throws Exception {
227         File pomFile = new File("src/test/resources/projects/badPom.xml").getAbsoluteFile();
228         MavenSession mavenSession = createMavenSession(null);
229         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
230         configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
231         configuration.setRepositorySession(mavenSession.getRepositorySession());
232         org.apache.maven.project.ProjectBuilder projectBuilder =
233                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
234 
235         // single project build entry point
236         Exception ex = assertThrows(Exception.class, () -> projectBuilder.build(pomFile, configuration));
237         assertThat(ex.getMessage(), containsString("expected START_TAG or END_TAG not TEXT"));
238 
239         // multi projects build entry point
240         ProjectBuildingException pex = assertThrows(
241                 ProjectBuildingException.class,
242                 () -> projectBuilder.build(Collections.singletonList(pomFile), false, configuration));
243         assertEquals(1, pex.getResults().size());
244         assertNotNull(pex.getResults().get(0).getPomFile());
245         assertThat(pex.getResults().get(0).getProblems().size(), greaterThan(0));
246         assertThat(
247                 pex.getResults(),
248                 contains(projectBuildingResultWithProblemMessage("expected START_TAG or END_TAG not TEXT")));
249     }
250 
251     @Test
252     void testReadParentAndChildWithRegularVersionSetParentFile() throws Exception {
253         List<File> toRead = new ArrayList<>(2);
254         File parentPom = getProject("MNG-6723");
255         toRead.add(parentPom);
256         toRead.add(new File(parentPom.getParentFile(), "child/pom.xml"));
257         MavenSession mavenSession = createMavenSession(null);
258         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
259         configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
260         configuration.setRepositorySession(mavenSession.getRepositorySession());
261         org.apache.maven.project.ProjectBuilder projectBuilder =
262                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
263 
264         // read poms separately
265         boolean parentFileWasFoundOnChild = false;
266         for (File file : toRead) {
267             List<ProjectBuildingResult> results =
268                     projectBuilder.build(Collections.singletonList(file), false, configuration);
269             assertResultShowNoError(results);
270             MavenProject project = findChildProject(results);
271             if (project != null) {
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         for (ProjectBuildingResult result : results) {
290             if (result.getPomFile().getParentFile().getName().equals("child")) {
291                 return result.getProject();
292             }
293         }
294         return null;
295     }
296 
297     private void assertResultShowNoError(List<ProjectBuildingResult> results) {
298         for (ProjectBuildingResult result : results) {
299             assertThat(result.getProblems(), is(empty()));
300             assertNotNull(result.getProject());
301         }
302     }
303 
304     @Test
305     void testBuildProperties() throws Exception {
306         File file = new File(getProject("MNG-6716").getParentFile(), "project/pom.xml");
307         MavenSession mavenSession = createMavenSession(null);
308         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
309         configuration.setRepositorySession(mavenSession.getRepositorySession());
310         configuration.setResolveDependencies(true);
311         List<ProjectBuildingResult> result = projectBuilder.build(Collections.singletonList(file), true, configuration);
312         MavenProject project = result.get(0).getProject();
313         // verify a few typical parameters are not duplicated
314         assertEquals(1, project.getTestCompileSourceRoots().size());
315         assertEquals(1, project.getCompileSourceRoots().size());
316         assertEquals(1, project.getMailingLists().size());
317         assertEquals(1, project.getResources().size());
318     }
319 
320     @Test
321     void testPropertyInPluginManagementGroupId() throws Exception {
322         File pom = getProject("MNG-6983");
323 
324         MavenSession session = createMavenSession(pom);
325         MavenProject project = session.getCurrentProject();
326 
327         for (Plugin buildPlugin : project.getBuildPlugins()) {
328             assertNotNull(buildPlugin.getVersion(), "Missing version for build plugin " + buildPlugin.getKey());
329         }
330     }
331 
332     @Test
333     void testBuildFromModelSourceResolvesBasedir() throws Exception {
334         File pomFile = new File("src/test/resources/projects/modelsourcebasedir/pom.xml");
335         MavenSession mavenSession = createMavenSession(null);
336         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
337         configuration.setRepositorySession(mavenSession.getRepositorySession());
338         ModelSource modelSource = new FileModelSource(pomFile);
339         ProjectBuildingResult result = getContainer()
340                 .lookup(org.apache.maven.project.ProjectBuilder.class)
341                 .build(modelSource, configuration);
342 
343         assertEquals(
344                 pomFile.getAbsoluteFile(),
345                 result.getProject().getModel().getPomFile().getAbsoluteFile());
346         int errors = 0;
347         for (ModelProblem p : result.getProblems()) {
348             if (p.getSeverity() == ModelProblem.Severity.ERROR) {
349                 errors++;
350             }
351         }
352         assertEquals(0, errors);
353     }
354 
355     @Test
356     void testLocationTrackingResolution() throws Exception {
357         File pom = getProject("MNG-7648");
358 
359         MavenSession session = createMavenSession(pom);
360         MavenProject project = session.getCurrentProject();
361 
362         InputLocation dependencyLocation = null;
363         for (Dependency dependency : project.getDependencies()) {
364             if (dependency.getManagementKey().equals("org.apache.maven.its:a:jar")) {
365                 dependencyLocation = dependency.getLocation("version");
366             }
367         }
368         assertNotNull(dependencyLocation, "missing dependency");
369         assertEquals(
370                 "org.apache.maven.its:bom:0.1", dependencyLocation.getSource().getModelId());
371 
372         InputLocation pluginLocation = null;
373         for (Plugin plugin : project.getBuildPlugins()) {
374             if (plugin.getKey().equals("org.apache.maven.plugins:maven-clean-plugin")) {
375                 pluginLocation = plugin.getLocation("version");
376             }
377         }
378         assertNotNull(pluginLocation, "missing build plugin");
379         assertEquals(
380                 "org.apache.maven.its:parent:0.1", pluginLocation.getSource().getModelId());
381     }
382 }