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.internal.impl;
20  
21  import java.util.Collection;
22  
23  import org.apache.maven.api.DependencyCoordinate;
24  import org.apache.maven.api.Exclusion;
25  import org.apache.maven.api.Scope;
26  import org.apache.maven.api.Type;
27  import org.apache.maven.api.VersionRange;
28  import org.apache.maven.api.annotations.Nonnull;
29  import org.apache.maven.api.annotations.Nullable;
30  import org.apache.maven.api.services.TypeRegistry;
31  import org.eclipse.aether.artifact.ArtifactProperties;
32  
33  import static org.apache.maven.internal.impl.Utils.nonNull;
34  
35  public class DefaultDependencyCoordinate implements DependencyCoordinate {
36      private final AbstractSession session;
37      private final org.eclipse.aether.graph.Dependency dependency;
38  
39      public DefaultDependencyCoordinate(
40              @Nonnull AbstractSession session, @Nonnull org.eclipse.aether.graph.Dependency dependency) {
41          this.session = nonNull(session, "session");
42          this.dependency = nonNull(dependency, "dependency");
43      }
44  
45      @Nonnull
46      public org.eclipse.aether.graph.Dependency getDependency() {
47          return dependency;
48      }
49  
50      @Override
51      public String getGroupId() {
52          return dependency.getArtifact().getGroupId();
53      }
54  
55      @Override
56      public String getArtifactId() {
57          return dependency.getArtifact().getArtifactId();
58      }
59  
60      @Override
61      public String getClassifier() {
62          return dependency.getArtifact().getClassifier();
63      }
64  
65      @Override
66      public VersionRange getVersion() {
67          return session.parseVersionRange(dependency.getArtifact().getVersion());
68      }
69  
70      @Override
71      public String getExtension() {
72          return dependency.getArtifact().getExtension();
73      }
74  
75      @Override
76      public Type getType() {
77          String type = dependency
78                  .getArtifact()
79                  .getProperty(ArtifactProperties.TYPE, dependency.getArtifact().getExtension());
80          return session.getService(TypeRegistry.class).getType(type);
81      }
82  
83      @Nonnull
84      @Override
85      public Scope getScope() {
86          return Scope.get(dependency.getScope());
87      }
88  
89      @Nullable
90      @Override
91      public Boolean getOptional() {
92          return dependency.getOptional();
93      }
94  
95      @Nonnull
96      @Override
97      public Collection<Exclusion> getExclusions() {
98          return new MappedCollection<>(dependency.getExclusions(), this::toExclusion);
99      }
100 
101     private Exclusion toExclusion(org.eclipse.aether.graph.Exclusion exclusion) {
102         return new Exclusion() {
103             @Nullable
104             @Override
105             public String getGroupId() {
106                 return exclusion.getGroupId();
107             }
108 
109             @Nullable
110             @Override
111             public String getArtifactId() {
112                 return exclusion.getArtifactId();
113             }
114         };
115     }
116 }