o Distinguished different severity levels of model problems, allowing warnings to be collected but still have the build request succeed

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@790109 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-07-01 10:45:29 +00:00
parent 49acd30848
commit b91f837e12
4 changed files with 101 additions and 15 deletions

View File

@ -187,7 +187,7 @@ public class DefaultModelBuilder
ModelValidationResult validationResult = modelValidator.validateEffectiveModel( resultModel, request ); ModelValidationResult validationResult = modelValidator.validateEffectiveModel( resultModel, request );
addProblems( resultModel, validationResult, problems ); addProblems( resultModel, validationResult, problems );
if ( !problems.isEmpty() ) if ( hasErrors( problems ) )
{ {
throw new ModelBuildingException( problems ); throw new ModelBuildingException( problems );
} }
@ -196,6 +196,8 @@ public class DefaultModelBuilder
resultData.setArtifactId( resultModel.getArtifactId() ); resultData.setArtifactId( resultModel.getArtifactId() );
resultData.setVersion( resultModel.getVersion() ); resultData.setVersion( resultModel.getVersion() );
result.setProblems( problems );
result.setEffectiveModel( resultModel ); result.setEffectiveModel( resultModel );
result.setActiveExternalProfiles( activeExternalProfiles ); result.setActiveExternalProfiles( activeExternalProfiles );
@ -240,13 +242,13 @@ public class DefaultModelBuilder
catch ( ModelParseException e ) catch ( ModelParseException e )
{ {
problems.add( new ModelProblem( "Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage(), problems.add( new ModelProblem( "Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage(),
modelSource.getLocation(), e ) ); ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) );
throw new ModelBuildingException( problems ); throw new ModelBuildingException( problems );
} }
catch ( IOException e ) catch ( IOException e )
{ {
problems.add( new ModelProblem( "Non-readable POM " + modelSource.getLocation() + ": " + e.getMessage(), problems.add( new ModelProblem( "Non-readable POM " + modelSource.getLocation() + ": " + e.getMessage(),
modelSource.getLocation(), e ) ); ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) );
throw new ModelBuildingException( problems ); throw new ModelBuildingException( problems );
} }
@ -258,6 +260,22 @@ public class DefaultModelBuilder
return model; return model;
} }
private boolean hasErrors( List<ModelProblem> problems )
{
if ( problems != null )
{
for ( ModelProblem problem : problems )
{
if ( ModelProblem.Severity.ERROR.compareTo( problem.getSeverity() ) >= 0 )
{
return true;
}
}
}
return false;
}
private void addProblems( Model model, ModelValidationResult result, List<ModelProblem> problems ) private void addProblems( Model model, ModelValidationResult result, List<ModelProblem> problems )
{ {
if ( result.getMessageCount() > 0 ) if ( result.getMessageCount() > 0 )
@ -266,7 +284,8 @@ public class DefaultModelBuilder
for ( int i = 0; i < result.getMessageCount(); i++ ) for ( int i = 0; i < result.getMessageCount(); i++ )
{ {
problems.add( new ModelProblem( "Invalid POM " + source + ": " + result.getMessage( i ), source ) ); problems.add( new ModelProblem( "Invalid POM " + source + ": " + result.getMessage( i ),
ModelProblem.Severity.WARNING, source ) );
} }
} }
} }
@ -279,7 +298,7 @@ public class DefaultModelBuilder
for ( ProfileActivationException e : result.getActivationExceptions() ) for ( ProfileActivationException e : result.getActivationExceptions() )
{ {
problems.add( new ModelProblem( "Invalid activation condition for external profile " problems.add( new ModelProblem( "Invalid activation condition for external profile "
+ e.getProfile().getId() + ": " + e.getMessage(), "(external profiles)", e ) ); + e.getProfile().getId() + ": " + e.getMessage(), ModelProblem.Severity.ERROR, "(external profiles)", e ) );
} }
return result.getActiveProfiles(); return result.getActiveProfiles();
@ -294,7 +313,7 @@ public class DefaultModelBuilder
{ {
problems.add( new ModelProblem( "Invalid activation condition for project profile " problems.add( new ModelProblem( "Invalid activation condition for project profile "
+ e.getProfile().getId() + " in POM " + toSourceHint( model ) + ": " + e.getMessage(), + e.getProfile().getId() + " in POM " + toSourceHint( model ) + ": " + e.getMessage(),
toSourceHint( model ), e ) ); ModelProblem.Severity.ERROR, toSourceHint( model ), e ) );
} }
return result.getActiveProfiles(); return result.getActiveProfiles();
@ -319,7 +338,8 @@ public class DefaultModelBuilder
catch ( InvalidRepositoryException e ) catch ( InvalidRepositoryException e )
{ {
problems.add( new ModelProblem( "Invalid repository " + repository.getId() + " in POM " problems.add( new ModelProblem( "Invalid repository " + repository.getId() + " in POM "
+ toSourceHint( model ) + ": " + e.getMessage(), toSourceHint( model ), e ) ); + toSourceHint( model ) + ": " + e.getMessage(), ModelProblem.Severity.ERROR,
toSourceHint( model ), e ) );
} }
} }
} }
@ -345,7 +365,7 @@ public class DefaultModelBuilder
catch ( ModelInterpolationException e ) catch ( ModelInterpolationException e )
{ {
problems.add( new ModelProblem( "Invalid expression in POM " + toSourceHint( model ) + ": " problems.add( new ModelProblem( "Invalid expression in POM " + toSourceHint( model ) + ": "
+ e.getMessage(), toSourceHint( model ), e ) ); + e.getMessage(), ModelProblem.Severity.ERROR, toSourceHint( model ), e ) );
return model; return model;
} }
@ -438,7 +458,8 @@ public class DefaultModelBuilder
if ( modelResolver == null ) if ( modelResolver == null )
{ {
problems.add( new ModelProblem( "Non-resolvable parent POM " + toId( parent ) + " for POM " problems.add( new ModelProblem( "Non-resolvable parent POM " + toId( parent ) + " for POM "
+ toSourceHint( childModel ) + ": " + "No model resolver provided", toSourceHint( childModel ) ) ); + toSourceHint( childModel ) + ": " + "No model resolver provided", ModelProblem.Severity.FATAL,
toSourceHint( childModel ) ) );
throw new ModelBuildingException( problems ); throw new ModelBuildingException( problems );
} }
@ -450,7 +471,8 @@ public class DefaultModelBuilder
catch ( UnresolvableModelException e ) catch ( UnresolvableModelException e )
{ {
problems.add( new ModelProblem( "Non-resolvable parent POM " + toId( parent ) + " for POM " problems.add( new ModelProblem( "Non-resolvable parent POM " + toId( parent ) + " for POM "
+ toSourceHint( childModel ) + ": " + e.getMessage(), toSourceHint( childModel ), e ) ); + toSourceHint( childModel ) + ": " + e.getMessage(), ModelProblem.Severity.FATAL,
toSourceHint( childModel ), e ) );
throw new ModelBuildingException( problems ); throw new ModelBuildingException( problems );
} }

View File

@ -44,12 +44,15 @@ class DefaultModelBuildingResult
private List<Profile> activeExternalProfiles; private List<Profile> activeExternalProfiles;
private List<ModelProblem> problems;
public DefaultModelBuildingResult() public DefaultModelBuildingResult()
{ {
modelIds = new ArrayList<String>(); modelIds = new ArrayList<String>();
rawModels = new HashMap<String, Model>(); rawModels = new HashMap<String, Model>();
activePomProfiles = new HashMap<String, List<Profile>>(); activePomProfiles = new HashMap<String, List<Profile>>();
activeExternalProfiles = new ArrayList<Profile>(); activeExternalProfiles = new ArrayList<Profile>();
problems = new ArrayList<ModelProblem>();
} }
public Model getEffectiveModel() public Model getEffectiveModel()
@ -147,4 +150,23 @@ class DefaultModelBuildingResult
return this; return this;
} }
public List<ModelProblem> getProblems()
{
return Collections.unmodifiableList( problems );
}
public DefaultModelBuildingResult setProblems( List<ModelProblem> problems )
{
if ( problems != null )
{
this.problems = new ArrayList<ModelProblem>( problems );
}
else
{
this.problems.clear();
}
return this;
}
} }

View File

@ -84,4 +84,13 @@ public interface ModelBuildingResult
*/ */
List<Profile> getActiveExternalProfiles(); List<Profile> getActiveExternalProfiles();
/**
* Gets the problems that were encountered during the model building. Note that only problems of severity
* {@link ModelProblem.Severity#WARNING} and below are reported here. Problems with a higher severity level cause
* the model builder to fail with a {@link ModelBuildingException}.
*
* @return The problems that were encountered during the model building, can be empty but never {@code null}.
*/
List<ModelProblem> getProblems();
} }

View File

@ -21,29 +21,45 @@ package org.apache.maven.model;
/** /**
* Describes a problem that was encountered during model building. A problem can either be an exception that was thrown * Describes a problem that was encountered during model building. A problem can either be an exception that was thrown
* or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that could not * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that exhibits
* be processed. * the problem.
* *
* @author Benjamin Bentmann * @author Benjamin Bentmann
*/ */
public class ModelProblem public class ModelProblem
{ {
/**
* The different severity levels for a problem, in decreasing order.
*/
public enum Severity
{
FATAL, //
ERROR, //
WARNING, //
}
private String source; private String source;
private String message; private String message;
private Exception exception; private Exception exception;
private Severity severity;
/** /**
* Creates a new problem with the specified message. * Creates a new problem with the specified message.
* *
* @param message The message describing the problem, may be {@code null}. * @param message The message describing the problem, may be {@code null}.
* @param severity The severity level of the problem, may be {@code null} to default to {@link Severity#ERROR}.
* @param source A hint about the source of the problem, may be {@code null}. * @param source A hint about the source of the problem, may be {@code null}.
*/ */
public ModelProblem( String message, String source ) public ModelProblem( String message, Severity severity, String source )
{ {
this.message = message; this.message = message;
this.severity = ( severity != null ) ? severity : Severity.ERROR;
this.source = ( source != null ) ? source : ""; this.source = ( source != null ) ? source : "";
} }
@ -51,12 +67,14 @@ public class ModelProblem
* Creates a new problem with the specified message and exception. * Creates a new problem with the specified message and exception.
* *
* @param message The message describing the problem, may be {@code null}. * @param message The message describing the problem, may be {@code null}.
* @param severity The severity level of the problem, may be {@code null} to default to {@link Severity#ERROR}.
* @param source A hint about the source of the problem, may be {@code null}. * @param source A hint about the source of the problem, may be {@code null}.
* @param exception The exception that caused this problem, may be {@code null}. * @param exception The exception that caused this problem, may be {@code null}.
*/ */
public ModelProblem( String message, String source, Exception exception ) public ModelProblem( String message, Severity severity, String source, Exception exception )
{ {
this.message = message; this.message = message;
this.severity = ( severity != null ) ? severity : Severity.ERROR;
this.source = ( source != null ) ? source : ""; this.source = ( source != null ) ? source : "";
this.exception = exception; this.exception = exception;
} }
@ -109,10 +127,25 @@ public class ModelProblem
return msg; return msg;
} }
/**
* Gets the severity level of this problem.
*
* @return The severity level of this problem, never {@code null}.
*/
public Severity getSeverity()
{
return severity;
}
@Override @Override
public String toString() public String toString()
{ {
return getSource() + ": " + getMessage(); StringBuilder buffer = new StringBuilder( 128 );
buffer.append( "[" ).append( getSeverity() ).append( "] " );
buffer.append( getSource() ).append( ": " ).append( getMessage() );
return buffer.toString();
} }
} }