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