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.DependencyScope;
25  import org.apache.maven.api.Exclusion;
26  import org.apache.maven.api.Type;
27  import org.apache.maven.api.VersionConstraint;
28  import org.apache.maven.api.annotations.Nonnull;
29  import org.apache.maven.api.annotations.Nullable;
30  import org.eclipse.aether.artifact.ArtifactProperties;
31  
32  import static org.apache.maven.internal.impl.Utils.nonNull;
33  
34  public class DefaultDependencyCoordinate implements DependencyCoordinate {
35      private final InternalSession session;
36      private final org.eclipse.aether.graph.Dependency dependency;
37  
38      public DefaultDependencyCoordinate(
39              @Nonnull InternalSession 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 VersionConstraint getVersion() {
66          return session.parseVersionConstraint(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.requireType(type);
80      }
81  
82      @Nonnull
83      @Override
84      public DependencyScope getScope() {
85          return session.requireDependencyScope(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 }