[MNG-6533] ProjectBuilder report ModelProblem instead of Exception
authorMickael Istria <mistria@redhat.com>
Fri, 30 Nov 2018 10:28:03 +0000 (11:28 +0100)
committerHervé Boutemy <hboutemy@apache.org>
Mon, 14 Jan 2019 12:55:45 +0000 (13:55 +0100)
Sending ModelProblems allows to keep processing other pom files.

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/badPom.xml [new file with mode: 0644]

index bf94abc..df2aac9 100644 (file)
@@ -459,8 +459,16 @@ public class DefaultProjectBuilder
         }
 
         Model model = result.getEffectiveModel();
-        // first pass: build without building parent.
-        initProject( project, projectIndex, false, result, new HashMap<File, Boolean>( 0 ), config.request );
+        try
+        {
+            // first pass: build without building parent.
+            initProject( project, projectIndex, false, result, new HashMap<File, Boolean>( 0 ), config.request );
+        }
+        catch ( Exception e )
+        {
+            result.getProblems().add( new DefaultModelProblem( null, ModelProblem.Severity.ERROR, null, model, -1, -1,
+                  e ) );
+        }
 
         projectIndex.put( result.getModelIds().get( 0 ), project );
 
@@ -607,7 +615,15 @@ public class DefaultProjectBuilder
                 ModelBuildingResult result = modelBuilder.build( interimResult.request, interimResult.result );
 
                 // 2nd pass of initialization: resolve and build parent if necessary
-                initProject( project, projectIndex, true, result, profilesXmls, request );
+                try
+                {
+                    initProject( project, projectIndex, true, result, profilesXmls, request );
+                }
+                catch ( Exception e )
+                {
+                    result.getProblems().add( new DefaultModelProblem( null, ModelProblem.Severity.ERROR, null,
+                            result.getEffectiveModel(), -1, -1, e ) );
+                }
 
                 List<MavenProject> modules = new ArrayList<>();
                 noErrors =
index 369b22e..624a9d1 100644 (file)
@@ -208,4 +208,30 @@ public class ProjectBuilderTest
             assertEquals( pomFile, project.getFile() );
         }
     }
+
+    public void testReadInvalidPom()
+        throws Exception
+    {
+        File pomFile = new File( "src/test/resources/projects/badPom.xml" ).getAbsoluteFile();
+        MavenSession mavenSession = createMavenSession( null );
+        ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
+        configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
+        configuration.setRepositorySession( mavenSession.getRepositorySession() );
+        org.apache.maven.project.ProjectBuilder projectBuilder =
+            lookup( org.apache.maven.project.ProjectBuilder.class );
+
+        // multi projects build entry point
+        try
+        {
+            projectBuilder.build( Collections.singletonList( pomFile ), false, configuration );
+        }
+        catch ( ProjectBuildingException ex )
+        {
+            assertEquals( 1, ex.getResults().size() );
+            MavenProject project = ex.getResults().get( 0 ).getProject();
+            assertNotNull( project );
+            assertNotSame( 0, ex.getResults().get( 0 ).getProblems().size() );
+        }
+    }
+
 }
diff --git a/maven-core/src/test/resources/projects/badPom.xml b/maven-core/src/test/resources/projects/badPom.xml
new file mode 100644 (file)
index 0000000..219ae94
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  some garbage maven project builder can't parse
+
+</project>