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      @Deprecated
81      public String getPomLocation() {
82          if (getPomFile() != null) {
83              return getPomFile().getAbsolutePath();
84          } else {
85              return "null";
86          }
87      }
88  
89      public String getProjectId() {
90          return projectId;
91      }
92  
93      public List<ProjectBuildingResult> getResults() {
94          return results;
95      }
96  
97      private static String createMessage(String message, String projectId, File pomFile) {
98          StringBuilder buffer = new StringBuilder(256);
99          buffer.append(message);
100         buffer.append(" for project ").append(projectId);
101         if (pomFile != null) {
102             buffer.append(" at ").append(pomFile.getAbsolutePath());
103         }
104         return buffer.toString();
105     }
106 
107     private static String createMessage(List<ProjectBuildingResult> results) {
108         StringWriter buffer = new StringWriter(1024);
109         PrintWriter writer = new PrintWriter(buffer);
110         writer.println("Some problems were encountered while processing the POMs:");
111         try {
112 
113             for (ProjectBuildingResult result : results) {
114                 for (ModelProblem problem : result.getProblems()) {
115                     writer.print("[");
116                     writer.print(problem.getSeverity());
117                     writer.print("] ");
118                     writer.print(problem.getMessage());
119                     writer.print(" @ ");
120                     writer.println(ModelProblemUtils.formatLocation(problem, result.getProjectId()));
121                 }
122             }
123         } finally {
124             writer.close();
125         }
126         return buffer.toString();
127     }
128 }