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