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