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