View Javadoc
1   package org.apache.maven.project.collector;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.DefaultMaven;
23  import org.apache.maven.execution.MavenExecutionRequest;
24  import org.apache.maven.model.building.ModelSource;
25  import org.apache.maven.model.building.UrlModelSource;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.project.ProjectBuilder;
28  import org.apache.maven.project.ProjectBuildingException;
29  import org.apache.maven.project.ProjectBuildingRequest;
30  
31  import javax.inject.Inject;
32  import javax.inject.Named;
33  import javax.inject.Singleton;
34  import java.util.Arrays;
35  import java.util.List;
36  
37  /**
38   * Strategy to collect projects for building when the Maven invocation is not in a directory that contains a pom.xml.
39   */
40  @Named( "PomlessCollectionStrategy" )
41  @Singleton
42  public class PomlessCollectionStrategy
43      implements ProjectCollectionStrategy
44  {
45      private final ProjectBuilder projectBuilder;
46  
47      @Inject
48      public PomlessCollectionStrategy( ProjectBuilder projectBuilder )
49      {
50          this.projectBuilder = projectBuilder;
51      }
52  
53      @Override
54      public List<MavenProject> collectProjects( final MavenExecutionRequest request )
55              throws ProjectBuildingException
56      {
57          ProjectBuildingRequest buildingRequest = request.getProjectBuildingRequest();
58          ModelSource modelSource = new UrlModelSource( DefaultMaven.class.getResource( "project/standalone.xml" ) );
59          MavenProject project = projectBuilder.build( modelSource,  buildingRequest ).getProject();
60          project.setExecutionRoot( true );
61          request.setProjectPresent( false );
62  
63          return Arrays.asList( project );
64      }
65  }