1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.nio.file.Path;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Optional;
27
28 import org.apache.maven.RepositoryUtils;
29 import org.apache.maven.api.DependencyCoordinates;
30 import org.apache.maven.api.DependencyScope;
31 import org.apache.maven.api.Exclusion;
32 import org.apache.maven.api.Packaging;
33 import org.apache.maven.api.ProducedArtifact;
34 import org.apache.maven.api.Project;
35 import org.apache.maven.api.Type;
36 import org.apache.maven.api.VersionConstraint;
37 import org.apache.maven.api.annotations.Nonnull;
38 import org.apache.maven.api.annotations.Nullable;
39 import org.apache.maven.api.model.DependencyManagement;
40 import org.apache.maven.api.model.Model;
41 import org.apache.maven.project.MavenProject;
42 import org.apache.maven.project.artifact.ProjectArtifact;
43 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
44
45 import static org.apache.maven.internal.impl.Utils.nonNull;
46
47 public class DefaultProject implements Project {
48
49 private final InternalMavenSession session;
50 private final MavenProject project;
51 private final Packaging packaging;
52
53 public DefaultProject(InternalMavenSession session, MavenProject project) {
54 this.session = session;
55 this.project = project;
56 ClassLoader ttcl = Thread.currentThread().getContextClassLoader();
57 try {
58 Thread.currentThread().setContextClassLoader(project.getClassRealm());
59 this.packaging = session.requirePackaging(project.getPackaging());
60 } finally {
61 Thread.currentThread().setContextClassLoader(ttcl);
62 }
63 }
64
65 public InternalMavenSession getSession() {
66 return session;
67 }
68
69 public MavenProject getProject() {
70 return project;
71 }
72
73 @Nonnull
74 @Override
75 public String getGroupId() {
76 return project.getGroupId();
77 }
78
79 @Nonnull
80 @Override
81 public String getArtifactId() {
82 return project.getArtifactId();
83 }
84
85 @Nonnull
86 @Override
87 public String getVersion() {
88 return project.getVersion();
89 }
90
91 @Nonnull
92 @Override
93 public List<ProducedArtifact> getArtifacts() {
94 org.eclipse.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact(new ProjectArtifact(project));
95 org.eclipse.aether.artifact.Artifact projectArtifact = RepositoryUtils.toArtifact(project.getArtifact());
96
97 ArrayList<ProducedArtifact> result = new ArrayList<>(2);
98 result.add(session.getArtifact(ProducedArtifact.class, pomArtifact));
99 if (!ArtifactIdUtils.equalsVersionlessId(pomArtifact, projectArtifact)) {
100 result.add(session.getArtifact(ProducedArtifact.class, projectArtifact));
101 }
102 return Collections.unmodifiableList(result);
103 }
104
105 @Nonnull
106 @Override
107 public Packaging getPackaging() {
108 return packaging;
109 }
110
111 @Nonnull
112 @Override
113 public Model getModel() {
114 return project.getModel().getDelegate();
115 }
116
117 @Nonnull
118 @Override
119 public Path getPomPath() {
120 return nonNull(project.getFile(), "pomPath").toPath();
121 }
122
123 @Override
124 public Path getBasedir() {
125 return nonNull(project.getBasedir(), "basedir").toPath();
126 }
127
128 @Nonnull
129 @Override
130 public List<DependencyCoordinates> getDependencies() {
131 return new MappedList<>(getModel().getDependencies(), this::toDependency);
132 }
133
134 @Nonnull
135 @Override
136 public List<DependencyCoordinates> getManagedDependencies() {
137 DependencyManagement dependencyManagement = getModel().getDependencyManagement();
138 if (dependencyManagement != null) {
139 return new MappedList<>(dependencyManagement.getDependencies(), this::toDependency);
140 }
141 return Collections.emptyList();
142 }
143
144 @Override
145 public boolean isTopProject() {
146 return getBasedir().equals(getSession().getTopDirectory());
147 }
148
149 @Override
150 public boolean isRootProject() {
151 return getBasedir().equals(getRootDirectory());
152 }
153
154 @Override
155 public Path getRootDirectory() {
156 return project.getRootDirectory();
157 }
158
159 @Override
160 public Optional<Project> getParent() {
161 MavenProject parent = project.getParent();
162 return parent != null ? Optional.of(session.getProject(parent)) : Optional.empty();
163 }
164
165 @Nonnull
166 private DependencyCoordinates toDependency(org.apache.maven.api.model.Dependency dependency) {
167 return new DependencyCoordinates() {
168 @Override
169 public String getGroupId() {
170 return dependency.getGroupId();
171 }
172
173 @Override
174 public String getArtifactId() {
175 return dependency.getArtifactId();
176 }
177
178 @Override
179 public String getClassifier() {
180 String classifier = dependency.getClassifier();
181 if (classifier == null || classifier.isEmpty()) {
182 classifier = getType().getClassifier();
183 if (classifier == null) {
184 classifier = "";
185 }
186 }
187 return classifier;
188 }
189
190 @Override
191 public VersionConstraint getVersionConstraint() {
192 return session.parseVersionConstraint(dependency.getVersion());
193 }
194
195 @Override
196 public String getExtension() {
197 return getType().getExtension();
198 }
199
200 @Override
201 public Type getType() {
202 String type = dependency.getType();
203 return session.requireType(type);
204 }
205
206 @Nonnull
207 @Override
208 public DependencyScope getScope() {
209 String scope = dependency.getScope() != null ? dependency.getScope() : "";
210 return session.requireDependencyScope(scope);
211 }
212
213 @Override
214 public Boolean getOptional() {
215 return dependency.isOptional();
216 }
217
218 @Nonnull
219 @Override
220 public Collection<Exclusion> getExclusions() {
221 return new MappedCollection<>(dependency.getExclusions(), this::toExclusion);
222 }
223
224 private Exclusion toExclusion(org.apache.maven.api.model.Exclusion exclusion) {
225 return new Exclusion() {
226 @Nullable
227 @Override
228 public String getGroupId() {
229 return exclusion.getGroupId();
230 }
231
232 @Nullable
233 @Override
234 public String getArtifactId() {
235 return exclusion.getArtifactId();
236 }
237 };
238 }
239 };
240 }
241 }