[MNG-6952] Fail early if pom cannot be transformed

This commit is contained in:
rfscholte 2020-07-04 23:27:12 +02:00
parent 65ec04c236
commit 2e66809e0e
3 changed files with 35 additions and 8 deletions

View File

@ -19,11 +19,12 @@
* under the License. * under the License.
*/ */
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.File; import java.io.File;
import java.nio.file.Files; import java.nio.file.Files;
@ -34,7 +35,6 @@
import java.util.Properties; import java.util.Properties;
import org.apache.maven.AbstractCoreMavenComponentTestCase; import org.apache.maven.AbstractCoreMavenComponentTestCase;
import org.apache.maven.artifact.InvalidArtifactRTException;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.building.FileModelSource; import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingRequest;
@ -235,9 +235,9 @@ public void testReadInvalidPom()
{ {
projectBuilder.build( pomFile, configuration ); 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 // multi projects build entry point
@ -248,9 +248,9 @@ public void testReadInvalidPom()
catch ( ProjectBuildingException ex ) catch ( ProjectBuildingException ex )
{ {
assertEquals( 1, ex.getResults().size() ); assertEquals( 1, ex.getResults().size() );
MavenProject project = ex.getResults().get( 0 ).getProject(); assertNotNull( ex.getResults().get( 0 ).getPomFile() );
assertNotNull( project ); assertThat( ex.getResults().get( 0 ).getProblems().size(), greaterThan( 0 ) );
assertNotSame( 0, ex.getResults().get( 0 ).getProblems().size() ); assertThat( ex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) );
} }
} }

View File

@ -41,7 +41,9 @@
import org.apache.maven.xml.Factories; import org.apache.maven.xml.Factories;
import org.apache.maven.xml.sax.ext.CommentRenormalizer; import org.apache.maven.xml.sax.ext.CommentRenormalizer;
import org.apache.maven.xml.sax.filter.AbstractSAXFilter; import org.apache.maven.xml.sax.filter.AbstractSAXFilter;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/** /**
* Offers a transformation implementation based on PipelineStreams. * Offers a transformation implementation based on PipelineStreams.
@ -82,6 +84,31 @@ public final InputStream transform( Path pomFile, TransformerContext context )
{ {
filter = getSAXFilter( pomFile, context ); filter = getSAXFilter( pomFile, context );
filter.setLexicalHandler( transformerHandler ); 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 ) catch ( TransformerConfigurationException | SAXException | ParserConfigurationException e )
{ {

View File

@ -680,7 +680,7 @@ else if ( modelSource instanceof FileModelSource )
} }
catch ( IOException e ) catch ( IOException e )
{ {
problems.add( new ModelProblemCollectorRequest( Severity.WARNING, Version.V37 ).setException( e ) ); problems.add( new ModelProblemCollectorRequest( Severity.FATAL, Version.V37 ).setException( e ) );
} }
} }