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.io.IOException;
23 import java.io.InputStream;
24 import java.nio.file.Path;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Optional;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.inject.Singleton;
31 import org.apache.maven.RepositoryUtils;
32 import org.apache.maven.api.Artifact;
33 import org.apache.maven.api.ArtifactCoordinate;
34 import org.apache.maven.api.Node;
35 import org.apache.maven.api.Project;
36 import org.apache.maven.api.annotations.Nonnull;
37 import org.apache.maven.api.services.BuilderProblem;
38 import org.apache.maven.api.services.DependencyCollectorResult;
39 import org.apache.maven.api.services.ProjectBuilder;
40 import org.apache.maven.api.services.ProjectBuilderException;
41 import org.apache.maven.api.services.ProjectBuilderRequest;
42 import org.apache.maven.api.services.ProjectBuilderResult;
43 import org.apache.maven.api.services.Source;
44 import org.apache.maven.artifact.DefaultArtifact;
45 import org.apache.maven.artifact.repository.ArtifactRepository;
46 import org.apache.maven.model.building.ModelProblem;
47 import org.apache.maven.model.building.ModelSource;
48 import org.apache.maven.project.DefaultProjectBuildingRequest;
49 import org.apache.maven.project.ProjectBuildingException;
50 import org.apache.maven.project.ProjectBuildingRequest;
51 import org.apache.maven.project.ProjectBuildingResult;
52
53 @Named
54 @Singleton
55 public class DefaultProjectBuilder implements ProjectBuilder {
56
57 private final org.apache.maven.project.ProjectBuilder builder;
58
59 @Inject
60 public DefaultProjectBuilder(org.apache.maven.project.ProjectBuilder builder) {
61 this.builder = builder;
62 }
63
64 @SuppressWarnings("MethodLength")
65 @Nonnull
66 @Override
67 public ProjectBuilderResult build(ProjectBuilderRequest request)
68 throws ProjectBuilderException, IllegalArgumentException {
69 DefaultSession session = (DefaultSession) request.getSession();
70 try {
71 List<ArtifactRepository> repositories = session.toArtifactRepositories(session.getRemoteRepositories());
72 ProjectBuildingRequest req = new DefaultProjectBuildingRequest()
73 .setRepositorySession(session.getSession())
74 .setRemoteRepositories(repositories)
75 .setPluginArtifactRepositories(repositories)
76 .setProcessPlugins(request.isProcessPlugins());
77 ProjectBuildingResult res;
78 if (request.getPath().isPresent()) {
79 Path path = request.getPath().get();
80 res = builder.build(path.toFile(), req);
81 } else if (request.getSource().isPresent()) {
82 Source source = request.getSource().get();
83 ModelSource modelSource = new ModelSource() {
84 @Override
85 public InputStream getInputStream() throws IOException {
86 return source.getInputStream();
87 }
88
89 @Override
90 public String getLocation() {
91 return source.getLocation();
92 }
93 };
94 res = builder.build(modelSource, req);
95 } else if (request.getArtifact().isPresent()) {
96 Artifact a = request.getArtifact().get();
97 org.eclipse.aether.artifact.Artifact aetherArtifact = session.toArtifact(a);
98 org.apache.maven.artifact.Artifact artifact = RepositoryUtils.toArtifact(aetherArtifact);
99 res = builder.build(artifact, request.isAllowStubModel(), req);
100 } else if (request.getCoordinate().isPresent()) {
101 ArtifactCoordinate c = request.getCoordinate().get();
102 org.apache.maven.artifact.Artifact artifact = new DefaultArtifact(
103 c.getGroupId(),
104 c.getArtifactId(),
105 c.getVersion().asString(),
106 null,
107 c.getExtension(),
108 c.getClassifier(),
109 null);
110 res = builder.build(artifact, request.isAllowStubModel(), req);
111 } else {
112 throw new IllegalArgumentException("Invalid request");
113 }
114 return new ProjectBuilderResult() {
115 @Nonnull
116 @Override
117 public String getProjectId() {
118 return res.getProjectId();
119 }
120
121 @Nonnull
122 @Override
123 public Optional<Path> getPomFile() {
124 return Optional.ofNullable(res.getPomFile()).map(File::toPath);
125 }
126
127 @Nonnull
128 @Override
129 public Optional<Project> getProject() {
130 return Optional.ofNullable(res.getProject()).map(session::getProject);
131 }
132
133 @Nonnull
134 @Override
135 public Collection<BuilderProblem> getProblems() {
136 return new MappedCollection<>(res.getProblems(), this::toProblem);
137 }
138
139 private BuilderProblem toProblem(ModelProblem problem) {
140 return new BuilderProblem() {
141 @Override
142 public String getSource() {
143 return problem.getSource();
144 }
145
146 @Override
147 public int getLineNumber() {
148 return problem.getLineNumber();
149 }
150
151 @Override
152 public int getColumnNumber() {
153 return problem.getColumnNumber();
154 }
155
156 @Override
157 public String getLocation() {
158 StringBuilder buffer = new StringBuilder(256);
159
160 if (getSource().length() > 0) {
161 if (buffer.length() > 0) {
162 buffer.append(", ");
163 }
164 buffer.append(getSource());
165 }
166
167 if (getLineNumber() > 0) {
168 if (buffer.length() > 0) {
169 buffer.append(", ");
170 }
171 buffer.append("line ").append(getLineNumber());
172 }
173
174 if (getColumnNumber() > 0) {
175 if (buffer.length() > 0) {
176 buffer.append(", ");
177 }
178 buffer.append("column ").append(getColumnNumber());
179 }
180
181 return buffer.toString();
182 }
183
184 @Override
185 public Exception getException() {
186 return problem.getException();
187 }
188
189 @Override
190 public String getMessage() {
191 return problem.getMessage();
192 }
193
194 @Override
195 public Severity getSeverity() {
196 return Severity.valueOf(problem.getSeverity().name());
197 }
198 };
199 }
200
201 @Nonnull
202 @Override
203 public Optional<DependencyCollectorResult> getDependencyResolverResult() {
204 return Optional.ofNullable(res.getDependencyResolutionResult())
205 .map(r -> new DependencyCollectorResult() {
206 @Override
207 public List<Exception> getExceptions() {
208 return r.getCollectionErrors();
209 }
210
211 @Override
212 public Node getRoot() {
213 return session.getNode(r.getDependencyGraph());
214 }
215
216
217
218
219
220
221
222 });
223 }
224 };
225 } catch (ProjectBuildingException e) {
226 throw new ProjectBuilderException("Unable to build project", e);
227 }
228 }
229 }