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 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 }