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.plugin.version;
20  
21  import java.util.List;
22  import org.eclipse.aether.repository.LocalRepository;
23  import org.eclipse.aether.repository.RemoteRepository;
24  
25  /**
26   * PluginVersionResolutionException
27   */
28  public class PluginVersionResolutionException extends Exception {
29      private final String groupId;
30  
31      private final String artifactId;
32  
33      private final String baseMessage;
34  
35      public PluginVersionResolutionException(String groupId, String artifactId, String baseMessage, Throwable cause) {
36          super("Error resolving version for plugin '" + groupId + ":" + artifactId + "': " + baseMessage, cause);
37  
38          this.groupId = groupId;
39          this.artifactId = artifactId;
40          this.baseMessage = baseMessage;
41      }
42  
43      public PluginVersionResolutionException(String groupId, String artifactId, String baseMessage) {
44          super("Error resolving version for plugin '" + groupId + ":" + artifactId + "': " + baseMessage);
45  
46          this.groupId = groupId;
47          this.artifactId = artifactId;
48          this.baseMessage = baseMessage;
49      }
50  
51      public PluginVersionResolutionException(
52              String groupId,
53              String artifactId,
54              LocalRepository localRepository,
55              List<RemoteRepository> remoteRepositories,
56              String baseMessage) {
57          super("Error resolving version for plugin '" + groupId + ":" + artifactId + "' from the repositories "
58                  + format(localRepository, remoteRepositories) + ": " + baseMessage);
59  
60          this.groupId = groupId;
61          this.artifactId = artifactId;
62          this.baseMessage = baseMessage;
63      }
64  
65      public String getGroupId() {
66          return groupId;
67      }
68  
69      public String getArtifactId() {
70          return artifactId;
71      }
72  
73      public String getBaseMessage() {
74          return baseMessage;
75      }
76  
77      private static String format(LocalRepository localRepository, List<RemoteRepository> remoteRepositories) {
78          StringBuilder repos = new StringBuilder("[");
79  
80          if (localRepository != null) {
81              repos.append(localRepository.getId())
82                      .append(" (")
83                      .append(localRepository.getBasedir())
84                      .append(")");
85          }
86  
87          if (remoteRepositories != null && !remoteRepositories.isEmpty()) {
88              for (RemoteRepository repository : remoteRepositories) {
89                  repos.append(", ");
90  
91                  if (repository != null) {
92                      repos.append(repository.getId())
93                              .append(" (")
94                              .append(repository.getUrl())
95                              .append(")");
96                  }
97              }
98          }
99  
100         repos.append("]");
101 
102         return repos.toString();
103     }
104 }