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   */
29  public class LifecycleExecutionException extends Exception {
30      private MavenProject project;
31  
32      public LifecycleExecutionException(String message) {
33          super(message);
34      }
35  
36      public LifecycleExecutionException(Throwable cause) {
37          super(cause);
38      }
39  
40      public LifecycleExecutionException(String message, Throwable cause) {
41          super(message, cause);
42      }
43  
44      public LifecycleExecutionException(String message, MavenProject project) {
45          super(message);
46          this.project = project;
47      }
48  
49      public LifecycleExecutionException(String message, MojoExecution execution, MavenProject project) {
50          super(message);
51          this.project = project;
52      }
53  
54      public LifecycleExecutionException(String message, MojoExecution execution, MavenProject project, Throwable cause) {
55          super(message, cause);
56          this.project = project;
57      }
58  
59      public LifecycleExecutionException(MojoExecution execution, MavenProject project, Throwable cause) {
60          this(new DefaultMessageBuilderFactory(), execution, project, cause);
61      }
62  
63      public LifecycleExecutionException(
64              MessageBuilderFactory messageBuilderFactory,
65              MojoExecution execution,
66              MavenProject project,
67              Throwable cause) {
68          this(createMessage(messageBuilderFactory, execution, project, cause), execution, project, cause);
69      }
70  
71      public MavenProject getProject() {
72          return project;
73      }
74  
75      private static String createMessage(
76              MessageBuilderFactory messageBuilderFactory,
77              MojoExecution execution,
78              MavenProject project,
79              Throwable cause) {
80          MessageBuilder buffer = messageBuilderFactory.builder(256);
81  
82          buffer.a("Failed to execute goal");
83  
84          if (execution != null) {
85              buffer.a(' ');
86              buffer.mojo(execution.getGroupId()
87                      + ':'
88                      + execution.getArtifactId()
89                      + ':'
90                      + execution.getVersion()
91                      + ':'
92                      + execution.getGoal());
93              buffer.a(' ').strong('(' + execution.getExecutionId() + ')');
94          }
95  
96          if (project != null) {
97              buffer.a(" on project ");
98              buffer.project(project.getArtifactId());
99          }
100 
101         if (cause != null) {
102             buffer.a(": ").failure(cause.getMessage());
103         }
104 
105         return buffer.toString();
106     }
107 }