From 84dc78cbe4bd95697f2e8b19a3af2c55d2aa9706 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Wed, 5 Aug 2009 22:45:14 +0000 Subject: [PATCH] o Extended error reporting to provide the id of the POM that failed git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@801449 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/project/DefaultProjectBuilder.java | 7 ++-- .../project/DefaultProjectBuildingResult.java | 12 ++++++- .../maven/project/ProjectBuildingResult.java | 9 +++++ .../model/building/DefaultModelBuilder.java | 18 +++++----- .../DefaultModelProblemCollector.java | 27 +++++++++++++++ .../building/ModelBuildingException.java | 33 ++++++++++++++++--- .../model/building/ModelProblemUtils.java | 14 ++++++-- 7 files changed, 101 insertions(+), 19 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 54ec05bfa5..21e19d4ffd 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -113,7 +113,7 @@ public class DefaultProjectBuilder } catch ( ModelBuildingException e ) { - throw new ProjectBuildingException( "[unknown]", "Encountered POM errors", pomFile, e ); + throw new ProjectBuildingException( e.getModelId(), "Encountered POM errors", pomFile, e ); } Model model = result.getEffectiveModel(); @@ -327,7 +327,8 @@ public class DefaultProjectBuilder } catch ( ModelBuildingException e ) { - results.add( new DefaultProjectBuildingResult( interimResult.pomFile, e.getProblems() ) ); + results.add( new DefaultProjectBuildingResult( e.getModelId(), interimResult.pomFile, + e.getProblems() ) ); errors = true; } @@ -431,7 +432,7 @@ public class DefaultProjectBuilder } catch ( ModelBuildingException e ) { - results.add( new DefaultProjectBuildingResult( pomFile, e.getProblems() ) ); + results.add( new DefaultProjectBuildingResult( e.getModelId(), pomFile, e.getProblems() ) ); errors = true; } diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java index a590c91159..c3ca4e5225 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java @@ -34,6 +34,8 @@ class DefaultProjectBuildingResult implements ProjectBuildingResult { + private String projectId; + private File pomFile; private MavenProject project; @@ -48,6 +50,7 @@ class DefaultProjectBuildingResult */ public DefaultProjectBuildingResult( MavenProject project, List problems ) { + this.projectId = ( project != null ) ? project.getId() : ""; this.pomFile = ( project != null ) ? project.getFile() : null; this.project = project; this.problems = problems; @@ -56,15 +59,22 @@ class DefaultProjectBuildingResult /** * Creates a new result with the specified contents. * + * @param projectId The * @param pomFile The POM file from which the project was built, may be {@code null}. * @param problems The problems that were encouterned, may be {@code null}. */ - public DefaultProjectBuildingResult( File pomFile, List problems ) + public DefaultProjectBuildingResult( String projectId, File pomFile, List problems ) { + this.projectId = ( projectId != null ) ? projectId : ""; this.pomFile = pomFile; this.problems = problems; } + public String getProjectId() + { + return projectId; + } + public File getPomFile() { return pomFile; diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingResult.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingResult.java index e7c5521e86..9bdc75ec28 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingResult.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingResult.java @@ -32,6 +32,15 @@ import org.apache.maven.model.building.ModelProblem; public interface ProjectBuildingResult { + /** + * Gets the identifier of the project that could not be built. The general format of the identifier is {@code + * ::} but some of these coordinates may still be unknown at the point the exception + * is thrown so this information is merely meant to assist the user. + * + * @return The identifier of the project or an empty string if not known, never {@code null}. + */ + String getProjectId(); + /** * Gets the POM file from which the project was built. * 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 4a65e9f882..62b88f5251 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 @@ -121,6 +121,8 @@ public class DefaultModelBuilder Model inputModel = readModel( request.getModelSource(), request.getPomFile(), request, problems ); + problems.setRootModel( inputModel ); + ModelData resultData = new ModelData( inputModel ); List lineage = new ArrayList(); @@ -170,6 +172,7 @@ public class DefaultModelBuilder Model resultModel = resultData.getModel(); problems.setSourceHint( resultModel ); + problems.setRootModel( resultModel ); resultModel = interpolateModel( resultModel, request, problems ); resultData.setModel( resultModel ); @@ -208,6 +211,7 @@ public class DefaultModelBuilder DefaultModelProblemCollector problems = new DefaultModelProblemCollector( result.getProblems() ); problems.setSourceHint( resultModel ); + problems.setRootModel( resultModel ); modelPathTranslator.alignToBaseDirectory( resultModel, resultModel.getProjectDirectory(), request ); @@ -237,7 +241,7 @@ public class DefaultModelBuilder if ( hasErrors( problems.getProblems() ) ) { - throw new ModelBuildingException( problems.getProblems() ); + throw new ModelBuildingException( problems.getRootModelId(), problems.getProblems() ); } return result; @@ -274,13 +278,13 @@ public class DefaultModelBuilder { problems.add( new ModelProblem( "Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage(), ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) ); - throw new ModelBuildingException( problems.getProblems() ); + throw new ModelBuildingException( problems.getRootModelId(), problems.getProblems() ); } catch ( IOException e ) { problems.add( new ModelProblem( "Non-readable POM " + modelSource.getLocation() + ": " + e.getMessage(), ModelProblem.Severity.FATAL, modelSource.getLocation(), e ) ); - throw new ModelBuildingException( problems.getProblems() ); + throw new ModelBuildingException( problems.getRootModelId(), problems.getProblems() ); } model.setPomFile( pomFile ); @@ -517,11 +521,9 @@ public class DefaultModelBuilder } catch ( UnresolvableModelException e ) { - problems.add( new ModelProblem( "Non-resolvable parent POM " - + ModelProblemUtils.toId( groupId, artifactId, version ) + ": " + e.getMessage(), - ModelProblem.Severity.FATAL, ModelProblemUtils.toSourceHint( childModel ), - e ) ); - throw new ModelBuildingException( problems.getProblems() ); + problems.addFatalError( "Non-resolvable parent POM " + + ModelProblemUtils.toId( groupId, artifactId, version ) + ": " + e.getMessage(), e ); + throw new ModelBuildingException( problems.getRootModelId(), problems.getProblems() ); } Model parentModel = readModel( modelSource, null, request, problems ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java index bfade8de2a..0f74d9cfa3 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblemCollector.java @@ -43,6 +43,8 @@ class DefaultModelProblemCollector private Model sourceModel; + private Model rootModel; + public DefaultModelProblemCollector( List problems ) { this.problems = ( problems != null ) ? problems : new ArrayList(); @@ -74,11 +76,36 @@ class DefaultModelProblemCollector return sourceHint; } + public void setRootModel( Model rootModel ) + { + this.rootModel = rootModel; + } + + public Model getRootModel() + { + return rootModel; + } + + public String getRootModelId() + { + return ModelProblemUtils.toId( rootModel ); + } + public void add( ModelProblem problem ) { problems.add( problem ); } + public void addFatalError( String message ) + { + problems.add( new ModelProblem( message, ModelProblem.Severity.FATAL, getSourceHint() ) ); + } + + public void addFatalError( String message, Exception cause ) + { + problems.add( new ModelProblem( message, ModelProblem.Severity.FATAL, getSourceHint(), cause ) ); + } + public void addError( String message ) { problems.add( new ModelProblem( message, ModelProblem.Severity.ERROR, getSourceHint() ) ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java index e811d6fcc3..c05db4766e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingException.java @@ -35,16 +35,21 @@ public class ModelBuildingException extends Exception { + private final String modelId; + private final List problems; /** * Creates a new exception with the specified problems. * + * @param modelId The identifier of the model that could not be built, may be {@code null}. * @param problems The problems that causes this exception, may be {@code null}. */ - public ModelBuildingException( List problems ) + public ModelBuildingException( String modelId, List problems ) { - super( toMessage( problems ) ); + super( toMessage( modelId, problems ) ); + + this.modelId = ( modelId != null ) ? modelId : ""; this.problems = new ArrayList(); if ( problems != null ) @@ -53,6 +58,18 @@ public class ModelBuildingException } } + /** + * Gets the identifier of the POM whose effective model could not be built. The general format of the identifier is + * {@code ::} but some of these coordinates may still be unknown at the point the + * exception is thrown so this information is merely meant to assist the user. + * + * @return The identifier of the POM or an empty string if not known, never {@code null}. + */ + public String getModelId() + { + return modelId; + } + /** * Gets the problems that caused this exception. * @@ -60,10 +77,10 @@ public class ModelBuildingException */ public List getProblems() { - return this.problems; + return problems; } - private static String toMessage( List problems ) + private static String toMessage( String modelId, List problems ) { StringWriter buffer = new StringWriter( 1024 ); @@ -71,7 +88,13 @@ public class ModelBuildingException writer.print( problems.size() ); writer.print( ( problems.size() == 1 ) ? " problem was " : " problems were " ); - writer.println( "encountered during construction of the effective model:" ); + writer.print( "encountered while building the effective model" ); + if ( modelId != null && modelId.length() > 0 ) + { + writer.print( " for " ); + writer.print( modelId ); + } + writer.println(); for ( ModelProblem problem : problems ) { diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java index 20bcc64fe7..a60a16102c 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java @@ -34,11 +34,16 @@ class ModelProblemUtils /** * Creates a user-friendly source hint for the specified model. * - * @param model The model to create a source hint for, must not be {@code null}. + * @param model The model to create a source hint for, may be {@code null}. * @return The user-friendly source hint, never {@code null}. */ public static String toSourceHint( Model model ) { + if ( model == null ) + { + return ""; + } + StringBuilder buffer = new StringBuilder( 192 ); buffer.append( toId( model ) ); @@ -52,8 +57,13 @@ class ModelProblemUtils return buffer.toString(); } - private static String toId( Model model ) + public static String toId( Model model ) { + if ( model == null ) + { + return ""; + } + String groupId = model.getGroupId(); if ( groupId == null && model.getParent() != null ) {