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.artifact.resolver;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25
26 /**
27 * Exception caused when one or more artifacts can not be resolved because they are not found in the
28 * local or remote repositories.
29 */
30 public class MultipleArtifactsNotFoundException extends ArtifactResolutionException {
31 private static final String LS = System.lineSeparator();
32
33 private final List<Artifact> resolvedArtifacts;
34 private final List<Artifact> missingArtifacts;
35
36 /**
37 * @param originatingArtifact the artifact that was being resolved
38 * @param missingArtifacts artifacts that could not be resolved
39 * @param remoteRepositories remote repositories where the missing artifacts were not found
40 * @deprecated use {@link #MultipleArtifactsNotFoundException(Artifact, List, List, List)}
41 */
42 @Deprecated
43 public MultipleArtifactsNotFoundException(
44 Artifact originatingArtifact,
45 List<Artifact> missingArtifacts,
46 List<ArtifactRepository> remoteRepositories) {
47 this(originatingArtifact, new ArrayList<>(), missingArtifacts, remoteRepositories);
48 }
49
50 /**
51 * Create an instance of the exception with all required information.
52 *
53 * @param originatingArtifact the artifact that was being resolved
54 * @param resolvedArtifacts artifacts that could be resolved
55 * @param missingArtifacts artifacts that could not be resolved
56 * @param remoteRepositories remote repositories where the missing artifacts were not found
57 */
58 public MultipleArtifactsNotFoundException(
59 Artifact originatingArtifact,
60 List<Artifact> resolvedArtifacts,
61 List<Artifact> missingArtifacts,
62 List<ArtifactRepository> remoteRepositories) {
63 super(constructMessage(missingArtifacts), originatingArtifact, remoteRepositories);
64 this.resolvedArtifacts = resolvedArtifacts;
65 this.missingArtifacts = missingArtifacts;
66 }
67
68 /**
69 * artifacts that could be resolved
70 *
71 * @return {@link List} of {@link Artifact}
72 */
73 public List<Artifact> getResolvedArtifacts() {
74 return resolvedArtifacts;
75 }
76
77 /**
78 * artifacts that could NOT be resolved
79 *
80 * @return {@link List} of {@link Artifact}
81 */
82 public List<Artifact> getMissingArtifacts() {
83 return missingArtifacts;
84 }
85
86 private static String constructMessage(List<Artifact> artifacts) {
87 StringBuilder buffer = new StringBuilder(256);
88
89 buffer.append("Missing:").append(LS);
90 buffer.append("----------").append(LS);
91
92 int counter = 0;
93
94 for (Artifact artifact : artifacts) {
95 String message = (++counter) + ") " + artifact.getId();
96
97 buffer.append(constructMissingArtifactMessage(
98 message,
99 " ",
100 artifact.getGroupId(),
101 artifact.getArtifactId(),
102 artifact.getVersion(),
103 artifact.getType(),
104 artifact.getClassifier(),
105 artifact.getDownloadUrl(),
106 artifact.getDependencyTrail()));
107 }
108
109 buffer.append("----------").append(LS);
110
111 int size = artifacts.size();
112
113 buffer.append(size).append(" required artifact");
114
115 if (size > 1) {
116 buffer.append("s are");
117 } else {
118 buffer.append(" is");
119 }
120
121 buffer.append(" missing.").append(LS).append(LS).append("for artifact: ");
122
123 return buffer.toString();
124 }
125 }