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