[MNG-6529] ProjectBuilder.build(List<Project> ...) honor 193/head
authorMickael Istria <mistria@redhat.com>
Tue, 27 Nov 2018 14:54:14 +0000 (15:54 +0100)
committerMickael Istria <mistria@redhat.com>
Tue, 18 Dec 2018 08:04:17 +0000 (09:04 +0100)
request.isResolveDependency

maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
maven-core/src/test/resources/projects/basic-resolveDependencies.xml [new file with mode: 0644]

index a659046..35a4e9f 100644 (file)
@@ -371,7 +371,7 @@ public class DefaultProjectBuilder
         {
             noErrors =
                 build( results, new ArrayList<MavenProject>(), projectIndex, interimResults, request,
-                       new HashMap<File, Boolean>() ) && noErrors;
+                       new HashMap<File, Boolean>(), config.session ) && noErrors;
         }
         finally
         {
@@ -572,7 +572,8 @@ public class DefaultProjectBuilder
 
     private boolean build( List<ProjectBuildingResult> results, List<MavenProject> projects,
                            Map<String, MavenProject> projectIndex, List<InterimResult> interimResults,
-                           ProjectBuildingRequest request, Map<File, Boolean> profilesXmls )
+                           ProjectBuildingRequest request, Map<File, Boolean> profilesXmls,
+                           RepositorySystemSession session )
     {
         boolean noErrors = true;
 
@@ -587,15 +588,21 @@ public class DefaultProjectBuilder
 
                 List<MavenProject> modules = new ArrayList<>();
                 noErrors =
-                    build( results, modules, projectIndex, interimResult.modules, request, profilesXmls ) && noErrors;
+                    build( results, modules, projectIndex, interimResult.modules, request, profilesXmls, session )
+                    && noErrors;
 
                 projects.addAll( modules );
                 projects.add( project );
 
                 project.setExecutionRoot( interimResult.root );
                 project.setCollectedProjects( modules );
+                DependencyResolutionResult resolutionResult = null;
+                if ( request.isResolveDependencies() )
+                {
+                    resolutionResult = resolveDependencies( project, session );
+                }
 
-                results.add( new DefaultProjectBuildingResult( project, result.getProblems(), null ) );
+                results.add( new DefaultProjectBuildingResult( project, result.getProblems(), resolutionResult ) );
             }
             catch ( ModelBuildingException e )
             {
index 5511cb1..18f22bd 100644 (file)
@@ -20,6 +20,8 @@ package org.apache.maven.project;
  */
 
 import java.io.File;
+import java.util.Collections;
+import java.util.List;
 import java.util.Properties;
 
 import org.apache.maven.AbstractCoreMavenComponentTestCase;
@@ -30,6 +32,7 @@ import org.apache.maven.model.building.ModelSource;
 public class ProjectBuilderTest
     extends AbstractCoreMavenComponentTestCase
 {
+    @Override
     protected String getProjectsDirectory()
     {
         return "src/test/projects/project-builder";
@@ -84,4 +87,43 @@ public class ProjectBuilderTest
             // this is expected
         }
     }
+
+    public void testResolveDependencies()
+        throws Exception
+    {
+        File pomFile = new File( "src/test/resources/projects/basic-resolveDependencies.xml" );
+        MavenSession mavenSession = createMavenSession( null );
+        ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
+        configuration.setRepositorySession( mavenSession.getRepositorySession() );
+        configuration.setResolveDependencies( true );
+
+        // single project build entry point
+        ProjectBuildingResult result = lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
+        assertEquals( 1, result.getProject().getArtifacts().size() );
+        // multi projects build entry point
+        List<ProjectBuildingResult> results = lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false, configuration );
+        assertEquals( 1, results.size() );
+        MavenProject mavenProject = results.get( 0 ).getProject();
+        assertEquals( 1, mavenProject.getArtifacts().size() );
+    }
+
+    public void testDontResolveDependencies()
+        throws Exception
+    {
+        File pomFile = new File( "src/test/resources/projects/basic-resolveDependencies.xml" );
+        MavenSession mavenSession = createMavenSession( null );
+        ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
+        configuration.setRepositorySession( mavenSession.getRepositorySession() );
+        configuration.setResolveDependencies( false );
+
+        // single project build entry point
+        ProjectBuildingResult result = lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
+        assertEquals( 0, result.getProject().getArtifacts().size() );
+        // multi projects build entry point
+        List<ProjectBuildingResult> results = lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false, configuration );
+        assertEquals( 1, results.size() );
+        MavenProject mavenProject = results.get( 0 ).getProject();
+        assertEquals( 0, mavenProject.getArtifacts().size() );
+    }
+
 }
diff --git a/maven-core/src/test/resources/projects/basic-resolveDependencies.xml b/maven-core/src/test/resources/projects/basic-resolveDependencies.xml
new file mode 100644 (file)
index 0000000..d25326b
--- /dev/null
@@ -0,0 +1,15 @@
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>test</groupId>
+  <artifactId>basic-resolveDependencies.xml</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.its</groupId>
+      <artifactId>a</artifactId>
+      <version>0.1</version>
+    </dependency>
+  </dependencies>
+</project>
\ No newline at end of file