View Javadoc

1   package org.apache.maven.plugin;
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 org.codehaus.plexus.compiler.CompilerError;
23  
24  import java.util.List;
25  
26  /**
27   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
28   * @version $Id: CompilationFailureException.html 816619 2012-05-08 13:09:26Z hboutemy $
29   * @since 2.0
30   */
31  @SuppressWarnings( "serial" )
32  public class CompilationFailureException
33      extends MojoFailureException
34  {
35      private static final String LS = System.getProperty( "line.separator" );
36  
37      public CompilationFailureException( List<CompilerError> messages )
38      {
39          super( null, shortMessage( messages ), longMessage( messages ) );
40      }
41  
42      public static String longMessage( List<CompilerError> messages )
43      {
44          StringBuilder sb = new StringBuilder();
45  
46          if ( messages != null )
47          {
48              for ( CompilerError compilerError : messages )
49              {
50                  sb.append( compilerError ).append( LS );
51              }
52          }
53          return sb.toString();
54      }
55  
56      /**
57       * Short message will have the error message if there's only one, useful for errors forking the compiler
58       *
59       * @param messages
60       * @return the short error message
61       * @since 2.0.2
62       */
63      public static String shortMessage( List<CompilerError> messages )
64      {
65          StringBuffer sb = new StringBuffer();
66  
67          sb.append( "Compilation failure" );
68  
69          if ( messages.size() == 1 )
70          {
71              sb.append( LS );
72  
73              CompilerError compilerError = (CompilerError) messages.get( 0 );
74  
75              sb.append( compilerError ).append( LS );
76          }
77          
78          return sb.toString();
79      }
80  }