From 2e66809e0efcf84c551664efff82113b66591a74 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Sat, 4 Jul 2020 23:27:12 +0200 Subject: [PATCH] [MNG-6952] Fail early if pom cannot be transformed --- .../maven/project/ProjectBuilderTest.java | 14 +++++----- .../AbstractModelSourceTransformer.java | 27 +++++++++++++++++++ .../model/building/DefaultModelBuilder.java | 2 +- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java index 538f887b02..5493d89970 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java @@ -19,11 +19,12 @@ package org.apache.maven.project; * under the License. */ +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; import java.io.File; import java.nio.file.Files; @@ -34,7 +35,6 @@ import java.util.List; import java.util.Properties; import org.apache.maven.AbstractCoreMavenComponentTestCase; -import org.apache.maven.artifact.InvalidArtifactRTException; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.building.FileModelSource; import org.apache.maven.model.building.ModelBuildingRequest; @@ -235,9 +235,9 @@ public class ProjectBuilderTest { projectBuilder.build( pomFile, configuration ); } - catch ( InvalidArtifactRTException iarte ) + catch ( Exception ex ) { - assertThat( iarte.getMessage(), containsString( "The groupId cannot be empty." ) ); + assertThat( ex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) ); } // multi projects build entry point @@ -248,9 +248,9 @@ public class ProjectBuilderTest 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() ); + assertNotNull( ex.getResults().get( 0 ).getPomFile() ); + assertThat( ex.getResults().get( 0 ).getProblems().size(), greaterThan( 0 ) ); + assertThat( ex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) ); } } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/AbstractModelSourceTransformer.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/AbstractModelSourceTransformer.java index 31b88bfa7d..6f0f5d4b3f 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/AbstractModelSourceTransformer.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/AbstractModelSourceTransformer.java @@ -41,7 +41,9 @@ import javax.xml.transform.stream.StreamResult; import org.apache.maven.xml.Factories; import org.apache.maven.xml.sax.ext.CommentRenormalizer; import org.apache.maven.xml.sax.filter.AbstractSAXFilter; +import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; /** * Offers a transformation implementation based on PipelineStreams. @@ -82,6 +84,31 @@ public abstract class AbstractModelSourceTransformer { filter = getSAXFilter( pomFile, context ); filter.setLexicalHandler( transformerHandler ); + // By default errors are written to stderr. + // Hence set custom errorHandler to reduce noice + filter.setErrorHandler( new ErrorHandler() + { + @Override + public void warning( SAXParseException exception ) + throws SAXException + { + throw exception; + } + + @Override + public void fatalError( SAXParseException exception ) + throws SAXException + { + throw exception; + } + + @Override + public void error( SAXParseException exception ) + throws SAXException + { + throw exception; + } + } ); } catch ( TransformerConfigurationException | SAXException | ParserConfigurationException e ) { diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index 77a117da7a..1aba9bc70d 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -680,7 +680,7 @@ public class DefaultModelBuilder } catch ( IOException e ) { - problems.add( new ModelProblemCollectorRequest( Severity.WARNING, Version.V37 ).setException( e ) ); + problems.add( new ModelProblemCollectorRequest( Severity.FATAL, Version.V37 ).setException( e ) ); } }