View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.net.URI;
29  import java.nio.file.Path;
30  import java.util.Collection;
31  import java.util.List;
32  import java.util.Optional;
33  
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.repository.ArtifactRepository;
45  import org.apache.maven.model.building.ModelProblem;
46  import org.apache.maven.model.building.ModelSource2;
47  import org.apache.maven.project.DefaultProjectBuildingRequest;
48  import org.apache.maven.project.ProjectBuildingException;
49  import org.apache.maven.project.ProjectBuildingRequest;
50  import org.apache.maven.project.ProjectBuildingResult;
51  
52  @Named
53  @Singleton
54  public class DefaultProjectBuilder implements ProjectBuilder {
55  
56      private final org.apache.maven.project.ProjectBuilder builder;
57  
58      @Inject
59      public DefaultProjectBuilder(org.apache.maven.project.ProjectBuilder builder) {
60          this.builder = builder;
61      }
62  
63      @SuppressWarnings("MethodLength")
64      @Nonnull
65      @Override
66      public ProjectBuilderResult build(ProjectBuilderRequest request)
67              throws ProjectBuilderException, IllegalArgumentException {
68          InternalSession session = InternalSession.from(request.getSession());
69          try {
70              List<ArtifactRepository> repositories = session.toArtifactRepositories(session.getRemoteRepositories());
71              ProjectBuildingRequest req = new DefaultProjectBuildingRequest()
72                      .setRepositorySession(session.getSession())
73                      .setRemoteRepositories(repositories)
74                      .setPluginArtifactRepositories(repositories)
75                      .setProcessPlugins(request.isProcessPlugins());
76              ProjectBuildingResult res;
77              if (request.getPath().isPresent()) {
78                  Path path = request.getPath().get();
79                  res = builder.build(path.toFile(), req);
80              } else if (request.getSource().isPresent()) {
81                  Source source = request.getSource().get();
82                  ModelSource2 modelSource = new SourceWrapper(source);
83                  res = builder.build(modelSource, req);
84              } else {
85                  throw new IllegalArgumentException("Invalid request");
86              }
87              return new ProjectBuilderResult() {
88                  @Nonnull
89                  @Override
90                  public String getProjectId() {
91                      return res.getProjectId();
92                  }
93  
94                  @Nonnull
95                  @Override
96                  public Optional<Path> getPomFile() {
97                      return Optional.ofNullable(res.getPomFile()).map(File::toPath);
98                  }
99  
100                 @Nonnull
101                 @Override
102                 public Optional<Project> getProject() {
103                     return Optional.ofNullable(res.getProject()).map(session::getProject);
104                 }
105 
106                 @Nonnull
107                 @Override
108                 public Collection<BuilderProblem> getProblems() {
109                     return new MappedCollection<>(res.getProblems(), this::toProblem);
110                 }
111 
112                 private BuilderProblem toProblem(ModelProblem problem) {
113                     return new BuilderProblem() {
114                         @Override
115                         public String getSource() {
116                             return problem.getSource();
117                         }
118 
119                         @Override
120                         public int getLineNumber() {
121                             return problem.getLineNumber();
122                         }
123 
124                         @Override
125                         public int getColumnNumber() {
126                             return problem.getColumnNumber();
127                         }
128 
129                         @Override
130                         public String getLocation() {
131                             StringBuilder buffer = new StringBuilder(256);
132 
133                             if (!getSource().isEmpty()) {
134                                 buffer.append(getSource());
135                             }
136 
137                             if (getLineNumber() > 0) {
138                                 if (buffer.length() > 0) {
139                                     buffer.append(", ");
140                                 }
141                                 buffer.append("line ").append(getLineNumber());
142                             }
143 
144                             if (getColumnNumber() > 0) {
145                                 if (buffer.length() > 0) {
146                                     buffer.append(", ");
147                                 }
148                                 buffer.append("column ").append(getColumnNumber());
149                             }
150 
151                             return buffer.toString();
152                         }
153 
154                         @Override
155                         public Exception getException() {
156                             return problem.getException();
157                         }
158 
159                         @Override
160                         public String getMessage() {
161                             return problem.getMessage();
162                         }
163 
164                         @Override
165                         public Severity getSeverity() {
166                             return Severity.valueOf(problem.getSeverity().name());
167                         }
168                     };
169                 }
170 
171                 @Nonnull
172                 @Override
173                 public Optional<DependencyCollectorResult> getDependencyResolverResult() {
174                     return Optional.ofNullable(res.getDependencyResolutionResult())
175                             .map(r -> new DependencyCollectorResult() {
176                                 @Override
177                                 public List<Exception> getExceptions() {
178                                     return r.getCollectionErrors();
179                                 }
180 
181                                 @Override
182                                 public Node getRoot() {
183                                     return session.getNode(r.getDependencyGraph());
184                                 }
185                             });
186                 }
187             };
188         } catch (ProjectBuildingException e) {
189             throw new ProjectBuilderException("Unable to build project", e);
190         }
191     }
192 
193     private static class SourceWrapper implements ModelSource2 {
194         private final Source source;
195 
196         SourceWrapper(Source source) {
197             this.source = source;
198         }
199 
200         @Override
201         public InputStream getInputStream() throws IOException {
202             return source.openStream();
203         }
204 
205         @Override
206         public String getLocation() {
207             return source.getLocation();
208         }
209 
210         @Override
211         public ModelSource2 getRelatedSource(String relPath) {
212             Source rel = source.resolve(relPath);
213             return rel != null ? new SourceWrapper(rel) : null;
214         }
215 
216         @Override
217         public URI getLocationURI() {
218             Path path = source.getPath();
219             return path != null ? path.toUri() : URI.create(source.getLocation());
220         }
221     }
222 }