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