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.impl;
20
21 import java.util.Objects;
22
23 import org.apache.maven.api.DependencyScope;
24 import org.apache.maven.api.Type;
25 import org.apache.maven.api.annotations.Nonnull;
26 import org.eclipse.aether.artifact.ArtifactProperties;
27 import org.eclipse.aether.graph.Dependency;
28
29 /**
30 * Base class of {@code Dependency} or {@code DependencyCoordinates} implementations as a wrapper around
31 * an Eclipse Aether object. This class implements the methods that are common to {@code Dependency} and
32 * {@code DependencyCoordinates}, even if this class does not implement directly any of those interfaces.
33 * Having matching method signatures is sufficient, even if there is no {@code @Override} annotations.
34 *
35 * <p>The fact that this class is wrapping an Eclipse Aether object is an implementation details that may
36 * change in any future Maven version. For now, one purpose of this class is to have a single type to check
37 * for unwrapping the Eclipse Aether object.</p>
38 */
39 abstract class AetherDependencyWrapper {
40 /**
41 * The session to install / deploy / resolve artifacts and dependencies.
42 */
43 final InternalSession session;
44
45 /**
46 * The wrapped Eclipse Aether dependency.
47 */
48 final Dependency dependency;
49
50 /**
51 * Creates a new wrapper for the given dependency.
52 *
53 * @param dependency the Eclipse Aether dependency to wrap
54 */
55 AetherDependencyWrapper(@Nonnull InternalSession session, @Nonnull Dependency dependency) {
56 this.session = Objects.requireNonNull(session, "session");
57 this.dependency = Objects.requireNonNull(dependency, "dependency");
58 }
59
60 /**
61 * {@return the group identifier of the wrapped dependency}
62 * The default implementation delegates to the Eclipse Aether artifact.
63 */
64 public String getGroupId() {
65 return dependency.getArtifact().getGroupId();
66 }
67
68 /**
69 * {@return the artifact identifier of the wrapped dependency}
70 * The default implementation delegates to the Eclipse Aether artifact.
71 */
72 public String getArtifactId() {
73 return dependency.getArtifact().getArtifactId();
74 }
75
76 /**
77 * {@return the file extension of the wrapped dependency}
78 * The default implementation delegates to the Eclipse Aether artifact.
79 */
80 public String getExtension() {
81 return dependency.getArtifact().getExtension();
82 }
83
84 /**
85 * {@return the type of the wrapped dependency}
86 * The default implementation infers the type from the properties associated to the Eclipse Aether artifact.
87 */
88 public Type getType() {
89 String type = dependency.getArtifact().getProperty(ArtifactProperties.TYPE, getExtension());
90 return session.requireType(type);
91 }
92
93 /**
94 * {@return the classifier ("jar", "test-jar", …) of the wrapped dependency}
95 * The default implementation first delegates to the Eclipse Aether artifact.
96 * If the latter does not provide a non-empty classifier,
97 * then the default value is determined by {@linkplain #getType() type}.
98 */
99 @Nonnull
100 public String getClassifier() {
101 String classifier = dependency.getArtifact().getClassifier();
102 if (classifier.isEmpty()) {
103 classifier = getType().getClassifier();
104 if (classifier == null) {
105 classifier = "";
106 }
107 }
108 return classifier;
109 }
110
111 /**
112 * {@return the scope (compile, test, …) of this dependency}
113 */
114 @Nonnull
115 public DependencyScope getScope() {
116 return session.requireDependencyScope(dependency.getScope());
117 }
118
119 /**
120 * {@return a string representation of this dependency}
121 * This is for debugging purposes only and may change in any future version.
122 */
123 @Override
124 public String toString() {
125 return dependency.toString();
126 }
127 }