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.message.MessageBuilder;
22  import org.apache.maven.message.MessageBuilderFactory;
23  import org.apache.maven.message.internal.DefaultMessageBuilderFactory;
24  import org.apache.maven.plugin.MojoExecution;
25  import org.apache.maven.project.MavenProject;
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      /**
61       * @deprecated Use {@link #LifecycleExecutionException(MessageBuilderFactory, MojoExecution, MavenProject, Throwable)}
62       * instead to allow for better message formatting.
63       */
64      @Deprecated
65      public LifecycleExecutionException(MojoExecution execution, MavenProject project, Throwable cause) {
66          this(createMessage(new DefaultMessageBuilderFactory(), execution, project, cause), execution, project, cause);
67      }
68  
69      public LifecycleExecutionException(
70              MessageBuilderFactory messageBuilderFactory,
71              MojoExecution execution,
72              MavenProject project,
73              Throwable cause) {
74          this(createMessage(messageBuilderFactory, execution, project, cause), execution, project, cause);
75      }
76  
77      public MavenProject getProject() {
78          return project;
79      }
80  
81      private static String createMessage(
82              MessageBuilderFactory messageBuilderFactory,
83              MojoExecution execution,
84              MavenProject project,
85              Throwable cause) {
86          MessageBuilder buffer = messageBuilderFactory.builder(256);
87  
88          buffer.a("Failed to execute goal");
89  
90          if (execution != null) {
91              buffer.a(' ');
92              buffer.mojo(execution.getGroupId()
93                      + ':'
94                      + execution.getArtifactId()
95                      + ':'
96                      + execution.getVersion()
97                      + ':'
98                      + execution.getGoal());
99              buffer.a(' ').strong('(' + execution.getExecutionId() + ')');
100         }
101 
102         if (project != null) {
103             buffer.a(" on project ");
104             buffer.project(project.getArtifactId());
105         }
106 
107         if (cause != null) {
108             buffer.a(": ").failure(cause.getMessage());
109         }
110 
111         return buffer.toString();
112     }
113 }