1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
46
47
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
57
58
59
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
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 }