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   */
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       * @param projectId
50       * @param message
51       * @param pomFile   pom file location
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       * @param projectId
62       * @param message
63       * @param pomFile   pom file location
64       * @param cause
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       * @deprecated use {@link #getPomFile()}
87       */
88      public String getPomLocation()
89      {
90          if ( getPomFile() != null )
91          {
92              return getPomFile().getAbsolutePath();
93          }
94          else
95          {
96              return "null";
97          }
98      }
99  
100     public String getProjectId()
101     {
102         return projectId;
103     }
104 
105     public List<ProjectBuildingResult> getResults()
106     {
107         return results;
108     }
109 
110     private static String createMessage( String message, String projectId, File pomFile )
111     {
112         StringBuilder buffer = new StringBuilder( 256 );
113         buffer.append( message );
114         buffer.append( " for project " ).append( projectId );
115         if ( pomFile != null )
116         {
117             buffer.append( " at " ).append( pomFile.getAbsolutePath() );
118         }
119         return buffer.toString();
120     }
121 
122     private static String createMessage( List<ProjectBuildingResult> results )
123     {
124         StringWriter buffer = new StringWriter( 1024 );
125 
126         PrintWriter writer = new PrintWriter( buffer );
127         writer.println( "Some problems were encountered while processing the POMs:" );
128         for ( ProjectBuildingResult result : results )
129         {
130             for ( ModelProblem problem : result.getProblems() )
131             {
132                 writer.print( "[" );
133                 writer.print( problem.getSeverity() );
134                 writer.print( "] " );
135                 writer.print( problem.getMessage() );
136                 writer.print( " @ " );
137                 writer.println( ModelProblemUtils.formatLocation( problem, result.getProjectId() ) );
138             }
139         }
140         writer.close();
141 
142         return buffer.toString();
143     }
144 
145 }