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 * @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 /** 38 * Wrap error messages from the compiler 39 * 40 * @param messages the messages, not null 41 * @since 2.0 42 */ 43 public CompilationFailureException( List<CompilerMessage> messages ) 44 { 45 super( null, shortMessage( messages ), longMessage( messages ) ); 46 } 47 48 /** 49 * Long message will have all messages, one per line 50 * 51 * @param messages the messages, not null 52 * @return the long error message 53 * @since 2.0 54 */ 55 public static String longMessage( List<CompilerMessage> messages ) 56 { 57 StringBuilder sb = new StringBuilder(); 58 59 for ( CompilerMessage compilerError : messages ) 60 { 61 sb.append( compilerError ).append( LS ); 62 } 63 64 return sb.toString(); 65 } 66 67 /** 68 * Short message will have the error message if there's only one, useful for errors forking the compiler 69 * 70 * @param messages the messages, not null 71 * @return the short error message 72 * @since 2.0.2 73 */ 74 public static String shortMessage( List<CompilerMessage> messages ) 75 { 76 StringBuilder sb = new StringBuilder( "Compilation failure" ); 77 78 if ( messages.size() == 1 ) 79 { 80 sb.append( LS ).append( messages.get( 0 ) ).append( LS ); 81 } 82 83 return sb.toString(); 84 } 85 }