001package org.apache.maven.lifecycle;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.plugin.MojoExecution;
023import org.apache.maven.project.MavenProject;
024
025/**
026 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
027 */
028public class LifecycleExecutionException
029    extends Exception
030{
031    private MavenProject project;
032
033    public LifecycleExecutionException( String message )
034    {
035        super( message );
036    }
037
038    public LifecycleExecutionException( Throwable cause )
039    {
040        super( cause );
041    }
042
043    public LifecycleExecutionException( String message, Throwable cause )
044    {
045        super( message, cause );
046    }
047
048    public LifecycleExecutionException( String message, MavenProject project )
049    {
050        super( message );
051        this.project = project;
052    }
053
054    public LifecycleExecutionException( String message, MojoExecution execution, MavenProject project )
055    {
056        super( message );
057        this.project = project;
058    }
059
060    public LifecycleExecutionException( String message, MojoExecution execution, MavenProject project, Throwable cause )
061    {
062        super( message, cause );
063        this.project = project;
064    }
065
066    public LifecycleExecutionException( MojoExecution execution, MavenProject project, Throwable cause )
067    {
068        this( createMessage( execution, project, cause ), execution, project, cause );
069    }
070
071    public MavenProject getProject()
072    {
073        return project;
074    }
075
076    private static String createMessage( MojoExecution execution, MavenProject project, Throwable cause )
077    {
078        StringBuilder buffer = new StringBuilder( 256 );
079
080        buffer.append( "Failed to execute goal" );
081
082        if ( execution != null )
083        {
084            buffer.append( ' ' );
085            buffer.append( execution.getGroupId() );
086            buffer.append( ':' );
087            buffer.append( execution.getArtifactId() );
088            buffer.append( ':' );
089            buffer.append( execution.getVersion() );
090            buffer.append( ':' );
091            buffer.append( execution.getGoal() );
092            buffer.append( " (" );
093            buffer.append( execution.getExecutionId() );
094            buffer.append( ")" );
095        }
096
097        if ( project != null )
098        {
099            buffer.append( " on project " );
100            buffer.append( project.getArtifactId() );
101        }
102
103        if ( cause != null )
104        {
105            buffer.append( ": " ).append( cause.getMessage() );
106        }
107
108        return buffer.toString();
109    }
110
111}