Skip to content

Commit

Permalink
[MNG-6533] ProjectBuilder report ModelProblem instead of Exception
Browse files Browse the repository at this point in the history
Sending ModelProblems allows to keep processing other pom files.
  • Loading branch information
mickaelistria committed Nov 30, 2018
1 parent 51ee05c commit ff2a6de
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,16 @@ private boolean build( List<ProjectBuildingResult> results, List<InterimResult>
}

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 );

Expand Down Expand Up @@ -595,7 +603,15 @@ private boolean build( List<ProjectBuildingResult> results, List<MavenProject> p
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,24 @@ public void testReadErroneousMavenProjectContainsReference() throws Exception
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 );
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() );
}
}

}
8 changes: 8 additions & 0 deletions maven-core/src/test/resources/projects/badPom.xml
Original file line number Diff line number Diff line change
@@ -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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

some garbage maven project builder can't parse

</project>

0 comments on commit ff2a6de

Please sign in to comment.