View Javadoc

1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  import java.util.List;
26  
27  import org.apache.maven.model.building.ModelProblem;
28  import org.apache.maven.model.building.ModelProblemUtils;
29  
30  /**
31   * @author Jason van Zyl
32   * @version $Id: ProjectBuildingException.java 934861 2010-04-16 13:18:48Z bentmann $
33   */
34  public class ProjectBuildingException
35      extends Exception
36  {
37      private final String projectId;
38  
39      private File pomFile;
40  
41      private List<ProjectBuildingResult> results;
42  
43      public ProjectBuildingException( String projectId, String message, Throwable cause )
44      {
45          super( createMessage( message, projectId, null ), cause );
46          this.projectId = projectId;
47      }
48  
49      /**
50       * @param projectId
51       * @param message
52       * @param pomFile   pom file location
53       */
54      public ProjectBuildingException( String projectId, String message, File pomFile )
55      {
56          super( createMessage( message, projectId, pomFile ) );
57          this.projectId = projectId;
58          this.pomFile = pomFile;
59      }
60  
61      /**
62       * @param projectId
63       * @param message
64       * @param pomFile   pom file location
65       * @param cause
66       */
67      protected ProjectBuildingException( String projectId, String message, File pomFile, Throwable cause )
68      {
69          super( createMessage( message, projectId, pomFile ), cause );
70          this.projectId = projectId;
71          this.pomFile = pomFile;
72      }
73  
74      public ProjectBuildingException( List<ProjectBuildingResult> results )
75      {
76          super( createMessage( results ) );
77          this.projectId = "";
78          this.results = results;
79      }
80  
81      public File getPomFile()
82      {
83          return pomFile;
84      }
85  
86      /**
87       * @deprecated use {@link #getPomFile()}
88       */
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 
127         PrintWriter writer = new PrintWriter( buffer );
128         writer.println( "Some problems were encountered while processing the POMs:" );
129         for ( ProjectBuildingResult result : results )
130         {
131             for ( ModelProblem problem : result.getProblems() )
132             {
133                 writer.print( "[" );
134                 writer.print( problem.getSeverity() );
135                 writer.print( "] " );
136                 writer.print( problem.getMessage() );
137                 writer.print( " @ " );
138                 writer.println( ModelProblemUtils.formatLocation( problem, result.getProjectId() ) );
139             }
140         }
141         writer.close();
142 
143         return buffer.toString();
144     }
145 
146 }