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.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   * Simple representation of Maven-coordinates of a dependency.
36   *
37   * @author Andreas Gudian
38   * @since 3.4
39   *
40   * @deprecated Used for {@link AbstractCompilerMojo#annotationProcessorPaths}, which is deprecated.
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       * Converts this coordinate to the Maven API.
83       *
84       * @param project the current project
85       * @param session the current build session instance
86       * @return this coordinate as Maven API
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 = ""; // Needed for comparison with dep.getClassifier() because of method contract.
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 }