1 package org.apache.maven.model.building; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.io.PrintWriter; 23 import java.io.StringWriter; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import org.apache.maven.model.Model; 28 29 /** 30 * Signals one ore more errors during model building. The model builder tries to collect as many problems as possible 31 * before eventually failing to provide callers with rich error information. Use {@link #getProblems()} to query the 32 * details of the failure. 33 * 34 * @author Benjamin Bentmann 35 */ 36 public class ModelBuildingException 37 extends Exception 38 { 39 40 private final Model model; 41 42 private final String modelId; 43 44 private final List<ModelProblem> problems; 45 46 /** 47 * Creates a new exception with the specified problems. 48 * 49 * @param model The model that could not be built, may be {@code null}. 50 * @param modelId The identifier of the model that could not be built, may be {@code null}. 51 * @param problems The problems that causes this exception, may be {@code null}. 52 */ 53 public ModelBuildingException( Model model, String modelId, List<ModelProblem> problems ) 54 { 55 super( toMessage( modelId, problems ) ); 56 57 this.model = model; 58 this.modelId = ( modelId != null ) ? modelId : ""; 59 60 this.problems = new ArrayList<ModelProblem>(); 61 if ( problems != null ) 62 { 63 this.problems.addAll( problems ); 64 } 65 } 66 67 /** 68 * Gets the model that could not be built properly. 69 * 70 * @return The erroneous model or {@code null} if not available. 71 */ 72 public Model getModel() 73 { 74 return model; 75 } 76 77 /** 78 * Gets the identifier of the POM whose effective model could not be built. The general format of the identifier is 79 * {@code <groupId>:<artifactId>:<version>} but some of these coordinates may still be unknown at the point the 80 * exception is thrown so this information is merely meant to assist the user. 81 * 82 * @return The identifier of the POM or an empty string if not known, never {@code null}. 83 */ 84 public String getModelId() 85 { 86 return modelId; 87 } 88 89 /** 90 * Gets the problems that caused this exception. 91 * 92 * @return The problems that caused this exception, never {@code null}. 93 */ 94 public List<ModelProblem> getProblems() 95 { 96 return problems; 97 } 98 99 private static String toMessage( String modelId, List<ModelProblem> problems ) 100 { 101 StringWriter buffer = new StringWriter( 1024 ); 102 103 PrintWriter writer = new PrintWriter( buffer ); 104 105 writer.print( problems.size() ); 106 writer.print( ( problems.size() == 1 ) ? " problem was " : " problems were " ); 107 writer.print( "encountered while building the effective model" ); 108 if ( modelId != null && modelId.length() > 0 ) 109 { 110 writer.print( " for " ); 111 writer.print( modelId ); 112 } 113 writer.println(); 114 115 for ( ModelProblem problem : problems ) 116 { 117 writer.print( "[" ); 118 writer.print( problem.getSeverity() ); 119 writer.print( "] " ); 120 writer.print( problem.getMessage() ); 121 writer.print( " @ " ); 122 writer.println( ModelProblemUtils.formatLocation( problem, modelId ) ); 123 } 124 125 return buffer.toString(); 126 } 127 128 }