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