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.project.collector;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  import javax.inject.Inject;
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  import org.apache.maven.DefaultMaven;
27  import org.apache.maven.execution.MavenExecutionRequest;
28  import org.apache.maven.model.building.ModelSource;
29  import org.apache.maven.model.building.UrlModelSource;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.project.ProjectBuilder;
32  import org.apache.maven.project.ProjectBuildingException;
33  import org.apache.maven.project.ProjectBuildingRequest;
34  
35  /**
36   * Strategy to collect projects for building when the Maven invocation is not in a directory that contains a pom.xml.
37   */
38  @Named("PomlessCollectionStrategy")
39  @Singleton
40  public class PomlessCollectionStrategy implements ProjectCollectionStrategy {
41      private final ProjectBuilder projectBuilder;
42  
43      @Inject
44      public PomlessCollectionStrategy(ProjectBuilder projectBuilder) {
45          this.projectBuilder = projectBuilder;
46      }
47  
48      @Override
49      public List<MavenProject> collectProjects(final MavenExecutionRequest request) throws ProjectBuildingException {
50          ProjectBuildingRequest buildingRequest = request.getProjectBuildingRequest();
51          ModelSource modelSource = new UrlModelSource(DefaultMaven.class.getResource("project/standalone.xml"));
52          MavenProject project =
53                  projectBuilder.build(modelSource, buildingRequest).getProject();
54          project.setExecutionRoot(true);
55          request.setProjectPresent(false);
56  
57          return Arrays.asList(project);
58      }
59  }