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.io.PrintWriter;
23  import java.io.StringWriter;
24  import java.util.List;
25  
26  import org.apache.maven.model.building.ModelProblem;
27  import org.apache.maven.model.building.ModelProblemUtils;
28  
29  /**
30   * @author Jason van Zyl
31   */
32  public class ProjectBuildingException extends Exception {
33      private final String projectId;
34  
35      private File pomFile;
36  
37      private List<ProjectBuildingResult> results;
38  
39      public ProjectBuildingException(String projectId, String message, Throwable cause) {
40          super(createMessage(message, projectId, null), cause);
41          this.projectId = projectId;
42      }
43  
44      /**
45       * @param projectId
46       * @param message
47       * @param pomFile   pom file location
48       */
49      public ProjectBuildingException(String projectId, String message, File pomFile) {
50          super(createMessage(message, projectId, pomFile));
51          this.projectId = projectId;
52          this.pomFile = pomFile;
53      }
54  
55      /**
56       * @param projectId
57       * @param message
58       * @param pomFile   pom file location
59       * @param cause
60       */
61      protected ProjectBuildingException(String projectId, String message, File pomFile, Throwable cause) {
62          super(createMessage(message, projectId, pomFile), cause);
63          this.projectId = projectId;
64          this.pomFile = pomFile;
65      }
66  
67      public ProjectBuildingException(List<ProjectBuildingResult> results) {
68          super(createMessage(results));
69          this.projectId = "";
70          this.results = results;
71      }
72  
73      public File getPomFile() {
74          return pomFile;
75      }
76  
77      /**
78       * @deprecated use {@link #getPomFile()}
79       */
80      public String getPomLocation() {
81          if (getPomFile() != null) {
82              return getPomFile().getAbsolutePath();
83          } else {
84              return "null";
85          }
86      }
87  
88      public String getProjectId() {
89          return projectId;
90      }
91  
92      public List<ProjectBuildingResult> getResults() {
93          return results;
94      }
95  
96      private static String createMessage(String message, String projectId, File pomFile) {
97          StringBuilder buffer = new StringBuilder(256);
98          buffer.append(message);
99          buffer.append(" for project ").append(projectId);
100         if (pomFile != null) {
101             buffer.append(" at ").append(pomFile.getAbsolutePath());
102         }
103         return buffer.toString();
104     }
105 
106     private static String createMessage(List<ProjectBuildingResult> results) {
107         StringWriter buffer = new StringWriter(1024);
108         PrintWriter writer = new PrintWriter(buffer);
109         writer.println("Some problems were encountered while processing the POMs:");
110         try {
111 
112             for (ProjectBuildingResult result : results) {
113                 for (ModelProblem problem : result.getProblems()) {
114                     writer.print("[");
115                     writer.print(problem.getSeverity());
116                     writer.print("] ");
117                     writer.print(problem.getMessage());
118                     writer.print(" @ ");
119                     writer.println(ModelProblemUtils.formatLocation(problem, result.getProjectId()));
120                 }
121             }
122         } finally {
123             writer.close();
124         }
125         return buffer.toString();
126     }
127 }