1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.model.building.ModelProblem;
23 import org.apache.maven.model.building.ModelProblemUtils;
24
25 import java.io.File;
26 import java.io.PrintWriter;
27 import java.io.StringWriter;
28 import java.util.List;
29
30
31
32
33 public class ProjectBuildingException
34 extends Exception
35 {
36 private final String projectId;
37
38 private File pomFile;
39
40 private List<ProjectBuildingResult> results;
41
42 public ProjectBuildingException( String projectId, String message, Throwable cause )
43 {
44 super( createMessage( message, projectId, null ), cause );
45 this.projectId = projectId;
46 }
47
48
49
50
51
52
53 public ProjectBuildingException( String projectId, String message, File pomFile )
54 {
55 super( createMessage( message, projectId, pomFile ) );
56 this.projectId = projectId;
57 this.pomFile = pomFile;
58 }
59
60
61
62
63
64
65
66 protected ProjectBuildingException( String projectId, String message, File pomFile, Throwable cause )
67 {
68 super( createMessage( message, projectId, pomFile ), cause );
69 this.projectId = projectId;
70 this.pomFile = pomFile;
71 }
72
73 public ProjectBuildingException( List<ProjectBuildingResult> results )
74 {
75 super( createMessage( results ) );
76 this.projectId = "";
77 this.results = results;
78 }
79
80 public File getPomFile()
81 {
82 return pomFile;
83 }
84
85
86
87
88 @Deprecated
89 public String getPomLocation()
90 {
91 if ( getPomFile() != null )
92 {
93 return getPomFile().getAbsolutePath();
94 }
95 else
96 {
97 return "null";
98 }
99 }
100
101 public String getProjectId()
102 {
103 return projectId;
104 }
105
106 public List<ProjectBuildingResult> getResults()
107 {
108 return results;
109 }
110
111 private static String createMessage( String message, String projectId, File pomFile )
112 {
113 StringBuilder buffer = new StringBuilder( 256 );
114 buffer.append( message );
115 buffer.append( " for project " ).append( projectId );
116 if ( pomFile != null )
117 {
118 buffer.append( " at " ).append( pomFile.getAbsolutePath() );
119 }
120 return buffer.toString();
121 }
122
123 private static String createMessage( List<ProjectBuildingResult> results )
124 {
125 StringWriter buffer = new StringWriter( 1024 );
126 PrintWriter writer = new PrintWriter( buffer );
127 writer.println( "Some problems were encountered while processing the POMs:" );
128 try
129 {
130
131 for ( ProjectBuildingResult result : results )
132 {
133 for ( ModelProblem problem : result.getProblems() )
134 {
135 writer.print( "[" );
136 writer.print( problem.getSeverity() );
137 writer.print( "] " );
138 writer.print( problem.getMessage() );
139 writer.print( " @ " );
140 writer.println( ModelProblemUtils.formatLocation( problem, result.getProjectId() ) );
141 }
142 }
143 }
144 finally
145 {
146 writer.close();
147 }
148 return buffer.toString();
149 }
150
151 }