001package org.apache.maven.plugin;
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.project.DuplicateArtifactAttachmentException;
023import org.apache.maven.project.MavenProject;
024import org.codehaus.plexus.util.StringUtils;
025
026public class PluginExecutionException
027    extends PluginManagerException
028{
029
030    private final MojoExecution mojoExecution;
031
032    public PluginExecutionException( MojoExecution mojoExecution, MavenProject project, String message )
033    {
034        super( mojoExecution.getMojoDescriptor(), project, message );
035        this.mojoExecution = mojoExecution;
036    }
037
038    public PluginExecutionException( MojoExecution mojoExecution, MavenProject project, String message,
039                                     Throwable cause )
040    {
041        super( mojoExecution.getMojoDescriptor(), project, message, cause );
042        this.mojoExecution = mojoExecution;
043    }
044
045    public PluginExecutionException( MojoExecution mojoExecution, MavenProject project, Exception cause )
046    {
047        super( mojoExecution.getMojoDescriptor(), project, constructMessage( mojoExecution, cause ), cause );
048        this.mojoExecution = mojoExecution;
049    }
050
051    public PluginExecutionException( MojoExecution mojoExecution, MavenProject project,
052                                     DuplicateArtifactAttachmentException cause )
053    {
054        super( mojoExecution.getMojoDescriptor(), project, constructMessage( mojoExecution, cause ), cause );
055        this.mojoExecution = mojoExecution;
056    }
057
058    public MojoExecution getMojoExecution()
059    {
060        return mojoExecution;
061    }
062
063    private static String constructMessage( MojoExecution mojoExecution, Throwable cause )
064    {
065        String message;
066
067        if ( mojoExecution != null )
068        {
069            message =
070                "Execution " + mojoExecution.getExecutionId() + " of goal " + mojoExecution.getMojoDescriptor().getId()
071                    + " failed";
072        }
073        else
074        {
075            message = "Mojo execution failed";
076        }
077
078        if ( cause != null && StringUtils.isNotEmpty( cause.getMessage() ) )
079        {
080            message += ": " + cause.getMessage();
081        }
082        else
083        {
084            message += ".";
085        }
086
087        return message;
088    }
089
090}