View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle;
20  
21  import org.apache.maven.plugin.MojoExecution;
22  import org.apache.maven.project.MavenProject;
23  import org.apache.maven.shared.utils.logging.MessageBuilder;
24  
25  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
26  
27  /**
28   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
29   */
30  public class LifecycleExecutionException extends Exception {
31      private MavenProject project;
32  
33      public LifecycleExecutionException(String message) {
34          super(message);
35      }
36  
37      public LifecycleExecutionException(Throwable cause) {
38          super(cause);
39      }
40  
41      public LifecycleExecutionException(String message, Throwable cause) {
42          super(message, cause);
43      }
44  
45      public LifecycleExecutionException(String message, MavenProject project) {
46          super(message);
47          this.project = project;
48      }
49  
50      public LifecycleExecutionException(String message, MojoExecution execution, MavenProject project) {
51          super(message);
52          this.project = project;
53      }
54  
55      public LifecycleExecutionException(String message, MojoExecution execution, MavenProject project, Throwable cause) {
56          super(message, cause);
57          this.project = project;
58      }
59  
60      public LifecycleExecutionException(MojoExecution execution, MavenProject project, Throwable cause) {
61          this(createMessage(execution, project, cause), execution, project, cause);
62      }
63  
64      public MavenProject getProject() {
65          return project;
66      }
67  
68      private static String createMessage(MojoExecution execution, MavenProject project, Throwable cause) {
69          MessageBuilder buffer = buffer(256);
70  
71          buffer.a("Failed to execute goal");
72  
73          if (execution != null) {
74              buffer.a(' ');
75              buffer.mojo(execution.getGroupId()
76                      + ':'
77                      + execution.getArtifactId()
78                      + ':'
79                      + execution.getVersion()
80                      + ':'
81                      + execution.getGoal());
82              buffer.a(' ').strong('(' + execution.getExecutionId() + ')');
83          }
84  
85          if (project != null) {
86              buffer.a(" on project ");
87              buffer.project(project.getArtifactId());
88          }
89  
90          if (cause != null) {
91              buffer.a(": ").failure(cause.getMessage());
92          }
93  
94          return buffer.toString();
95      }
96  }