View Javadoc
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.project;
20  
21  import java.io.File;
22  import java.util.List;
23  
24  import org.apache.maven.model.building.ModelProblem;
25  
26  /**
27   */
28  public class ProjectBuildingException extends Exception {
29      private final String projectId;
30  
31      private File pomFile;
32  
33      private List<ProjectBuildingResult> results;
34  
35      public ProjectBuildingException(String projectId, String message, Throwable cause) {
36          super(createMessage(message, projectId, null), cause);
37          this.projectId = projectId;
38      }
39  
40      /**
41       * @param projectId
42       * @param message
43       * @param pomFile   pom file location
44       */
45      public ProjectBuildingException(String projectId, String message, File pomFile) {
46          super(createMessage(message, projectId, pomFile));
47          this.projectId = projectId;
48          this.pomFile = pomFile;
49      }
50  
51      /**
52       * @param projectId
53       * @param message
54       * @param pomFile   pom file location
55       * @param cause
56       */
57      protected ProjectBuildingException(String projectId, String message, File pomFile, Throwable cause) {
58          super(createMessage(message, projectId, pomFile), cause);
59          this.projectId = projectId;
60          this.pomFile = pomFile;
61      }
62  
63      public ProjectBuildingException(List<ProjectBuildingResult> results) {
64          super(createMessage(results));
65          this.projectId = "";
66          this.results = results;
67      }
68  
69      public File getPomFile() {
70          return pomFile;
71      }
72  
73      /**
74       * @deprecated use {@link #getPomFile()}
75       */
76      @Deprecated
77      public String getPomLocation() {
78          if (getPomFile() != null) {
79              return getPomFile().getAbsolutePath();
80          } else {
81              return "null";
82          }
83      }
84  
85      public String getProjectId() {
86          return projectId;
87      }
88  
89      public List<ProjectBuildingResult> getResults() {
90          return results;
91      }
92  
93      private static String createMessage(String message, String projectId, File pomFile) {
94          StringBuilder buffer = new StringBuilder(256);
95          buffer.append(message);
96          buffer.append(" for project ").append(projectId);
97          if (pomFile != null) {
98              buffer.append(" at ").append(pomFile.getAbsolutePath());
99          }
100         return buffer.toString();
101     }
102 
103     private static String createMessage(List<ProjectBuildingResult> results) {
104         if (results == null || results.isEmpty()) {
105             return "Some problems were encountered while processing the POMs";
106         }
107 
108         long totalProblems = 0;
109         long errorProblems = 0;
110 
111         for (ProjectBuildingResult result : results) {
112             List<ModelProblem> problems = result.getProblems();
113             totalProblems += problems.size();
114 
115             for (ModelProblem problem : problems) {
116                 if (problem.getSeverity() != ModelProblem.Severity.WARNING) {
117                     errorProblems++;
118                 }
119             }
120         }
121 
122         StringBuilder buffer = new StringBuilder(1024);
123         buffer.append(totalProblems);
124         buffer.append(totalProblems == 1 ? " problem was " : " problems were ");
125         buffer.append("encountered while processing the POMs");
126 
127         if (errorProblems > 0) {
128             buffer.append(" (")
129                     .append(errorProblems)
130                     .append(" ")
131                     .append(errorProblems > 1 ? "errors" : "error")
132                     .append(")");
133         }
134 
135         buffer.append(":\n");
136 
137         for (ProjectBuildingResult result : results) {
138             if (!result.getProblems().isEmpty()) {
139                 String projectInfo = result.getProjectId();
140                 if (projectInfo.trim().isEmpty()) {
141                     projectInfo =
142                             result.getPomFile() != null ? result.getPomFile().getName() : "unknown project";
143                 }
144 
145                 buffer.append("\n[").append(projectInfo).append("]\n");
146 
147                 for (ModelProblem problem : result.getProblems()) {
148                     if (errorProblems > 0 && problem.getSeverity() == ModelProblem.Severity.WARNING) {
149                         continue;
150                     }
151 
152                     buffer.append("  [").append(problem.getSeverity()).append("] ");
153                     buffer.append(problem.getMessage());
154 
155                     String location = "";
156                     if (!problem.getSource().trim().isEmpty()) {
157                         location = problem.getSource();
158                     }
159                     if (problem.getLineNumber() > 0) {
160                         if (!location.isEmpty()) {
161                             location += ", ";
162                         }
163                         location += "line " + problem.getLineNumber();
164                     }
165                     if (problem.getColumnNumber() > 0) {
166                         if (!location.isEmpty()) {
167                             location += ", ";
168                         }
169                         location += "column " + problem.getColumnNumber();
170                     }
171 
172                     if (!location.isEmpty()) {
173                         buffer.append(" @ ").append(location);
174                     }
175                     buffer.append("\n");
176                 }
177             }
178         }
179 
180         return buffer.toString();
181     }
182 }