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.io.File;
22 import java.nio.file.Path;
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.*;
30 import org.apache.maven.api.annotations.Nonnull;
31 import org.apache.maven.api.annotations.Nullable;
32 import org.apache.maven.api.model.DependencyManagement;
33 import org.apache.maven.api.model.Model;
34 import org.apache.maven.api.services.ArtifactManager;
35 import org.apache.maven.api.services.TypeRegistry;
36 import org.apache.maven.project.MavenProject;
37
38 public class DefaultProject implements Project {
39
40 private final InternalSession session;
41 private final MavenProject project;
42
43 public DefaultProject(InternalSession session, MavenProject project) {
44 this.session = session;
45 this.project = project;
46 }
47
48 public InternalSession getSession() {
49 return session;
50 }
51
52 public MavenProject getProject() {
53 return project;
54 }
55
56 @Nonnull
57 @Override
58 public String getGroupId() {
59 return project.getGroupId();
60 }
61
62 @Nonnull
63 @Override
64 public String getArtifactId() {
65 return project.getArtifactId();
66 }
67
68 @Nonnull
69 @Override
70 public String getVersion() {
71 return project.getVersion();
72 }
73
74 @Nonnull
75 @Override
76 public Artifact getArtifact() {
77 org.eclipse.aether.artifact.Artifact resolverArtifact = RepositoryUtils.toArtifact(project.getArtifact());
78 Artifact artifact = session.getArtifact(resolverArtifact);
79 Path path =
80 resolverArtifact.getFile() != null ? resolverArtifact.getFile().toPath() : null;
81 session.getService(ArtifactManager.class).setPath(artifact, path);
82 return artifact;
83 }
84
85 @Nonnull
86 @Override
87 public String getPackaging() {
88 return project.getPackaging();
89 }
90
91 @Nonnull
92 @Override
93 public Model getModel() {
94 return project.getModel().getDelegate();
95 }
96
97 @Nonnull
98 @Override
99 public Optional<Path> getPomPath() {
100 File file = project.getFile();
101 return Optional.ofNullable(file).map(File::toPath);
102 }
103
104 @Nonnull
105 @Override
106 public List<DependencyCoordinate> getDependencies() {
107 return new MappedList<>(getModel().getDependencies(), this::toDependency);
108 }
109
110 @Nonnull
111 @Override
112 public List<DependencyCoordinate> getManagedDependencies() {
113 DependencyManagement dependencyManagement = getModel().getDependencyManagement();
114 if (dependencyManagement != null) {
115 return new MappedList<>(dependencyManagement.getDependencies(), this::toDependency);
116 }
117 return Collections.emptyList();
118 }
119
120 @Override
121 public boolean isExecutionRoot() {
122 return project.isExecutionRoot();
123 }
124
125 @Override
126 public boolean isTopProject() {
127 return getBasedir().isPresent()
128 && getBasedir().get().equals(getSession().getTopDirectory());
129 }
130
131 @Override
132 public boolean isRootProject() {
133 return getBasedir().isPresent() && getBasedir().get().equals(getRootDirectory());
134 }
135
136 @Override
137 public Path getRootDirectory() {
138 return project.getRootDirectory();
139 }
140
141 @Override
142 public Optional<Project> getParent() {
143 MavenProject parent = project.getParent();
144 return parent != null ? Optional.of(session.getProject(parent)) : Optional.empty();
145 }
146
147 @Override
148 public List<RemoteRepository> getRemoteProjectRepositories() {
149 return new MappedList<>(project.getRemoteProjectRepositories(), session::getRemoteRepository);
150 }
151
152 @Override
153 public List<RemoteRepository> getRemotePluginRepositories() {
154 return new MappedList<>(project.getRemotePluginRepositories(), session::getRemoteRepository);
155 }
156
157 @Nonnull
158 private DependencyCoordinate toDependency(org.apache.maven.api.model.Dependency dependency) {
159 return new DependencyCoordinate() {
160 @Override
161 public String getGroupId() {
162 return dependency.getGroupId();
163 }
164
165 @Override
166 public String getArtifactId() {
167 return dependency.getArtifactId();
168 }
169
170 @Override
171 public String getClassifier() {
172 return dependency.getClassifier();
173 }
174
175 @Override
176 public VersionConstraint getVersion() {
177 return session.parseVersionConstraint(dependency.getVersion());
178 }
179
180 @Override
181 public String getExtension() {
182 return getType().getExtension();
183 }
184
185 @Override
186 public Type getType() {
187 String type = dependency.getType();
188 return session.getService(TypeRegistry.class).getType(type);
189 }
190
191 @Nonnull
192 @Override
193 public Scope getScope() {
194 return Scope.get(dependency.getScope());
195 }
196
197 @Override
198 public Boolean getOptional() {
199 return dependency.isOptional();
200 }
201
202 @Nonnull
203 @Override
204 public Collection<Exclusion> getExclusions() {
205 return new MappedCollection<>(dependency.getExclusions(), this::toExclusion);
206 }
207
208 private Exclusion toExclusion(org.apache.maven.api.model.Exclusion exclusion) {
209 return new Exclusion() {
210 @Nullable
211 @Override
212 public String getGroupId() {
213 return exclusion.getGroupId();
214 }
215
216 @Nullable
217 @Override
218 public String getArtifactId() {
219 return exclusion.getArtifactId();
220 }
221 };
222 }
223 };
224 }
225 }