View Javadoc

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