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