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