1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.compiler;
20
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import java.util.Set;
26
27 import org.apache.maven.api.Exclusion;
28 import org.apache.maven.api.Project;
29 import org.apache.maven.api.Session;
30 import org.apache.maven.api.plugin.MojoException;
31 import org.apache.maven.api.services.DependencyCoordinatesFactory;
32 import org.apache.maven.api.services.DependencyCoordinatesFactoryRequest;
33
34
35
36
37
38
39
40
41
42 @Deprecated(since = "4.0.0")
43 public final class DependencyCoordinate {
44 private String groupId;
45
46 private String artifactId;
47
48 private String version;
49
50 private String classifier;
51
52 private String type = "jar";
53
54 private Set<DependencyExclusion> exclusions;
55
56 @Override
57 public int hashCode() {
58 return Objects.hash(groupId, artifactId, version, classifier, type, exclusions);
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj) {
64 return true;
65 }
66 return obj instanceof DependencyCoordinate other
67 && Objects.equals(groupId, other.groupId)
68 && Objects.equals(artifactId, other.artifactId)
69 && Objects.equals(version, other.version)
70 && Objects.equals(classifier, other.classifier)
71 && Objects.equals(type, other.type)
72 && Objects.equals(exclusions, other.exclusions);
73 }
74
75 @Override
76 public String toString() {
77 return groupId + ":" + artifactId + (version != null ? ":" + version : "")
78 + (classifier != null ? ":" + classifier : "") + (type != null ? "." + type : "");
79 }
80
81
82
83
84
85
86
87
88 org.apache.maven.api.DependencyCoordinates toCoordinate(Project project, Session session) {
89 return session.getService(DependencyCoordinatesFactory.class)
90 .create(DependencyCoordinatesFactoryRequest.builder()
91 .session(session)
92 .groupId(groupId)
93 .artifactId(artifactId)
94 .classifier(classifier)
95 .type(type)
96 .version(version)
97 .version(getAnnotationProcessorPathVersion(project))
98 .exclusions(toExclusions(exclusions))
99 .build());
100 }
101
102 private String getAnnotationProcessorPathVersion(Project project) throws MojoException {
103 if (version != null) {
104 return version;
105 } else {
106 if (classifier == null) {
107 classifier = "";
108 }
109 List<org.apache.maven.api.DependencyCoordinates> managedDependencies = project.getManagedDependencies();
110 return findManagedVersion(managedDependencies)
111 .orElseThrow(() -> new CompilationFailureException(String.format(
112 "Cannot find version for annotation processor path '%s'.%nThe version needs to be either"
113 + " provided directly in the plugin configuration or via dependency management.",
114 this)));
115 }
116 }
117
118 private Optional<String> findManagedVersion(List<org.apache.maven.api.DependencyCoordinates> managedDependencies) {
119 return managedDependencies.stream()
120 .filter(dep -> Objects.equals(dep.getGroupId(), groupId)
121 && Objects.equals(dep.getArtifactId(), artifactId)
122 && Objects.equals(dep.getClassifier(), classifier)
123 && Objects.equals(dep.getType().id(), type))
124 .findAny()
125 .map(d -> d.getVersionConstraint().asString());
126 }
127
128 private static Collection<Exclusion> toExclusions(Set<DependencyExclusion> exclusions) {
129 if (exclusions == null || exclusions.isEmpty()) {
130 return List.of();
131 }
132 return exclusions.stream()
133 .map(e -> (Exclusion) new Exclusion() {
134 @Override
135 public String getGroupId() {
136 return e.groupId;
137 }
138
139 @Override
140 public String getArtifactId() {
141 return e.artifactId;
142 }
143 })
144 .toList();
145 }
146 }