From b91f837e1267bf8fa66ac24eba9694687d91b2bd Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Wed, 1 Jul 2009 10:45:29 +0000 Subject: [PATCH] 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 --- .../maven/model/DefaultModelBuilder.java | 42 +++++++++++++----- .../model/DefaultModelBuildingResult.java | 22 ++++++++++ .../maven/model/ModelBuildingResult.java | 9 ++++ .../org/apache/maven/model/ModelProblem.java | 43 ++++++++++++++++--- 4 files changed, 101 insertions(+), 15 deletions(-) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java index 4bc8c5bfaf..63dbde462e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java @@ -187,7 +187,7 @@ public class DefaultModelBuilder ModelValidationResult validationResult = modelValidator.validateEffectiveModel( resultModel, request ); addProblems( resultModel, validationResult, problems ); - if ( !problems.isEmpty() ) + if ( hasErrors( problems ) ) { throw new ModelBuildingException( problems ); } @@ -196,6 +196,8 @@ public class DefaultModelBuilder resultData.setArtifactId( resultModel.getArtifactId() ); resultData.setVersion( resultModel.getVersion() ); + result.setProblems( problems ); + result.setEffectiveModel( resultModel ); result.setActiveExternalProfiles( activeExternalProfiles ); @@ -240,13 +242,13 @@ public class DefaultModelBuilder catch ( ModelParseException e ) { problems.add( new ModelProblem( "Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage(), - modelSource.getLocation(), e ) ); + ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) ); throw new ModelBuildingException( problems ); } catch ( IOException e ) { problems.add( new ModelProblem( "Non-readable POM " + modelSource.getLocation() + ": " + e.getMessage(), - modelSource.getLocation(), e ) ); + ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) ); throw new ModelBuildingException( problems ); } @@ -258,6 +260,22 @@ public class DefaultModelBuilder return model; } + private boolean hasErrors( List 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 problems ) { if ( result.getMessageCount() > 0 ) @@ -266,7 +284,8 @@ public class DefaultModelBuilder 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() ) { 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(); @@ -294,7 +313,7 @@ public class DefaultModelBuilder { problems.add( new ModelProblem( "Invalid activation condition for project profile " + e.getProfile().getId() + " in POM " + toSourceHint( model ) + ": " + e.getMessage(), - toSourceHint( model ), e ) ); + ModelProblem.Severity.ERROR, toSourceHint( model ), e ) ); } return result.getActiveProfiles(); @@ -319,7 +338,8 @@ public class DefaultModelBuilder catch ( InvalidRepositoryException e ) { 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 ) { 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; } @@ -438,7 +458,8 @@ public class DefaultModelBuilder if ( modelResolver == null ) { 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 ); } @@ -450,7 +471,8 @@ public class DefaultModelBuilder catch ( UnresolvableModelException e ) { 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 ); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java index 99c2d11475..28d55b9682 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java @@ -44,12 +44,15 @@ class DefaultModelBuildingResult private List activeExternalProfiles; + private List problems; + public DefaultModelBuildingResult() { modelIds = new ArrayList(); rawModels = new HashMap(); activePomProfiles = new HashMap>(); activeExternalProfiles = new ArrayList(); + problems = new ArrayList(); } public Model getEffectiveModel() @@ -147,4 +150,23 @@ class DefaultModelBuildingResult return this; } + public List getProblems() + { + return Collections.unmodifiableList( problems ); + } + + public DefaultModelBuildingResult setProblems( List problems ) + { + if ( problems != null ) + { + this.problems = new ArrayList( problems ); + } + else + { + this.problems.clear(); + } + + return this; + } + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java index 5d47b5ff6e..67c42fbf7c 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java @@ -84,4 +84,13 @@ public interface ModelBuildingResult */ List 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 getProblems(); + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/ModelProblem.java b/maven-model-builder/src/main/java/org/apache/maven/model/ModelProblem.java index d2b3629d46..f12a330f2a 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/ModelProblem.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/ModelProblem.java @@ -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 - * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that could not - * be processed. + * or a simple string message. In addition, a problem carries a hint about its source, e.g. the POM file that exhibits + * the problem. * * @author Benjamin Bentmann */ public class ModelProblem { + /** + * The different severity levels for a problem, in decreasing order. + */ + public enum Severity + { + + FATAL, // + ERROR, // + WARNING, // + + } + private String source; private String message; private Exception exception; + private Severity severity; + /** * Creates a new problem with the specified message. * * @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}. */ - public ModelProblem( String message, String source ) + public ModelProblem( String message, Severity severity, String source ) { this.message = message; + this.severity = ( severity != null ) ? severity : Severity.ERROR; this.source = ( source != null ) ? source : ""; } @@ -51,12 +67,14 @@ public class ModelProblem * Creates a new problem with the specified message and exception. * * @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 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.severity = ( severity != null ) ? severity : Severity.ERROR; this.source = ( source != null ) ? source : ""; this.exception = exception; } @@ -109,10 +127,25 @@ public class ModelProblem 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 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(); } }